Skip to content

Instantly share code, notes, and snippets.

@msure
Last active May 2, 2018 21:57
Show Gist options
  • Save msure/229fd30d6e8d77fa8937 to your computer and use it in GitHub Desktop.
Save msure/229fd30d6e8d77fa8937 to your computer and use it in GitHub Desktop.
Get Information About Active Optimizely Experiments
// all of this is made possible by the optimizely javascript api, specifically:
// http://developers.optimizely.com/javascript/reference/#the-data-object
function getExperiments() {
var experimentInfo = {};
var wasRedirected = false;
/*
* create an array of experiments that are active on the page
* you must pass two tests to be placed in an active experiment:
* 1) url targeting: https://help.optimizely.com/hc/en-us/articles/200040835-URL-Targeting-Choose-where-your-experiment-runs
* 2) audience https://help.optimizely.com/hc/en-us/articles/200039685-Audiences-Choose-which-visitors-to-include
*/
var activeExps = optimizely.activeExperiments;
// next check if you were placed into redirect experiment
if (window['optimizely'].data.state.redirectExperiment === undefined) {
experimentInfo['redirected'] = 'no redirect experiments found';
}
else {
// if you are in a redirect experiment, push that onto the active experiments array
experimentInfo['redirected'] = 'redirect experiment found';
activeExps.push(window['optimizely'].data.state.redirectExperiment.experimentId);
wasRedirected = true;
}
/*
* at this point, if the active experiments array is length 0 or undefined, there are two possibilities:
* 1) no experiments are running on the page you are looking at
* 2) you were not placed in any experiments (did not pass URL or audience tests)
* https://help.optimizely.com/hc/en-us/articles/200040335-How-Optimizely-Works-Snippet-order-of-execution-JavaScript-evaluation-timing-and-cookies
*/
if (activeExps.length == 0 | activeExps === undefined) {
console.log("can't find any experiments!");
}
else {
// otherwise, something is in the array and we should investigate
activeExps.forEach(function(x) {
// add an experiment from the experimentInfo object
// the experiment ID is used as the key, and the value is the name of the experiment
experimentInfo[x] = {"name": optimizely.data.experiments[x].name};
// next, get all the variation ids for the current experiment
var expVariations = optimizely.data.experiments[x].variation_ids;
expVariations.forEach(function(v) {
/*
* using the name of the variation as the key, create a force variation url as a value
* you can (theoretically) pop this url into an incognito browser and see the variation live
* see more here: https://help.optimizely.com/hc/en-us/articles/200107480-Force-a-specific-variation-to-run-and-other-URL-parameters-
*/
var baseUrl = "";
// there is some logic first to grab the correct URL if you are in a redirect experiment
if (wasRedirected) {
baseUrl = window['optimizely'].data.visitor.referrer;
}
else {
baseUrl = document.location.href;
// remove ad-tracking params if present
if(baseUrl.indexOf('?') > -1) {
baseUrl = baseUrl.split("?")[0]
}
}
// magically, the x param is still available to us even inside the inner forEach call
experimentInfo[x][window['optimizely'].data.variations[v].name] = baseUrl + "?optimizely_x" + x + "=" + v
});
});
// return the object with all the experiment/variation info
return experimentInfo;
}
};
// store active optimizely experiments and variations
var optimizeThis = getExperiments();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment