Sets the current visitor's consent policy regarding allowed cookies and data transfer to 3rd parties, such as for GDPR or CCPA purposes.
The setConsentPolicy()
function returns a Promise that resolves to the
consent policy details of the visitor.
You can use the onConsentPolicyChanged()
event to listen for
changes made when a visitor changes their consent policy with setConsentPolicy()
. Handle
the policy change accordingly in the event handler's ConsentPolicyChangedHandler callback function.
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 visitor's consent policy.
import { createClient } from "@wix/sdk";
import { site } from "@wix/site";
import { consentPolicy } from "@wix/site-window";
const wixClient = createClient({
host: site.host(),
modules: { consentPolicy },
});
// ...
const myPolicy = {
essential: document.querySelector("#essentialCheckbox").checked,
analytics: document.querySelector("#analyticsCheckbox").checked,
functional: document.querySelector("#functionalCheckbox").checked,
advertising: document.querySelector("#advertisingCheckbox").checked,
dataToThirdParty: document.querySelector("#dataToThirdPartyCheckbox").checked,
};
(async () => {
try {
const policyDetails =
await wixClient.consentPolicy.setConsentPolicy(myPolicy);
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.