Created
May 20, 2015 20:11
-
-
Save simondahla/06194eee7393898e626b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 experimentID = optimizely.data.state.activeExperiments[0]; | |
var experimentName = experimentID; | |
if (typeof optimizely.data.experiments[experimentID] !== 'undefined') { | |
experimentName = optimizely.data.experiments[experimentID].name; | |
} | |
var variationName = optimizely.data.state.variationNamesMap[experimentID]; | |
var universalGA; | |
var classicGA; | |
var standardEventHandler; | |
if (typeof ga === "function") { | |
universalGA = true; | |
} | |
if (typeof _gaq !== "undefined" && typeof _gaq.push === "function") { | |
classicGA = true; | |
} | |
if (typeof dataLayer !== "undefined" && typeof dataLayer.push === "function") { | |
standardEventHandler = dataLayer.push; | |
} | |
function sendEvent(action, label) { | |
if (standardEventHandler) { | |
standardEventHandler({ | |
'event': 'ABEvent', | |
'eventCategory': 'AB-test', | |
'eventAction': experimentName, | |
'eventLabel': variationName | |
}); | |
} else { | |
if (universalGA) { | |
ga('send', 'event', 'ABEvent', action, label, 1, { | |
'nonInteraction': true | |
}); | |
} | |
if (classicGA) { | |
_gaq.push(['_trackEvent', 'ABEvent', action, label, 1, true]); | |
} | |
} | |
} | |
sendEvent(experimentName, variationName); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment