Integrating Google Optimize with Google Tag Manager
You may need to know if the current page has any Google Optimize Experiment running, to track that info on any tool, or to fire some vendor tag based on the current experiments statuses.
The following snippet will take care of sending a dataLayer push if there's any active experiment running, including:
- The Experiment ID
- The Optimize Container ID where the experiment is running on
- The current experiment variation being shown to the current user/device
(function() {
for (var gtm in window.google_tag_manager) {
if (gtm.match(/^GTM/)) {
if (google_tag_manager[gtm].experiment) {
dataLayer.push({
'event': 'optimize-experiment-active',
'optimize-container-id': gtm,
'optimize-exp-name': google_tag_manager[gtm].experiment.split("$")[0],
'optimize-exp-variation': 'Variation: ' + google_tag_manager[gtm].experiment.split("$")[1]
});
}
}
}
})();
Then just create the needed dataLayer type variables to read the pushed data and use it on your tags/triggers.
The dataLayer push will look like:
{
event: "optimize-experiment-active",
optimize-container-id: "GTM-XXXXXX",
optimize-exp-name: "GTM-XXXXXX_OPT-YYYYY",
optimize-exp-variation: "Variation: 1"
}