Forked from simondahla/optimizely-gtm-custom-integration.js
Created
August 26, 2018 03:18
-
-
Save denji/27ab66cc0587b6f231a40c388addaa1e to your computer and use it in GitHub Desktop.
set d to true for console debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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