Rich-text Reply

How can I use a query parameter value to run a test on a page down the funnel?

JohnH 07-24-14
Accepted Solution

How can I use a query parameter value to run a test on a page down the funnel?

I would like to run an experiment for a certain sub-set of people based on the value of a query string parameter. However, I need the experiment to run on pages that exist down the funnel as well. The problem is that the query string parameter is only present on the initial URL, not on the pages that exist down the funnel. What is the best way to set an experiment like this up? 

Level 2

Shaunak 07-25-14
 

Re: How can I use a query parameter value to run a test on a page down the funnel?

Hey John,

 

One of the ways I was thinking you could achieve this would be using cookies. Here is how I would do it:

 

1. Define a function to set up the cookie in your Global JS:

window.createCookie =function(name, value, days){ 
   var expires; 
   if(days){   
   	var date =newDate(); 
    date.setTime(date.getTime()+(days *24*60*60*1000)); 
    expires ="; expires="+ date.toGMTString(); 
    }    
   else{
    expires ="";    
    }    
    document.cookie = name +"="+ value + expires +"; path=/";                  
 }

 

2. Parse the URL in your variation code to check if the required query parameter exists, it can look like:

 

if (window.location.href.indexOf("Test=True")>-1)

 

3. If the condition evalutes to true then create the cookie:

 

window.createCookie("test","true", 2);

 

4. On your page(s) that are down the funnel that you want to include in the variation,set up a new experiment and set the URL Targeting to match the URL, also make sure your additional Targeting Conditions (Audiences) are set to match the cookie you just created in step 3.

 

This should allow Optimizely  to include the pages in the specific variation. Let me know if that works !

 

Best,

Shaunak Mali

Solutions Architect | Optimizely

 

 

 

Optimizely
JohnH 07-25-14
 

Re: How can I use a query parameter value to run a test on a page down the funnel?

This is great, just what I was looking for. Out of curiosity, is there a way to change up the code so that we set a cookie with a dynamic variable? For example, if I want the cookie value to be the value of the utm_campaign parameter? Seems like the code above requires the param name test and the value true always, which may not be the case.
Level 2
Shaunak 07-26-14
 

Re: How can I use a query parameter value to run a test on a page down the funnel?

@John, it should be possible, you can write up a fucntion like the one I have below and pass the value returned to the createCookie function:

 

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");

    var params = {}, tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}

 Hope that helps !

 

Best,

Shaunak Mali

Solutions Architect | Optimizely 

Optimizely

Re: How can I use a query parameter value to run a test on a page down the funnel?

Hi Shaunak,
shouldn't the regexp look more like this?
/[\?\&]{1}([^=]+)=([^&]*)/g;
The one you have matches page urls into the first token, e.g. for this URL https://test.optimizely.com/something.aspx?a=b&c=d&e=f. The resulting token 1 is https://test.optimizely.com/something.aspx?a
Andreas