Guide to Google Global Site Tag – gtag.js – The New Google Analytics Library

If you try to install Google Analytics or Google Adwords Conversion Tracking code today, you are going to see a new tag called the ‘Global Site Tag (gtag.js):

<!– Global site tag (gtag.js) – Google Analytics –>

<script async src=”https://www.googletagmanager.com/gtag/js?id=1123456-71″></script>

<script>

 window.dataLayer = window.dataLayer || [];

 function gtag(){dataLayer.push(arguments);}

 gtag(‘js’, new Date());

 gtag(‘config’, ‘UA-1123456-71’);

</script>

For all brand new Google Analytics accounts, the global site tag (gtag.js) is the recommended tracking method now.

You need only one global site tag (also known as global snippet) per web page.

Google has repeatedly mentioned in its documentation that by using gtag.js, “you will be able to benefit from the latest dynamic features and integrations as they become available.”

This is a strong hint that in the near future Google is going to introduce new features which are available only in gtag.js and not necessarily in analytics.js

What is gtag.js?

Global site tag or gtag.js is the new JavaScript library from Google.

Unlike ga.js or analytics.js, it is not strictly a Google Analytics Library as it is supporting Google Adwords Conversion tracking and will support tracking for many other Google products in the near future.

So I won’t call gtag.js as the next version of analytics.js

What I think Google is trying to do, is to use one JavaScript Library for tracking all of its products.

For example, at present there is analytics.js library which is used to measure users interactions with your website and send them to Google Analytics Server.

Then we have got gtm.js library which is used for Google Tag Manager.

Then we have got firebase.js library which is used to measure users interactions with your mobile apps via Firebase

Then we have got conversion.js library which is used for Google Adwords Conversion Tracking.

The purpose of gtag.js seems to be, to replace all of these libraries with one single library and hence gtag.js is being promoted as ‘one tag to rule them all’.

In order to keep the size of gtag.js small, this library call other libraries internally.

For example gtag.js call analytics.js and conversions.js internally.

This has also been done to maintain backward compatibility.

Because if Google completely rewrite gtag.js from scratch, then most likely it won’t remain compatible with existing tracking methods.

And I think gtag.js will continue to call other libraries internally for another big reason i.e. to remain light weight.

For an end user like your client, this change won’t make any difference. But for developers, the implementation is going to become bit easier.

As a developer, you have to deal with (learn and remember) only one JavaScript Library.

Get the E-Book (37 Pages)

Learn to read e-commerce reports book banner

Get the E-Book (104 Pages)

The gtag() Command Queue Function

The second advantage of using gtag.js is likely to be the use of a single command queue function, regardless of the Google product being used.

This command queue function is gtag()

Examples:

gtag(‘config’, ‘UA-1234567-89’);
gtag(‘config’, ‘AW-957488885’);

If you remember, for analytics.js the command queue function was ga():

ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);

Here,

[trackerName.] [pluginName:] [methodName] is called Google Analytics Command.

For example:

ga(‘create’, ‘UA-XXXXX-Y’, ‘auto’);

ga(‘send’, ‘pageview’);

Here, ‘ga’ is the command queue function and ‘create’ and ‘send’ are commands.

We use command queue function to measure/track any type of user interactions on a website.

The ‘config’ Command

The command queue function for gtag.js is ‘gtag()’ and it uses different set of commands.

The most notable of these commands is ‘config’ (short form for ‘configure’).

The ‘config’ command configures the GA property with the tracking ID as the default property to receive data sent by the event command and also automatically send ‘pageview’ hit to the default property.

For example the following line of code automatically sends pageview hit to the GA property ‘UA-1234567-89’:

gtag(‘config’, ‘UA-1234567-89’);

You don’t need to create a tracking object before you can send the pageview hit.

The pageview hit is automatically sent.

If you want to send pageview hits to two different GA properties then use two ‘config’ commands like the one below:

gtag(‘config’, ‘UA-1234567-89’);

gtag(‘config’, ‘UA-1234567-90’);

So there is no need to create a new tracking object for each GA property.

If you have used multiple tracking objects in the past, you understand how cumbersome it is, to deal with them.

If you want to send the same pageview hit via analytics.js, then you would first need to create a new tracking object for each GA property via the ‘create’ command and then send pageview hit separately via the ‘send’ command:

ga(‘create’, ‘UA-1234567-89’, ‘auto’);

ga(‘send’, ‘pageview’);

ga(‘create’, ‘UA-1234567-90′, ‘auto’,{‘name’: ‘WebProperty2’});

ga(‘WebProperty2.send’, ‘pageview’);

The ‘config’ Command for Google Adwords

The ‘config’ command can also be used to configure a Google Adwords account with the Adwords ID to receive data sent by the event command.

For example the following line of code configure a Google Adwords account with the Adwords ID: 957488885

gtag(‘config’, ‘AW-957488885’);

As in:

<!– Global site tag (gtag.js) – Google AdWords: 957488885 –>

<script async src=”https://www.googletagmanager.com/gtag/js?id=AW-957488885″></script>

<script>

 window.dataLayer = window.dataLayer || [];

 function gtag(){dataLayer.push(arguments);}

 gtag(‘js’, new Date());

 gtag(‘config’, ‘AW-957488885’);

</script>

In case of Google Adwords, the global site tag is used to add your website visitors to your basic remarketing lists and to set new cookies on your domain, which will store information about the ad click that brought a user to your website.

Note: Google Adwords is using the same command queue function ‘gtag()’ as used by Google Analytics.

Defining groups through ‘config’ command

The ‘config’ command is also used to define groups.

Groups is a new concept introduced in gtag.js

A group is a collection of one or more GA properties and this is what makes it possible to send pageview and other hits to multiple GA properties without creating and using multiple tracking objects.  

There are two types of groups:

  1. Default groups
  2. Non-default groups

Use the following similar line of code to define a default group which contains only one GA property:

gtag(‘config’, ‘UA-1234567-89’, {‘groups’: ‘default’});

Which is equivalent to:

gtag(‘config’, ‘UA-1234567-89’);

Use the following similar lines of code to define a default group which contains two GA properties:

gtag(‘config’, ‘UA-1234567-89’, {‘groups’: ‘default’});

gtag(‘config’, ‘UA-1234567-90’, {‘groups’: ‘default’});

Which is equivalent to:

gtag(‘config’, ‘UA-1234567-89’);

gtag(‘config’, ‘UA-1234567-90’);

Use the following similar line of code to define a default group which contains three GA properties:

gtag(‘config’, ‘UA-1234567-89’, {‘groups’: ‘default’});

gtag(‘config’, ‘UA-1234567-90’, {‘groups’: ‘default’});

gtag(‘config’, ‘UA-1234567-91’, {‘groups’: ‘default’});

Which is equivalent to:

gtag(‘config’, ‘UA-1234567-89’);

gtag(‘config’, ‘UA-1234567-90’);

gtag(‘config’, ‘UA-1234567-91’);

Use the following similar line of code to define a non-default group named ‘optimizesmart’ which contains only one GA property:

gtag(‘config’, ‘UA-1234567-89’, {‘groups’: ‘optimizesmart’});

Use the following similar lines of code to define a non-default group named ‘optimizesmart’ which contains two GA properties:

gtag(‘config’, ‘UA-1234567-89’, {‘groups’: ‘optimizesmart”});

gtag(‘config’, ‘UA-1234567-90’, {‘groups’: ‘optimizesmart’});

So whenever the value of ‘groups’ parameter is anything other than ‘default’ then it is a non-default group.

The ‘event’ command

After ‘config’ the second most useful command in case of gtag.js is the ‘event’ command.

Following is the syntax for using the ‘event’ command:

gtag(‘event’, ‘event_name’, {

 // Event parameters

 ‘parameter_1’: ‘value_1’,

 ‘parameter_2’: ‘value_2’,

 // …

});

Here,

event_name’ can be a recommended event or custom event.  I will explain these events in more detail in another article.

‘Parameter_1’ can be ‘event_category‘.

‘Parameter_2’ can be ‘event_action’‘.

‘Parameter_3’ can be ‘event_label‘.

‘Parameter_4’ can be ‘value‘.

Example:

gtag(‘event’, ‘sign_up’, {

 ‘send_to’: [‘UA-1234567-21’, ‘UA-1234567-22’ ],

  ‘event_category’: ‘Videos’,

 ‘event_action’: ‘Fall Campaign’

 ‘event_label’: ‘Fall Campaign’

 ‘value’: 400

 ‘method’: ‘Google’

});

You can use the event command to send event data to a single GA property or group of GA properties.

Following is the example of code to send event data to a single GA property:

gtag(‘event’, ‘sign_up’, {‘method’: ‘Google’});

Following is the example of code to send same event data to multiple GA properties:

gtag(‘event’, ‘sign_up’, {

 ‘send_to’: [‘UA-1234567-21’, ‘UA-1234567-22’],

 ‘method’: ‘Google’

});

Following is the example of code to send same event data to multiple GA groups:

gtag(‘event’, ‘sign_up’, {

 ‘send_to’: [‘default’, ‘group_1’, ‘group_2’],

 ‘method’: ‘Google’

});

Here, ‘group_1’ and ‘group_2’ are non-default groups.

The ‘event’ command for Google Adwords

You can also use the same ‘event’ command to track conversions in Google Adwords.

For example:

<!– Event snippet for Form Lead conversion page –>

<script>

 gtag(‘event’, ‘conversion’, {

 ‘send_to’: ‘AW-957488885/tgtPCKf2q3gQn7XIyAM’});

</script>

Note: How the ‘send_to’ parameter is used here to send event data to another Google product called ‘Google Adwords’.

Likewise, the syntax for setting up ecommerce tracking, cross domain tracking, event tracking,  enhanced ecommerce tracking, custom dimensions and metrics or any other type of tracking is going to be different when you are using gtag.js.

Should you start using Global Site Tag – gtag.js?

If you are setting up a brand new Google Analytics account then you have no choice but to use the Global Site Tag.

If you already have Google Analytics account with all the tracking set up then you can continue to use your existing tracking setup for the foreseeable future.

Google almost always maintain backward compatibility.

So your existing tracking is not going to break, even if you choose not to use gtag.js

The gtag.js is currently in Beta and as such isn’t covered by an SLA (Service Level Agreement).

The downside of sticking to analytics.js is that you will not be able to take advantage of the more powerful event tracking methods mentioned in this article, which are available only in gtag.js

Also Google has repeatedly mentioned in its documentation that by using gtag.js, “you will be able to benefit from the latest dynamic features and integrations as they become available.”

This is a strong hint that in the near future Google is going to introduce new features which are available only in gtag.js and not necessarily in analytics.js

Eventually, gtag.js is going to become the primary and standard tracking method. 

Global Site Tag – gtag.js or Google Tag Manager

There is an interesting (better say ridiculous) discussion going on in the blogosphere where many ‘experts’ are comparing gtag.js with Google Tag Manager.

Some ‘experts’ have even declared gtag.js as mini-gtm.

I am not exactly sure, how they came up to this conclusion.

This could be lack of knowledge of the GA development environment or blind leading the blind or something else.

Internet is choke full of misinformation and you need to be super selective about, where you choose to consume information.

Just because gtag.js can send same event data to multiple GA properties or Google products does not make it a substitute for GTM, not even remotely.

For the start, gtag.js does not come with a user interface like GTM. It is not a tag management tool, not even remotely.

gtag.js does not work for non google products and I doubt, it ever will.

gtag.js is just a JavaScript library whereas GTM is a fully blown tag management solution with tag templates, user interface and other bells and whistles.

If you are using GTM then do you need to use gtag.js?

If you treat Google Tag Manager as a car then its engine is the container tag.

And what is container tag?

It is simply a JavaScript library called gtm.js

The container tag communicate with other libraries (like: analytics.js, conversion.js) etc for carrying out ETL (Extract, Transform, Load) functions.

gtag.js is just another JavaScript library with which GTM can communicate.

So, if you are using GTM then do you need to use gtag.js?

Well it depends upon, what exactly you trying to accomplish with GTM.

As mentioned earlier, if you are using gtag.js, you don’t need to create tracking objects.

So if your GTM tracking method involves the use of multiple tracking objects then implementation is going to be different.

Similarly, if you are sending same event data to multiple GA properties without using multiple trackers then GTM implementation is going to be different.

Everything is happening in the backend and if you are not directly dealing with the ‘code’, you won’t notice anything unusual in GTM user interface.

For an end user and for majority of GTM users, everything is going to look mostly the same.

 

I think sooner or later, gtm.js library will also be replaced by gtag.js and we will be using the global site tag to install Google Tag Manager.

Overall, Google is going in the right direction by reducing the number of tracking libraries to learn and remember.

However bear in mind that the advent of gtag.js is important for GA developers & not for end users.

If you are not into coding, you don’t need to worry about gtag.js.

You will most probably hire a GA developer and he can deal with this new headache called ‘gtag.js’ : )

Become a Web Analytics & Conversion Optimization PRO with our FIVE most popular FREE e-Books

Select the ebook you want:


E-Book #1: Best Excel Charts For Data Analysis And Reporting (40 Pages)

E-Book #2: Beginners Guide To Google Analytics Attribution Modelling (52 Pages)

E-Book #3: Beginners Guide To Google Tag Manager (72 Pages)

E-Book #4: Google Tag Manager Data Layers (62 Pages)

E-Book #5: Key Performance Indicator - KPI (32 Pages)

Take your Analytics knowledge to the next level. Checkout my Best Selling Books on Amazon

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Alert: Content is protected !!