Skip to content

Instantly share code, notes, and snippets.

@simondahla
Last active November 29, 2019 11:23
Show Gist options
  • Save simondahla/75f4afbdc918a8b0efb0236ca3437dca to your computer and use it in GitHub Desktop.
Save simondahla/75f4afbdc918a8b0efb0236ca3437dca to your computer and use it in GitHub Desktop.
set d to true for console debugging
(function() {
var d = false;
window.optimizely = window.optimizely || [];
var o = window.optimizely;
if (o.activeExperiments && o.allExperiments) {
for (var i = 0; i < o.activeExperiments.length; i++) {
var experimentId = o.data.state.activeExperiments[i];
var experimentName = experimentId;
if (typeof o.data.experiments[experimentId] !== 'undefined') {
experimentName = o.data.experiments[experimentId].name;
}
var variationName = o.data.state.variationNamesMap[experimentId];
if (d) console.info('You are in experiment "' + experimentName + '" and in variation "' + variationName + '".');
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'optimizelyEvent',
'eventCategory': 'Optimizely',
'eventAction': experimentName,
'eventLabel': variationName
});
if (d) console.log('Experiment infomation pushed to dataLayer.');
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment