Rich-text Reply

Change URL Param Value in Variation

jmerrill598 01-21-16

Change URL Param Value in Variation

I need Optimizely to change the value of a URL parameter on the variation.  For example:

 

Here is the original URL: 
http://domain.com/page_name/?utm_campaign=my_campaign

 

When the variation is shown to the user, in addition to the visual changes I've made in the editor, I also need Optimizely to change the value for the URL parameter "utm_campaign" to something different so that I can track it independently in our system:
http://domain.com/page_name/?utm_campaign=my_alt_campaign

 

Thanks in advance.

 

James

JDahlinANF 01-21-16
 

Re: Change URL Param Value in Variation

[ Edited ]

Use this as the code for Variation B.  This will redirect the user back to the same URL but with a different value for utm_campaign.

 

 

 

/* _optimizely_redirect=http://custom */
/* _optimizely_evaluate=force */
	var newURL = currentURL = window.location.toString();
	newURL = currentURL.replace('utm_campaign=111111','utm_campaign=222222');

	var _optly={redir:document.createElement("a")};
	_optly.redir.href=currentURL;

	if (_optly.redir.href !== '') {
		window.location.replace(_optly.redir.href);
	}
/* _optimizely_evaluate=safe */

 

 

Note: Optimizely's code already prevents multiple redirects (endless loops), but, since I never trust such things to actually work (though I have never seen this one fail), you may want to limit the redirect to only happen to users who have the old value which is being replaced by the modified value, like this:

 

/* _optimizely_redirect=http://custom */
/* _optimizely_evaluate=force */
	var newURL = currentURL = window.location.toString();
	if (newURL.indexOf('utm_campaign=111111') > -1) {
		newURL = currentURL.replace('utm_campaign=111111','utm_campaign=222222');

		var _optly={redir:document.createElement("a")};
		_optly.redir.href=currentURL;

		if (_optly.redir.href !== '') {
			window.location.replace(_optly.redir.href);
		}
	}
/* _optimizely_evaluate=safe */

That way you can limit your URL Targeting to only URLs that contain utm_campaign=111111 or utm_campaign=222222.

 

jmerrill598 01-21-16
 

Re: Change URL Param Value in Variation

Thanks for the code snippets.  I went with your second block which does the check before redirecting however it didnt redirect as expected.  I noticed that a warning is raised about leaking a variable...I did some research on the message and discovered it can happen when you attempt to make more than one inline assignment, possible that has something to do with why it isn't redirecting?

 

screencap.png