Rich-text Reply

How to create, use and delete a cookie for my specific experiment?

Nicolas_Hemidy 05-26-16
Accepted Solution

How to create, use and delete a cookie for my specific experiment?

Hi everyone,

 

I have an issue regarding one of my experiment and I don't have any developers available so I hope I could find some help here. 

I want to run an experiment which is a multipages experiment targeting different landing pages and then the same pages: basically we have A1 or A2 or A3 or... >> B >> C.
We have some links on the following pages (B,C..) that drive the users back to the landing page. We want to dynamically change this link depending on the landing page the users arrived on. If I landed on A2, the homepage links on B and C should drive him back on A2.
For doing that, I need to create a cookie and use this cookie with the following codes I found on the documentation. I'm not a developer and was just wondering if someone could tell me how to adapt these lines of code to my situation.

 

CREATE THE COOKIE >> On the landing page editor code

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=/";
}

 

>> Should I need to change name and value in the argument of the function?
>> Where do I store the url? Can I use window.location.href to grab it?

 

USE THE COOKIE >> On the following pages editor code

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

 

>> How can I access the url I stored?
>> Can I use $('a[href="/"]').attr(...);?

 

>> Last question, is there a way to kill the cookie on a specific action? 

 

Thanks a lot in advance!!

Nicolas

AllisonR 05-27-16
 

Re: How to create, use and delete a cookie for my specific experiment?

Hi Nicolas, I'm happy to clarify the code sample. 

 

Set the cookie: 

 

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=/";
}

 

This part of your code should be left as is. This is defining a function that can be used later on in your code to set a cookie. When you want to set a cookie, you can call the function like so (where c_name is the name of the cookie,  value is the value of the cookie, exdays is the number of days the cookie should last, and c_domain is the domain on which this cookie should be set and can be read):

 

setCookie(c_name,value,exdays,c_domain);

 

Read the cookie: 

 

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

  

This part of your code should also be left as is. This is defining a function that can be used later on in your code to read a cookie. When you want to read the value of a cookie, you can call the function like so: 

 

getCookie(cookieName);

 

One way to 'delete' a cookie is to change its expiration date to a date in the past. The StackOverflow post here outlines a good example of how to do this. 

 

Hope this helps!

 

Best,

Allison
Sr. Technical Support Engineer
Optimizely | EMEA

 

Allison
Sr. Technical Support Engineer
Optimizely | EMEA
Optimizely

Re: How to create, use and delete a cookie for my specific experiment?

Thanks a lot for your help Allison! It's really helpful.
I really better understood how I have to use the function and how to call them.
My last question is how can I store an url into the cookie and then use this url later to change an link on my page? Should I set the value of the function as the url when I call it?
Then, if I just need to use the getCookie function to retrieve the url and use it somewhere else?
Thanks a lot in advance,
Nicolas
beckyhall 06-07-16
 

Re: How to create, use and delete a cookie for my specific experiment?

Hi Nicolas,

 

To store the url to the cookie you would just call setCookie and pass in the url as the value of the cookie.
    setCookie('MyAwesomeCookie','url/you/want/to/pass/goes/here');

Then when you need to use the URL you can just call getCookie('MyAwesomeCookie');

 

So in your case, on all of the 'A' pages you will need to run setCookie with the name of your choice and the current URL (see window.location.href or window.location.pathname). Then on the 'B' and 'C' pages, set the href of your links dynamically with JavaScript. So if your link looks something like this:
    <a href=# id="really-cool-link">Go back to page A</a>

Your JavaScript would look something like this:
    document.getElementById('really-cool-link').href = getCookie('MyAwesomeCookie');

-Becky