Storefront JS API allows you to set cookie consent on behalf of the user or react to its changes depending on the value.
Find a list of available methods below.
Get user's cookie consent
The Ecwid.getTrackingConsent();
receives the customer's answer to the cookie consent banner. Use it to define what cookies your application can collect.
If the cookie consent value is overridden by the JS method or config,
Ecwid.getTrackingConsent()
returns the new value added by JS method/config.
Code example:
Ecwid.getTrackingConsent();
// { userResponse: "ACCEPTED", askConsent: true }
Fields available in the returned object:
Name | Type | Description |
---|---|---|
userResponse | string | Customer's cookie consent. Available values: - "UNDEFINED" : Customer hasn't given their consent yet.- "ACCEPTED" : Collect all types of cookies.- "DECLINED" : Collect only essential cookies.- "ANALYTICS_ONLY" : Collect essential and analytics cookies only.- "PERSONALIZATION_ONLY" : Collect essential and personalization cookies. |
askConsent | boolean | true if store requests customer consent, false otherwise. |
Set user's cookie consent
There are two ways of setting cookie consent value on behalf of the user: through the JS function and the HTML code. We recommend using only one of these methods on your website. If both are used, the HTML config gets higher priority.
The JavaScript method is useful when you add Ecwid to a website that asks for users' cookie consent before Ecwid. With it, you can dynamically set cookie consent for the Ecwid store, when users set it on your website.
The HTML config allows you to disable the cookie consent banner and set one value on behalf of users. For example, you can use it to only allow analytics cookies for all users.
Set cookie consent with JavaScript
Before using this JavaScript method, make sure to enable JS API in your script file.
The Ecwid.setTrackingConsent();
method accepts an argument matching one of the following values:
"UNDEFINED"
: Customer hasn't given their consent yet."ACCEPTED"
: Collect all types of cookies."DECLINED"
: Collect only essential cookies."ANALYTICS_ONLY"
: Collect essential and analytics cookies only."PERSONALIZATION_ONLY"
: Collect essential and personalization cookies.
Code example:
Ecwid.setTrackingConsent("DECLINED");
Ecwid.getTrackingConsent();
//{userResponse: "DECLINED", askConsent: false}
Set cookie consent with HTML
You can override the user's cookie consent with a static value through the Ecwid HTML integration code. You need to add a <script></script>
block right under the integration code with four config lines inside. All config lines are required.
The ec.config.tracking.ask_consent
config must always be true
.
Value of the ec.config.tracking.user_response
defines the consent and must be one of the following:
"UNDEFINED"
: Customer hasn't given their consent yet."ACCEPTED"
: Collect all types of cookies."DECLINED"
: Collect only essential cookies."ANALYTICS_ONLY"
: Collect essential and analytics cookies only."PERSONALIZATION_ONLY"
: Collect essential and personalization cookies.
Code example:
<script>
ec.config = ec.config || {};
ec.config.tracking = ec.config.tracking || {};
ec.config.tracking.ask_consent = true;
ec.config.tracking.user_response = "DECLINED";
</script>
Track changes in consent value
The Ecwid.OnConsentChanged();
method allows you to catch any changes to the consent value.
Code example:
Ecwid.OnConsentChanged.add(function(consent) {
console.log("consent changed to " + consent);
});
Ecwid.setTrackingConsent("DECLINE");
//consent changed to DECLINED
Enable cookie-free mode on the website
You can disable the collection of all non-essential cookies completely by adding a specific config to the Ecwid HTML integration code.
Code example:
<script>
window.ec = window.ec || Object();
window.ec.config = window.ec.config || Object();
window.ec.config.disable_all_cookies = true;
</script>