Google Tag Manager restart after AJaX page-reload

Quick Tip

So you have this very fast AJaX page loader (InstantClick) on your website, but you want to use Google Tag Manager and track your pageview with Analytics etc; here’s how you reset Tag Manager and re-load it after your AJaX page is loaded;

InstantClick.on('change', function () {
jQuery('link[rel="canonical"]').attr('href', location.href);
ga = gaData = gaGlobal = gaplugins = google_tag_manager = dataLayer = undefined;
(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-ABCDEF78');
});

Author: Remon Pel

WebDeveloper though not WebDesigner

Leave a Reply

Your email address will not be published. Required fields are marked *