AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Optimize
3.2K members online now
3.2K members online now
Everything you need to know about setting up Optimize including creating your account and container, linking to Google Analytics and how to tag your site.
Guide Me
star_border
Reply

Optimize + GTM

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

My understanding of the contents of this article is that, to implement Optimize alongside GTM, the web page source code would look something like this (assuming the GTM container code is placed at the bottom of the page):

 

<html>
<head>
<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-12345-1', 'auto');
  ga('require', 'GTM-XXXXX');
</script>
</head>
<body>

<!-- Rest of source code here >

<!-- 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-YYYYY');</script>
<!-- End Google Tag Manager -->
</body>
</html>

My question is:

 

Considering the GTM library creates a GA tracker object that follows the naming convention "gtm<random_number>", will there not be a conflict/mismatch between the tracker created by the optimize code and the tracker created by the GTM library? Is it necessary for both trackers to have the same name in order for things to work properly?

Optimize + GTM

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭

Same question here. I adjusted everything besides the dynamic tracker name to match the create of the GTM GA. Would be nice if someone could share some insights if this can cause any issues.

Optimize + GTM

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Update:

 

We implemented the optimize tag in the way discussed in the support article and something really strange happened. So before implementing, our code was as follows:
 
<html>
<head></head>
<body>
<!-- source code here -->
<GTM container code here>
</body>
</html>
 
and the dataLayer (on the transaction confirmation page) looked like this:
  1. enhanced ecommerce transaction data
  2. gtm.js
  3. gtm.dom
  4. gtm.load
Then we implemented optimize as follows:
 
<html>
<head>
<!-- Optimize code without ga(send) command to avoid duplicate page views -->
</head>
<body>
<!-- source code here -->
<!-- GTM container code here -->
 
The weird thing that happened is that after we did this, the dataLayer looked like this:
  1. gtm.js
  2. enhanced ecommerce transaction data
  3. gtm.js
  4. gtm.dom
  5. gtm.load
As you can see, there are now two gtm.js events being pushed onto the dataLayer. What's even more strange is that the GA page view tracking tag which is configured in GTM and has a trigger of "All Pages" (i.e. event equals gtm.js) only fired once (on the first gtm.js event on the dataLayer). This resulted in the enhanced ecommerce transactions to be dropped and no longer tracked in GA.
 
My theory is that the optimize code dynamically modified the HTML code and moved the GTM container code from the bottom of the page to the top of the page. Furthermore, the optimize code pushed its own "gtm.js" event onto the dataLayer resulting in duplicate gtm.js events and tag misfiring.
 
Has anyone seen this before?

Optimize + GTM

[ Edited ]
Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

Did you use your GTM or Optimize Container ID in the Optimize Snippet? AFAIK it should be the Optimize Container there.

Also make sure that your dataLayer with the ecommerce data etc. is still BEFORE the GTM Container snippet after all your changes.

A good guide for that part of the implementation can be found here: http://www.lunametrics.com/blog/2017/03/02/install-google-optimize/

Optimize + GTM

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

We used the Optimize Container ID. We copied/pasted the Optimize code directly from the Optimize UI and just removed the:

 

ga(send, pageview)

 

line from the code.

 

That's the weird thing, before we implemented Optimize, the enhanced ecommerce data was indeed being pushed onto the dataLayer BEFORE the GTM container code. But after we implemented Optimize, we got this double-pushing of the gtm.js event happening and that caused the enhanced ecommerce data to stop being picked up by GTM because the GA page view tracking tag was only firing on the FIRST gtm.js event.

Optimize + GTM

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭

Did you ever find a solution to this? I experienced a similar issue.

Optimize + GTM

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭

For now what we are doing is changing the condition of the GA tag in GTM so that it fires on gtm.dom instead of gtm.js. Since only one gtm.dom event is pushed onto the dataLayer, this should fix the problem. Not ideal, but it's a fix.