setConsentPolicy( )
Sets the current visitor's consent policy regarding allowed cookies and data transfer to 3rd parties, such as for GDPR or CCPA purposes.
Description
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.
Syntax
function setConsentPolicy(policy: Policy): Promise<PolicyDetails>
setConsentPolicy Parameters
NAME
TYPE
DESCRIPTION
An object representing the cookies of the visitor's consent policy.
Returns
Fulfilled - All the details of the visitor's consent policy, including if the the current policy is the default site policy and the policy's allowed cookies. Rejected - Error message.
Return Type:
NAME
TYPE
DESCRIPTION
Whether the policy is the default consent policy set by the site owner. If true
, either the user has not set a policy or the site owner has reset the policy.
An object representing the user's current consent policy.
If a cookie exists in the browser defining the current consent policy, the date the policy was set. Otherwise, undefined.
Was this helpful?
Set a visitor's consent policy
1import { consentPolicy } from 'wix-window-frontend';23// ...45const myPolicy = {6 essential: $w('#essentialCheckbox').checked,7 analytics: $w('#analyticsCheckbox').checked,8 functional: $w('#functionalCheckbox').checked,9 advertising: $w('#advertisingCheckbox').checked,10 dataToThirdParty: $w('#dataToThirdPartyCheckbox').checked11};1213consentPolicy.setConsentPolicy(myPolicy)14 .then((policyDetails) => {15 const newPolicy = policyDetails.policy;16 return policyDetails;17 })18 .catch((error) => {19 console.error(error);20 });2122/* policyDetails value:23 * {24 * "defaultPolicy" : false,25 * "policy" : {26 * "essential" : true,27 * "functional" : false,28 * "analytics" : false,29 * "advertising" : false,30 * "dataToThirdParty" : false31 * },32 * "createdDate" : 2020-12-20T12:33:09.775Z33 * }34 */