Rich-text Reply

How can I get a cookie value in Optimizely?

mkstvnsn 10-26-16
Accepted Solution

How can I get a cookie value in Optimizely?

I have the below code which adds a cookie, and then I use an audience to segment the users. But I want to update this code to show the experiment 2x instead of 1x. To do this, I'll have to retrieve the cookie value, but I'm not sure the best way to do this in Optimizely. 

 

window.setCookie = function(c_name,value,exdays,c_domain) {
c_domain = (typeof c_domain === "undefined") ? "" : "domain=" + c_domain + ";";
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays===null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value + ";" + c_domain + "path=/";
};

$(document).delegate('.close-button', 'mousedown', function(event) {
window.setCookie('modal_seen', true, 30);
});
Level 2

MartijnSch 10-27-16
 

Re: How can I get a cookie value in Optimizely?

The best thing to do here is connect this code as well to an example on how to retrieve the value of the cookie itself. So you can use that one to make sure that you can use it in Optimizely. More information on handling cookies in JavaScript can be found here: http://www.w3schools.com/js/js_cookies.asp
robertchan 10-27-16
 

Re: How can I get a cookie value in Optimizely?

Here's a snippet I created which might fit exactly what you're looking for:

 

/*
 * Usage
 * The following allows you to set a limit on the number of times a Code Block will execute for any given visitor.
*/

 // the number of times the code should execute for a given visitor
 var limit = 3;
 // the number of days the evaluation limit should last
 var days = 180;
 // name of the cookie we use as the counter
 var cookieName = 'counterCookie';

 // function to fetch cookie values
 var getCookie = function(name) {
   var match = document.cookie.match(name+'=([^;]*)');
   return match ? match[1] : undefined;
 };

 // function to create cookies
  var setCookie = function(c_name,value,exdays,c_domain) {
    c_domain = (typeof c_domain === "undefined") ? "" : "domain=" + c_domain + ";";
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value + ";" + c_domain + "path=/";
  }

 // logic that counts and limits number of times code can evaluate for given visitor
 if (!getCookie(cookieName)) {
   setCookie(cookieName, 1, days, window.location.hostname);
 } else {
   var numberPops = parseInt(getCookie(cookieName)) + 1;
   setCookie(cookieName, numberPops, days, window.location.hostname);
 }

 if (getCookie(cookieName) < limit) {
  // INSERT code to evaluate HERE
 }
Robert Chan

mkstvnsn 10-31-16
 

Re: How can I get a cookie value in Optimizely?

Robert, this is perfect.. thanks. When I use this, how does Optimizely track conversions? For this example I am showing a popup interstitial and I am tracking that interstitial all the way to user sign up. Will a user who sees the popup be bucketed into this experiment indefinitely? In other words, if they see the popup and dont sign up, but then visit again (and dont see the popup), then they DO sign up. Would Optimizely count that as a conversion? Or does it only count if it's in that browser session?

My audience target is people who do not have the generated cookie. With this code, I would probably have the audience target be users who do not have counterCookie value of 3.
Level 2
robertchan 10-31-16
 

Re: How can I get a cookie value in Optimizely?

Anytime, and glad to be of help. The cookie you set should persist unless the user clears their cookies. The conversion would be dependent on what you define as success. So in your case, I would form some logic that if a user clicks on your sign up button and has the cookie, then count that user as a conversion. Something additional I would consider is clicks on exits from the modal and any interactive elements that may also contribute to conversion along the way. It can't be assumed that just because a user signed up during a cookie session that the sign up wasn't affected by something else (e.g. other CTA on a page, etc.). Hope this helps, and feel free to mark my previous response as a solution to help others.
Robert Chan

mkstvnsn 10-31-16
 

Re: How can I get a cookie value in Optimizely?

Thanks. Just accepted. If I set up a custom event to track sign ups, is it possible if I don't have access to the HTML on the site? Would adding the additional JS required be OK if I inserted via Optimizely's editor?

Also, how difficult would it be to edit your code to only show 1x per browser session, but still show 2x total? (Your code works perfect by the way). I'm trying to avoid a user seeing the popup, and then seeing it again if they go to a different page.
Level 2
robertchan 10-31-16
 

Re: How can I get a cookie value in Optimizely?

Great and thanks. Yes, you can totally set up custom event tracking without access to HTML. This is a good article on that: https://help.optimizely.com/Measure_success%3A_Track_visitor_behaviors/Custom_event_goals_in_Optimiz...

 

And the Optimizely editor allows you to add as much JS as you'd like. Glad the code works, and it shouldn't be that much more difficult. It would be something along the lines of this:

 

window.onload=function(){
(function() {
    var visited = localStorage.getItem('visited');
    if (!visited) {
        document.getElementById("popupp").style.visibility = "visible";
        localStorage.setItem('visited', true);
    }
})();
}

This is from this Stack Overflow question.

Robert Chan