Skip to content

Instantly share code, notes, and snippets.

@brendanorourke
Last active August 30, 2016 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brendanorourke/d7111cafb92a270a5ef3 to your computer and use it in GitHub Desktop.
Save brendanorourke/d7111cafb92a270a5ef3 to your computer and use it in GitHub Desktop.
Optimizely: Single eVar SC integration
<script type="text/javascript">
/*
* The code below should be executed on every page.
* It will cycle through variationMap, prune only those that are still active,
* and concatenate "{{experiment_id}}:{{variation_index}}" into the eVar #
* you pass as an argument.
**/
function setOptimizelyTestMapToEvar(e) {
if ("undefined" != typeof window["optimizely"]) {
var allTests = window["optimizely"].allExperiments
, charCount = 0
, siteCatalyst = s_gi(s_account)
, variationMap = window["optimizely"].variationMap
, variationMapActive = [] // Leave behind only tests that are still running
;
for (var testId in variationMap) {
if (variationMap.hasOwnProperty(testId)) {
var map = []
, mapString = ""
, testDefined = false
, testEnabled = false
;
testDefined = allTests.hasOwnProperty(testId);
testEnabled = testDefined && allTests[testId].hasOwnProperty("enabled");
if (testEnabled) {
map = [].concat(variationMap[testId]).join("_");
mapString = testId + ":" + map;
if ((charCount + mapString.length) <= 255) {
// Only add if we won't exceed the 255 character cap
charCount += mapString.length;
variationMapActive.push(mapString);
}
}
}
}
siteCatalyst["eVar" + e] = variationMapActive.join();
}
}
/*
* Make the function call, passing in the eVar number as an argument.
* Should be called in the "plugin section" of your SC implementation.
* The plugin section is the same location where we set all other eVars
**/
setOptimizelyTestMapToEvar(15);
</script>
<script type="text/javascript">
/**
* The code below should be executed on every page.
* It will cycle through experiments active on the page and concatenate
* "{{experiment_id}}:{{variation_index}}" into the appropriate SC vars.
*/
function setOptimizelyTestMapToSC(arguments) {
var MVT_DELIMITER = "x";
var PAIR_DELIMITER = "|";
if ("undefined" != typeof window["optimizely"]) {
var activeExperiments = window["optimizely"].activeExperiments
, mapPairs = []
, siteCatalyst = s_gi(s_account)
, variationMap = window["optimizely"].variationMap
;
for (i = 0; i < activeExperiments.length; i++) {
if (variationMap.hasOwnProperty(activeExperiments[i])) {
var experimentId = activeExperiments[i]
, variantMap = [].concat(variationMap[experimentId]).join(MVT_DELIMITER)
, map = experimentId + ":" + variantMap
;
mapPairs.push(map);
}
}
mapPairString = mapPairs.join(PAIR_DELIMITER);
console.log(mapPairString);
if (!!arguments.listId) {
siteCatalyst["list" + arguments.listId] = mapPairString;
}
if (!!arguments.propId) {
siteCatalyst["prop" + arguments.propId] = mapPairString;
}
if (!!arguments.eVarId) {
siteCatalyst["eVar" + arguments.eVarId] = mapPairString;
}
}
}
setOptimizelyTestMapToSC({
listId: 11,
propId: 11
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment