Rich-text Reply
Highlighted

Redirect experiment with regular expresions

drapado 06-13-19

Redirect experiment with regular expresions

I want to create a redirect experiment for dynamic urls.
Can I use regular expressions to define the pattern?

Example:
Original URL: /product/skirt-x-y-1234
Redirect URL: /exp/product/skirt-x-y-1234

 

I need to apply this experiment to all product pages.

 

Thanks,

Dayris

Level 1

CouchPsycho 06-13-19
 

Re: Redirect experiment with regular expresions

Yes, you can do this, if you create your variation via the visual editor. You first set up the correct targeting rule and by applying a redirect change you can switch from url to code-mode. Query the current location, apply the modification and set the new location. That is it .-)
Kind regards

Michael


   
joshcoles 06-13-19
 

Re: Redirect experiment with regular expresions

[ Edited ]

Just following up on CouchPsycho's answer with some code, as I'm not sure if you are a developer. It could look something like this:

var path = document.location.pathname;

if (path.indexOf('/product/') > -1) {
  path = '/exp' + path;
}

document.location.href = document.location.origin + path;




Josh,
Front End Developer at Widerfunnel
JasonDahlin 06-13-19
 

Re: Redirect experiment with regular expresions

hi @joshcoles,

 

I've found you'll want to be more explicit in determining if a redirect is needed. How diligent you need to be depends of course on how well-patterned your paths are.

 

Also, existing query string parameters should probably be maintained.

 

Here is an example of what I developed for us:

(function(){
	var thisPath = window.location.pathname;
	var thisPathArr = thisPath.split('/');
	var thisQS = window.location.search;
	var newPath = "";

	if (newPath === "" && thisPathArr[3] == "/old/") {
		newPath = thisPath.replace('/old/','/new/');
	}

	//repeat above for any other redirect patterns that may exist


	if (newPath !== "" && newPath != thisPath) {
		window.location.replace(newPath + thisQS);
	}
})();

 

For @drapado purposes,

"old" could be "product", and "new" could be "exp/product"

--Jason Dahlin
Analytics and Testing Guru Smiley Happy


Experimentation Hero
joshcoles 06-13-19
 

Re: Redirect experiment with regular expresions

[ Edited ]

Great point Jason! You're right, query strings and other factors need to be accounted for, my code was just a basic example. I hope that @drapado tests whatever combination of these solutions works best for him on his own project regardless.

Josh,
Front End Developer at Widerfunnel