Firing tags in GTM (Google Tag Manager) containers

2019-07-21 10:10发布

问题:

Has anyone figured out a way to get a Google Tag Manager container to fire all its tags under Turbolinks?

I'm running a rails 4.0 application (with Turbolinks) and have put the code below in a footer that I render in every page (inside the tags):

<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXX');</script> 
<!-- End Google Tag Manager -->

(where XXXXX is my GTM container ID)

Unfortunately it does not fire a single tag when I'm browsing between pages (I assume because of Turbolinks). However, everything works fine if I refresh the page I'm in.

I've seen people posting questions about workarounds for Google Analytics tags that seemed to tackle that issue, but never for a full GTM container. Help? Anybody?

回答1:

You have to cause virtual page views to fire when using GTM with Turbolinks: Googletagmanager with Turbolinks

Also, you want to load the GTM script immediately after the <body> open tag.