Managing user's cookie consent

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:

NameTypeDescription
userResponsestringCustomer'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.
askConsentbooleantrue 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 API method and through the HTML code:

  • The Ecwid.setTrackingConsent(); JS method best suits for changing consent dynamically depending on some events/actions. For example, when your website asks for customers' cookie consent before Ecwid.
  • The ec.config.tracking.user_response config is useful if you want to define a static cookie consent value for all users without scripts.

📘

Cookie consent added through the integration code (ec.config.tracking.user_response = "DECLINED") overrides values passed through the Ecwid.setTrackingConsent() method.

We recommend using only one of these methods.

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

The configuration string is added to the Ecwid HTML integration code. Its value must be one of the:

  • "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>