Skip to content

Instantly share code, notes, and snippets.

@seanemmel-ba
Last active December 9, 2015 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanemmel-ba/751cda1f388f9c6d8146 to your computer and use it in GitHub Desktop.
Save seanemmel-ba/751cda1f388f9c6d8146 to your computer and use it in GitHub Desktop.
A bookmarklet that allows you to easily bucket yourself into an Optimizely experiment and variation via the use of query parameters. More info can be found here: https://help.optimizely.com/hc/en-us/articles/200107480-Force-a-specific-variation-to-run-and-other-URL-parameters-
javascript:(function() {
/**
* @package N/A
* @version 1.0
* @author Blue Acorn <code@blueacorn.com>, Sean Emmel <sean.emmel@blueacorn.com>
* @copyright Copyright © 2015 Blue Acorn.
*/
(function() {
var exp;
var v;
var url = window.location.href;
if ( !sessionStorage.exp ) {
exp = prompt("Experiment ID?");
if (exp === null || exp === false || exp === undefined) {
return false;
} else {
exp = Number(exp);
sessionStorage.exp = exp;
}
}
v = prompt("Variation?");
if (v === null || v === false || v === undefined) {
return false;
} else {
v = Number(v);
}
if (url.indexOf('?optimizely_x') != -1) {
window.location.href = url.split('?optimizely_x')[0] + "?optimizely_x" + sessionStorage.exp + "=" + v; /* replace old Optimizely params with new ones */
} else if (url.indexOf('&optimizely_x') != -1) {
window.location.href = url.split('&optimizely_x')[0] + "&optimizely_x" + sessionStorage.exp + "=" + v; /* replace old Optimizely params with new ones */
} else if ( url.indexOf('?optimizely_x') === -1 || url.indexOf('&optimizely_x') === -1 ) {
if (url.indexOf('?') != -1) {
if (url.split('?')[1].length > 0) {
window.location.href = url + "&optimizely_x" + sessionStorage.exp + "=" + v; /* other query params present, so append as an & param */
}
else {
window.location.href = url + "optimizely_x" + sessionStorage.exp + "=" + v; /* ? present but no params; append params to url */
}
} else {
window.location.href = url + "?optimizely_x" + sessionStorage.exp + "=" + v; /* no query params; append new Optimizely params to url */
}
}
})();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment