Use gtag.js to install Google Tag Manager

Viewed 1678

The Global Site Tag or gtag.js was recently introduced as the recommended script format for installing Google Analytics (GA). But I have found that you can also use it to install Google Tag Manager (GTM).

http://jesscarranza.blogspot.com/2017/11/use-gtagjs-to-install-google-tag-manager.html

Can any JavaScript expert validate that the two scripts below are functionally identical?

GTM using gtag.js
    <script async src="https://www.googletagmanager.com/gtag/js?id=GTM-XXXX"></script>
    <script>window.dataLayer=window.dataLayer||[];
    function gtag(){dataLayer.push(arguments);}
    gtag('js',new Date());</script>

GTM official code:
    <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-XXXX');</script>
1 Answers

AFAIK (and I'm not a "Javascript expert"), the new gtag is like a wrapper for "Google’s site measurement, conversion tracking, and remarketing products" that will enable you to do exactly the same thing as the gtm.js library, so from an end user pov, there is no difference in what you get out of them at the end of the day. The real benefit of the gtag.js library is when you need to also include other analytics based libraries, like AdWords and Firebase or other future integrations.

Related