Kemen Paulos Plaza Kemen Paulos Plaza - 17 days ago 12
Ajax Question

GTM as Singleton (Google Tag Manager)

Hellow

There is any way to execute GTM as a Singleton? , i need only a single execution of GTM.

To add some more context, my aplication is a single page aplications (without uri change), and the content is loaded via Ajax, but the entire HTML changes with every interaction. When the content changes the entire code is reloaded, reexecuting the GTM code getting 2 instances of GTM.

Any ideas to fix the problem?

Answer

At the end i used a javascript variable and check it before execute the gtm main snippet, just a basic workaround

if(typeof gtm_on == "undefined"){
<!-- Google Tag Manager -->
(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-582XFH')
}

Comments