Skip to content

Integrating Google Optimize with Google Tag Manager

:: 1 Comment

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"
}
Published inweb analytics

One Comment

  1. Christijan Christijan

    I deployed your script as an HTML tag with GTM and was able to get the data to push to the data layer for experiments when I do a web preview with Optimize, but I haven’t been able to get it to work with live experiments. Any ideas what I might be doing wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.