Rich-text Reply

Enable Google Universal Analytics Integration using Google Tag Manager

joehallissey 08-13-14

Enable Google Universal Analytics Integration using Google Tag Manager

Hi Everyone,

 

I'm trying to setup Optimizely's Google Universal Analytics integration but I'm not clear on how to add the necessary code to my GA snippet in Google Tag Manager. In the Optimizely support doc, it says to do the following:

 

  1. Add the following JavaScript code to your site wherever the Universal Analytics code exists after the ga('create'...) function fires and before the Universal Analytics ga('send','pageview') function fires and the tracking call is made (see details in the next section):

  2.  

     // Optimizely Universal Analytics Integration
     window.optimizely = window.optimizely || [];
     window.optimizely.push("activateUniversalAnalytics");

 

I'm not clear on where in Google Tag Manager this code should be placed within my Universal Analytics tag. 

 

Any ideas would be appreciated!

 

- Joe

Re: Enable Google Universal Analytics Integration using Google Tag Manager

[ Edited ]

Hi @joehallissey,

 

The assumption behind our best practice for integrating with tag managers is that the tag manager allows for a synchronous implementation of Optimizely and the analytics platform.  The required order for successful integration is the Optimizely snippet, then the UA snippet, then the UA tracking call.  Traditionally, GTM cannot guarantee that order, so we suggest an alternate integration method, outlined below.

 

  • The integration between Optimizely and Google Analytics uses the setCustomVar method
  • However, as we read there, using _gaq to push a custom variable won’t work with Google Tag Manager as they are using a randomly generated tracker name (actually, a number). Their intention is to not conflict with any existing Google Analytics code already on the page. In our case, however, this is exactly what we want (because we want our information to be picked up).
  • Workarounds you can use to get the integration working with GTM:
    • In Google Tag Manager, go to the Google Analytics tag, select the Settings, then go to ‘Advanced Configuration’. Tick the ‘Tracker Name’ checkbox and make sure the value is empty:
    • ga.png
    • Instead of using the Google Analytics Tag in GTM, use Custom HTML tag.
      • For UA: Make sure Optimizely is added synchronously in the head of the document, outside and before the GTM script call. Copy the UA snippet, add the Optimizely activation call and after that the pageview tracking call, like so:

      • Example code:
    • <script>
       
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
       
      ga('create', 'UA-XXXX-Y', 'auto');
       
      window.optimizely = window.optimizely || [];
      window.optimizely.push("activateUniversalAnalytics");
       
      ga('send', 'pageview');
       
      </script>

 

Once you've followed these steps, you should begin to see Optimizely experiment and variation ID information showing up as attributes in your UA reports.  If you continue to have issues, you are welcome to submit a case to our Support team, and we will be happy to dive more deeply into your specific implementation.

Harrison Krat
Solutions Architect | Optimizely, Inc.
harrison@optimizely.com