Note: The APIs in wix-window-frontend.ConsentPolicy
can only be used in frontend code.
The consent policy helps you comply with GDPR and CCPA regulations, and allows site visitors to control their information.
Set up the site consent policy and cookie consent banners in a site's Privacy Center.
To use the ConsentPolicy API, import { consentPolicy }
from the wix-window-frontend
module:
import { consentPolicy } from "wix-window-frontend";
Gets the site visitor's consent policy regarding allowed cookies and 3rd-party data transfers for GDPR or CCPA compliance.
Retrieves the site visitor's consent policy details, including which cookies are allowed and whether data transfer to 3rd parties is permitted.
function getCurrentConsentPolicy(): PolicyDetails;
import { consentPolicy } from "wix-window-frontend";
// ...
const policyDetails = consentPolicy.getCurrentConsentPolicy();
const policy = policyDetails.policy;
/* policyDetails value:
*
* {
* "defaultPolicy" : true,
* "policy" : {
* "essential" : true,
* "functional" : true,
* "analytics" : true,
* "advertising" : true,
* "dataToThirdParty" : true
* },
* "createdDate" : 2020-07-20T12:33:09.775Z
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Triggered when a site visitor's consent policy was changed using
setConsentPolicy()
or reset using
resetConsentPolicy()
.
Use the onConsentPolicyChanged()
method for code you want to run after
the site visitor's current consent policy was changed using
setConsentPolicy()
or reset using
resetConsentPolicy()
.
Usually, you want to call the onConsentPolicyChanged()
method in the masterpage.js file so that the onConsentPolicyChanged()
event handler runs no matter which
page on your site is used to change the policy.
function onConsentPolicyChanged(handler: function): void;
handler(event: ConsentPolicyChangedEvent): void
The name of the function or
the function expression to run when the consent policy is changed.
import { consentPolicy } from "wix-window-frontend";
consentPolicy.onConsentPolicyChanged((event) => {
const policy = event.policy;
console.log(event);
});
/* Full event object:
* {
* "defaultPolicy": false,
* "policy": {
* "functional": false,
* "analytics": true,
* "advertising": false,
* "dataToThirdParty": false,
* "essential": true
* },
* "createdDate": "2021-07-04T23:36:00.000Z"
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Removes the current policy from the site visitor's browser and resets the site visitor's consent policy to the default policy for the site.
function resetConsentPolicy(): Promise<void>;
import { consentPolicy } from "wix-window-frontend";
// ...
consentPolicy
.resetConsentPolicy()
.then((policy) => {
console.log("The policy is now set to the default site policy.");
return policy;
})
.catch((error) => {
console.error(error);
});
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Sets the current site visitor's consent policy regarding allowed cookies and data transfer to 3rd parties, such as for GDPR or CCPA purposes.
You can use the onConsentPolicyChanged()
event to listen for
changes made when a site visitor changes their consent policy with setConsentPolicy()
. Handle
the policy change accordingly in the event handler.
Changes to the consent policy take affect after the page is refreshed.
function setConsentPolicy(policy: Policy): Promise<PolicyDetails>;
An object representing the cookies of the site visitor's consent policy.
import { consentPolicy } from "wix-window-frontend";
// ...
const myPolicy = {
essential: $w("#essentialCheckbox").checked,
analytics: $w("#analyticsCheckbox").checked,
functional: $w("#functionalCheckbox").checked,
advertising: $w("#advertisingCheckbox").checked,
dataToThirdParty: $w("#dataToThirdPartyCheckbox").checked,
};
consentPolicy
.setConsentPolicy(myPolicy)
.then((policyDetails) => {
const newPolicy = policyDetails.policy;
return policyDetails;
})
.catch((error) => {
console.error(error);
});
/* policyDetails value:
* {
* "defaultPolicy" : false,
* "policy" : {
* "essential" : true,
* "functional" : false,
* "analytics" : false,
* "advertising" : false,
* "dataToThirdParty" : false
* },
* "createdDate" : 2020-12-20T12:33:09.775Z
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.