Skip to content

Instantly share code, notes, and snippets.

@tomfuertes
Created June 6, 2016 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomfuertes/7ec0f01cd0b0e45adaad69aefaad519b to your computer and use it in GitHub Desktop.
Save tomfuertes/7ec0f01cd0b0e45adaad69aefaad519b to your computer and use it in GitHub Desktop.
Google Tag Manager and Optimizely
<script>
// Curry Optimizely optimizely.activateUniversalAnalytics() into GTM via window.optimizelyGTM
(function() {
window.gtmData = window.gtmData || [];
window.optimizelyGTM = window.optimizelyGTM || {};
var oldObj = window.GoogleAnalyticsObject;
window.GoogleAnalyticsObject = 'gtmtmp';
window.gtmtmp = function(set, dim, val) {
window.optimizelyGTM[dim] = val;
};
try {
window.optimizely = window.optimizely || [];
window.optimizely.push("activateUniversalAnalytics");
} catch (e) {} finally {
window.GoogleAnalyticsObject = oldObj;
window.gtmtmp = undefined;
delete window.gtmtmp;
window.gtmData.push({event: 'gtm.curriedPageview'});
}
})();
</script>
@tomfuertes
Copy link
Author

tomfuertes commented Jun 6, 2016

  1. Paste this code into a GTM Tag on "all pages"
  2. Change your default GTM UA Pageview tag to fire on the GTM trigger gtm.curriedPageview
  3. Create 3-5 Dimensions in the UA Admin as Session or User Level Variables (e.g., AB Testing - {index} where index is the next number from 1-20 you're using)
  4. Create 3-5 GTM variables as javascript variable -> optimizelyGTM.dimensionN (e.g., optimizelyGTM.dimension4, optimizelyGTM.dimension5, etc...)
  5. You can now track each experiment back to UA using the Options -> Analytics Integrations -> Universal Analytics -> Dimension Index modal in optimizely.

References: https://help.optimizely.com/Integrate_Other_Platforms/Integrating_Optimizely_with_Google_Universal_Analytics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment