Delay cookie writing until visitor consent is provided

Overview

Due to upcoming ITP requirements, some customers have asked us, if it is possible to delay the writing of convert cookies until consent is provided by the customer. This without having flashing or blinking, behind the consent modal. 

Solution for blocking all Cookies

The following script needs to be added to your Project Global Javascript section.

Please adapt the script in the last section to enable the cookie writing after your consent javascript event such as the flag variable that will define if the cookies should be written. the disable_cookies variable should be set to false for the script blocking to be enabled. 


if (document.cookie.indexOf('_conv_') == -1 ) {


if(typeof disable_cookies === 'undefined') {
disable_cookies = true;
}

// Store the set and get original functions
var cookie_setter_orig = document.__lookupSetter__("cookie").bind(document);
var cookie_getter_orig = document.__lookupGetter__("cookie").bind(document);

// Redefine the function to only enable cookies when the switch disable_cookies is true.
convert.defineProperty(document, "cookie", {
get: function () {
return cookie_getter_orig();
},
set: function (val) {
// When disable cookies is false then the writing is enabled
if (!disable_cookies) {
console.log('Cookies restablished')
cookie_setter_orig(val);
}
},
configurable: true
});

// Change the click event to any event that you want to enable the cookie writing
convert.$( "#button" ).click(function() {
console.log('Resetting Cookies');

disable_cookies = false;

// Reload Convert Load Sequence (polling)
// So the convert cookies can be written

_conv_q = _conv_q || [];
_conv_q.push(["run","true"]);

});
};

Caveats:

  1. The script will not write cookies, before the consent event happens, however it will still send the experiment visitors and conversions to the Convert servers. 
  2. If the visitor does not consent, then there will not be cookies written, and if there is an experiment behind the modal a chosen variation will be shown to the visitor. However, if he comes back Convert might show a different variation than the original as there were no cookies written to remember this.

Solution for blocking only Convert Cookies:

console.log('Running Convert Cookie Disabling Code');
if (document.cookie.indexOf('_conv_') == -1 ) {


if(typeof disable_cookies === 'undefined') {
disable_cookies = true;
}

// Store the set and get original functions
var cookie_setter_orig = document.__lookupSetter__("cookie").bind(document);
var cookie_getter_orig = document.__lookupGetter__("cookie").bind(document);

// Redefine the function to only enable cookies when the switch disable_cookies is true.
Object.defineProperty(document, "cookie", {
get: function () {
return cookie_getter_orig();
},
set: function (val) {
// When disable cookies is false then the writing is enabled or when its not a Convert cookie
if (!disable_cookies || !val.includes('_conv')) {
cookie_setter_orig(val);
}
else {
console.log('Convert Cookie Blocked');
}
},
configurable: true
});

// Following example code is to disable the cookie blocking

convert.$( document ).ready(function() {

convert.$( "#button" ).click(function() {

console.log('Resetting Cookies');
disable_cookies = false;

// Reload Convert Load Sequence (polling)
// So the convert cookies can be written

window._conv_q = _conv_q || [];
_conv_q.push(["run","true"]);

});
})
Need assistance? Start a conversation with our Support Team.
CONVERT MORE

Reliably Convert More Site Visitors, Even If Everything Else is Changing in Your Business.

START 15-DAY FREE TRIAL
✓ No Credit Card  ✓ Easy Set-Up  ✓ Access All Features

Comments