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');
}
});
});