Will the module block cookies added by Javascipt?

Will the module block cookies added by Javascipt?

The Cookie Consent extension can block cookies added through PHP/JS scripts; this improvement was added in ver. 2.6.0 of the Cookie Consent extension. You may refer to the steps below to implement the compatibility.

Step 1. Create a 3rd-party cookie on the Cookies grid (similar to cookies added by the extension).
Step 2. Assign a cookie to the required cookie group.
Step 3. Change particular files as described below.

For example, you have the JS component or simple script that adds some cookie on your frontend:

define([
    'jquery',
    'uiCollection',
        // Some other dependencies
], function ($) {
    'use strict';

    return Collection.extend({
        initialize: function () {
            this._super();
            this.yourCustomMethodToAddCookie();
            return this;
        },
        /**
		 * Method to initialize custom cookie
     	 */
        yourCustomMethodToAddCookie: function () {
        	$.mage.cookies.set('my-awesome-cookie', 'cookie-data-value');
        }
    });
});

With our module, you can make this cookie GDPR-compliant. Use one of the approaches below (or even both if you wish).

Synchronous approach

Pros: your custom JS code will be executed on demand without any delays.

Cons: it's impossible to check essential cookies. Thus, you should avoid using this approach when you have different essential cookies on your stores.

Let's modify the component to make it GDRP-compilant via Synchronous approach:

define([
    'jquery',
    'uiCollection'
], function ($) {
    'use strict';

    return Collection.extend({
        initialize: function () {
            this._super();
            this.yourCustomMethodToAddCookie();
            return this;
        },
        /**
		 * Method to initialize custom cookie
     	 */
        yourCustomMethodToAddCookie: function () {
        	var disallowedCookies = document.cookie.match(new RegExp('(^| )amcookie_disallowed=([^;]+)')) || [];
        	var allowedCookies = document.cookie.match(new RegExp('(^| )amcookie_allowed=([^;]+)')) || [];
        	var isAllowedToRunScript = !!allowedCookies.length && (!disallowedCookies[2] || disallowedCookies[2].split('%2C').indexOf('my-awesome-cookie') === -1)
        	if (isAllowedToRunScript) {
        	    $.mage.cookies.set('my-awesome-cookie', 'cookie-data-value');
        	}
        }
    });
});


Asynchronous approach

Pros: all GDRP Cookie JS logic and your custom JS logic is fully compliant; you will have full control over cookies added via custom JS code and run a custom code after cookie acceptance.

Cons: your JS code will be executed after some GDRP Cookie JS logic with delay.

Let's modify the component to make it compliant via Asynchronous approach:

define([
    'jquery',
    'uiCollection'
], function ($, Collection) {
    'use strict';

    return Collection.extend({
        initialize: function () {
            this._super();
            this.cookieCompilantMethodWrapper(this.yourCustomMethodToAddCookie.bind(this)); // Wrap your method with error resistant wrapper
            this.initCookieEventListeners(); // Add event listener to run custom code immediately after the client accepts the cookies.
            return this;
        },
        /**
		 * Cookie Allow and Save actions event listener
     	 */
        initCookieEventListeners: function () {
        	$('body').on('amcookie_save amcookie_allow', function () {
                this.cookieCompilantMethodWrapper(this.yourCustomMethodToAddCookie.bind(this));
            }.bind(this));
        },
        /**
		 * Cookie compilant wrapper for your method
     	 */
        cookieCompilantMethodWrapper: function (cookieSetterCallback) {
        	require(['Amasty_GdprCookie/js/model/cookie'], function (cookieModel) {
        	    if (cookieModel.isCookieAllowed('my-awesome-cookie')) {
        	        cookieSetterCallback();
        	    }
        	}, function(errorMessage) {
`              cookieSetterCallback(); // In case if not existing of Amasty component we MUST run the callback
        	});
        },
        /**
		 * Method to initialize custom cookie
     	 */
        yourCustomMethodToAddCookie: function () {
        	$.mage.cookies.set('my-awesome-cookie', 'cookie-data-value');
        }
    });
});

      • Related Articles

      • Will the module work with GTM?

        Out of the box, the module is tested and guaranteed compatible with the following ways of GTM implementation: 1) Magento's native Google Analytics functionality (Stores → Configuration → Sales → Google API → Google Analytics). 2) Amasty Google ...
      • Can I add Google Analytics cookies to the essential group?

        Essential cookies are a site's basic form of memory, used to store the settings selected by a user on a given site. As the name implies, they are essential to a website's functionality and cannot be disabled by users. On that note, neither Marketing ...
      • How exactly do cookies work in the extension?

        Default Magento cookies are divided into 2 types - Essential and Non-Essential. Essential cookies do not require the user's consent for their work and are stored in the browser automatically, as they ensure the correct operation of the store from the ...
      • Where can I find the Consent Mode settings?

        Starting from the 2.0.0 version of the Google Analytics 4 with GTM Support module, we separated the compatibility functionality with Consent Mode V2 in a distinct package, and it is no longer appears as a part of the module's functionality by ...
      • Is it possible to set no cookies at all before any decision is taken?

        The Cookie Consent module allows blocking Magento marketing cookies and any third-party cookies that are present on the website. They will not be set until a customer allows them, and will be blocked if a customer chooses to. However, whether a ...