1. Help Center
  2. Target Visitors

Test for cookie values on the Locations

THIS ARTICLE WILL HELP YOU:

Example Use Case

Sometimes we may need to check for a specific condition to occur on every page visit, but that condition might be related to the options offered in the Locations.

A common need is to verify a cookie value for a user to be bucketed into the test, but if this cookie value changes the experience should no longer be shown to the visitor. For example, we might want to show a coupon only one time; or, the opposite.

What we would need is a JavaScript Condition that checks for the cookie value and the URL value at the same time.

Code

Place the following code in the Locations as a JavaScript condition. Make sure you modify the code with the correct values to check the cookie and the cookie name as specified in the cookie comments.

(function() {

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

var value_to_check_for = 'cookie value'; // Fill here what value should the cookie contain.
var coupon = readCookie('cookie_name'); // Fill the Cookie value
if(coupon==null) {
return false;}
else return ((window.location.href=="https://www.convert.com/") && (coupon.includes(value_to_check_for)));
// Feel above the URL of where the experience should run
})()