Clara B
Clara B

Reputation: 461

Solving configuration mismatch between Google Analytics and Google Optimize?

So I'm using Google Analytics, Google Optimize, and Google Tag Manager. I also feel that I may be losing my mind.

For performance reasons, we don't want to add the google optimize script through GTM. We want to hard-code it. But every time I try that, the installation doesn't work. Specifically, when I click "Run Diagnostics" on the google optimize setup page, I get an error that says

All Analytics tracking code on the page must have the same tracker configuration settings. [...] The following tracker configuration properties do not have the same configurations: alwaysSendReferrer.

So I go into my handy Analytics debugger, find the alwaysSendReferrer property, and note that it's set to true. Great. I edit the config object that i'm passing to the google optimize init script, and I add alwaysSendReferrer: true. So far so good. Except... I get the same error.

I thought maybe something was wrong with our analytics setup, so I got rid of the hardcoded google optimize script and instead included it via the Google Optimize GTM tag -- which, unexpectedly, worked perfectly. I thought "Okay i'll just find the script that GTM is adding and copy/paste it into the codebase" but of course it's minified within an inch of its life, and untangling that javascript from the rest of the tags would be a whole other endeavor.

I have access to a {{Default UA Settings}} variable in GTM, and google's documentation would really like me to just pass that object to the official "Google Optimize" tag in GTM. That works (I've tested it), but as mentioned, we don't want to do that.

If anyone knows what the Google Optimize config object should look like, that would help me massively.

Right now mine looks like this (with identifying details removed)

dataLayer.push(['config', 'UA-12345', {
    'optimize_id': 'GTM-55555',
    'linker': { 'domains': ['mywebsite.com <http://mywebsite.com>'] },
    'alwaysSendReferrer': true
});

I've tried false, "true", 1, and basically everything else I can think of (I'm quite confident that Google Analytics has this property set to true on our site) -- my only guess is that maybe I'm setting the value in the wrong place and google optimize isn't seeing it. Help?

Upvotes: 1

Views: 1767

Answers (1)

Dagmar
Dagmar

Reputation: 3261

I know the feeling of losing your mind trying to read through Google documentation!

This is the code I use on pages I wish to run Optimize experiments on:

<head>
...
  <!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-PRMQXXX');</script> <!-- End Google Tag Manager -->

  <!-- Page-hiding snippet (recommended)  -->
  <style>.async-hide { opacity: 0 !important} </style>
  <script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
  h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
  (a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
  })(window,document.documentElement,'async-hide','dataLayer',4000,
  {'GTM-KL7XXXX':true});</script>

  <!-- Modified Analytics tracking code with Optimize plugin -->
  <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','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-99999999-1', 'auto');
  ga('require', 'GTM-KL7XXXX');
  ga('send', 'pageview');
  </script>
...
</head>

Where:

  • GTM-PRMQXXX is the Google Tag Manager container id
  • GTM-KL7XXXX is the Google Optimize container id, and
  • UA-99999999-1 is my Google Analytics id

Note that you need to remove your usual GA script once you add the scripts above.

I hope this helps you!

Upvotes: 1

Related Questions