What is gtag.js and what is it used for?

Global Site Tag is a library (a set of variables, functions and javascript programs that we can load on our site) designed to use the entire Google and third party suite. In addition, it simplifies the labeling, exchange and analysis between different products.

Incubeta

Gtag.js makes the process much easier by keeping tags for different products in one place in the code without doing any additional counting or changing settings in any separate interface. It also simplifies installation, speeding up the deployment of products, and also gives us the opportunity to send information to different Google and third-party tools. It does this only once, rather than requesting different tagging.

We’ll see below that the gtag.js code looks much more compact than the analytics.js code.

Google Analytics default code:

<!– Google Analytics –> <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-XXXXXXXX-X’, ‘auto’); ga(‘send’, ‘pageview’); </script> <!– End Google Analytics –>

gtag.js code:

<!– Global Site Tag (gtag.js) – Google Analytics –> <script async src=” https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID “></script> <script>  window. dataLayer = window.dataLayer || [];  function gtag(){dataLayer.push(arguments)};  gtag(‘js’, new Date());  gtag(‘config’, ‘GA_TRACKING_ID’); </script>

There are different opinions regarding the migration to gtag.js, since depending on the characteristics of the site in question it could be more complex or easier to carry out.

Although Google assures us that the data will not be affected in case we decide to switch to gtag, it is important to have a migration plan and carry out an in-depth analysis that allows us to choose the easiest and most effective path for the team.

For the configuration of various tools on the same page, gtag.js facilitates the implementation in the same way and, for example, if we wanted to use Analytics and AdWords, we only have to add it in the ‘config’ section within the same code:

 gtag(‘config’, ‘UA-12345-6’);  gtag(‘config’, ‘AW-123456789’);

Another scenario would be the configuration of a tag for conversions with different cookies, in this case, the code would look like this:

gtag(‘config’, ‘AW-GOOGLE_CONVERSION_ID_1’, {conversion_cookie_prefix: aw_prefix});

 gtag(‘config’, ‘AW-GOOGLE_CONVERSION_ID_2’, {conversion_cookie_prefix: aw_prefix});

 gtag(‘config’, ‘AW-GOOGLE_CONVERSION_ID_3’, {conversion_cookie_prefix: aw_prefix});

 

For more examples and help on gtag.js implementation we can find information at:  https://developers.google.com/

 

Sources:

https://www.lunametrics.com/blog/2017/12/12/what-is-gtag-google-analytics

https://developers.google.com/analytics/devguides/collection/gtagjs/

https://developers.google.com/analytics/devguides/collection/gtagjs/migration

Read Next

Find out how we can help you

With offices around the world, we can build a team perfect for your needs.

Keep up to date with our monthly insight digest