I'm currently using the following code to add "?skin=skin_name" to a url whenever someone lands on a particular page. This allows us to load different themes that we're testing against one another.
So if someone lands on www.example.com this script (running in optimizely) will redirect the visitor to www.example.com?skin=skin_name.
The script also keeps the ?skin=skin_name from being appended if the ?skin=skin_name is already present in the url.
if(window.location.href.indexOf('?skin=skin_name') > -1){
} else {
/* _optimizely_redirect=http://custom */
var url = window.location,redirect_url = 'http://' + url.hostname + url.pathname + '?skin=skin_name' +url.search;
window.location = redirect_url;
}
The problem I am having is when there are already campaign parameters attached to the url, they are being overwritten by the script.
So, if the existing url is www.example.com?utm_source=test&utm_medium=test&utm_campaign=test
the above script is causing the page to load as www.example.com?skin=skin_name?utm_source=test&utm_medium=test&utm_campaign=test which is causing the visitor to be tracked incorrectly in analytics.
What I'm looking for is a script that will produce the following url:
www.example.com?utm_source=test&utm_medium=test&utm_campaign=test**&skin=skin_name**
when there is already "?utm_source=test&utm_medium=test&utm_campaign=test" present, but will produce
www.example.com?skin=skin_name when there is already not a "?" present in the url (so in one case, skin=skin_name is appended using a "&" and in the other case, it is appended using "?" depending on whether a "?" was present in the first place).
Any help is greatly appreciated.