Rich-text Reply

Appending query to URL

Flignats 06-03-15

Appending query to URL

Hi,

 

I'm having trouble targeting all URLs on my website and appending them with a query string. Using the below resources:

 

I was using the community support and example codes here: http://developers.optimizely.com/samples/#redirect---add-query-parameter-to-urlh

 

I've been unable to setup a proper experiment that targets all my site visitors. I have 3 variations setup and would like to target all pages on my website. When a user lands on a page each variation should redirect the user to the current page they are viewing, but with an added query to the URL.

 

When I set it up, I am getting an infinite redirect loop. I used the custom sample code and kept the redirect URL to the example url with the query box unchecked.

 

This is the code I used:

 

 

/*
* Usage
* This JavaScript will add a query parameter you specify to the visitor's current URL.
*/

/* _optimizely_redirect = custom_add_query_param */
var newQuery = "src=12SHPEDWB"; // Replace [PARAM_NAME] and [PARAM_VALUE]
var _optly = {redir: document.createElement("a")};
_optly.redir = {protocol: "https:" == document.location.protocol ? "https://" : "http://",
domain: window.location.hostname,
pathname: window.location.pathname
};
_optly.cur = window.location.search;
if (_optly.cur) {
_optly.redir.query = _optly.cur + "&" + newQuery;
} else {
_optly.redir.query = "?" + newQuery;
}
_optly.redir.href = _optly.redir.protocol + _optly.redir.domain + _optly.redir.pathname + _optly.redir.query;
window.location.replace(_optly.redir.href);

 

 

Does anyone have suggestions on this experiment setup?

 

Level 1

MartijnSch 06-04-15
 

Re: Appending query to URL

I don't have the exact code that you could use but I think what could go wrong is that you're not checking on the parameter at the first check. That's why you'll always be redirected as every new page will start loading the experiment code again. By checking the parameter you could just stop the process if it's found in the URL. Makes sense?
cubelodyte 04-15-16
 

Re: Appending query to URL

[ Edited ]

@Flignats
If you look at the second line of instructions for this: http://developers.optimizely.com/javascript/code-samples/index.html#helpers-add-query-param

It says, "Please exclude the query parameter as an Audience condition to prevent it from getting added multiple times."

It sounds like you can solve your problem with a simple Audience assignment.

 

Updated: 

I just tried this with the Audience. It worked for my experiment. 

paramTrue.png

Scott Ehly
Manager of Site Optimization
sehly@rentpath.com

'The single biggest problem with communication is the illusion that it has taken place.' - George Bernard Shaw
cubelodyte 04-18-16
 

Re: Appending query to URL

Update:
Using that query string redirect code has presented an interesting issue in our QA.

We find that refreshing the page will cause the query string to append to itself resulting in a potentially endless series of repeated query strings:

 

www.example.com?param=true&param=true&param=true...

 

This is likely to be harmless, but if you want to prevent it, you can wrap the main block of code in an IF that checks to see if it's already there:

/* _optimizely_redirect=http://custom_add_query_param */
var newQuery = "param=true";
if (window.location.search.indexOf(newQuery) === -1) {
    var _optly = {redir: document.createElement("a")};
    _optly.redir = {protocol: "https:" == document.location.protocol ? "https://" : "http://",
                    domain: window.location.hostname,
                    pathname: window.location.pathname
                   };
    _optly.cur = window.location.search;
    if (_optly.cur) {
      _optly.redir.query = _optly.cur + "&" + newQuery;
    } else {
      _optly.redir.query = "?" + newQuery;
    }
    _optly.redir.href = _optly.redir.protocol + _optly.redir.domain + _optly.redir.pathname + _optly.redir.query;
    window.location.replace(_optly.redir.href);
}

 

Scott Ehly
Manager of Site Optimization
sehly@rentpath.com

'The single biggest problem with communication is the illusion that it has taken place.' - George Bernard Shaw