gtag.js – Global Site Tag in Google Analytics – Tutorial

In this article, I am going to talk about the Global Site Tag (gtag.js). If you try to install Google Analytics or Google Ads 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.

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 shortly 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 Ads conversion tracking and will support tracking for many other Google products soon. So I won’t call gtag.js 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 that 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 Ads 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’.

To keep the size of gtag.js small, this library calls other libraries internally. For example, gtag.js calls analytics.js and conversions.js internally. 

This has also been done to maintain backward compatibility. Because if Google completely rewrites 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 lightweight.

For an end-user like your client, this change won’t make any difference. But for developers, the implementation is going to become a bit easier. As a developer, you have to deal with (learn and remember) only one JavaScript Library.

Get the E-Book (52 Pages)

Get the E-Book (50 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 the 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 a 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 sends a ‘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 Ads

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

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

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

As in:

<!– Global site tag (gtag.js) – Google Ads: 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 the case of Google Ads, 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 Ads is using the same command queue function ‘gtag()’ as used by Google Analytics.

Defining groups through the ‘config’ command

The ‘config’ command is also used to define groups. Groups are 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 the 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 Ads

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

For example:

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

<script>

 gtag(‘event’, ‘conversion’, {

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

</script>

Note: See how the ‘send_to’ parameter is used here to send event data to another Google product called ‘Google Ads’.

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.

Set persistent values with gtag.js

Using gtag.js, you can set parameters on the website which will be sent along with every event. This is very useful if you want to set a particular parameter across the website. For example, if all the transaction on your website happens in the same currency, you can set the currency by updating the config command.

To set parameters you can update the config command as below

gtag(‘config’, ‘UA-XXXXX-1’, {‘currency’: ‘USD’});

You can also set multiple parameters in the config command. The following code sets currency parameter as well as language parameter

gtag(‘config’, ‘UA-XXXXX-1’, {

  ‘currency’: ‘USD’,

  ‘language’: ‘english’

});

If you are sending data to multiple Google Analytics property you can use the SET command to make it more efficient, using the below code

gtag(‘set’, {

  ‘currency’: ‘USD’,

  ‘language’: ‘english’

});

gtag(‘config’, ‘UA-XXXXX-1’);

gtag(‘config’, ‘UA-XXXXX-2’);

gtag(‘config’, ‘UA-XXXXX-3’);

Cookies and user identification with gtag.js

gtag.js can also be used to set cookies on browsers or to update cookie values. You can also use it to create user ID. By default, Google Analytics sets its cookie, but in the case that you want to set an additional cookie you can use below code:

gtag(‘config’, ‘UA-XXXXX-1’, {

  ‘cookie_prefix’: ‘MyCookie’,

  ‘cookie_domain’: ‘blog.example.com’,

  ‘cookie_expires’: 30 * 24 * 60 * 60  // 30 days, in seconds

});

If you want to update an existing cookie you can set ‘cookie_update’ to true. This will update the cookie value for the most recent visit. If you set ‘cookie_update’ to false, cookies are not updated on each page load. This has the effect of cookie expiration being relative to the first time a user visited the site.

You can use the below code to update the cookie on every visit:

gtag(‘config’, ‘UA-XXXXX-1’, {

  cookie_update: true

});

You can use the below code if you don’t want to update the cookie expiry:

gtag(‘config’, ‘UA-XXXXX-1’, {

  cookie_update: true

});

To set a user ID using gtag.js, you can use the below code:

gtag(‘config’, ‘UA-XXXXX-1’, {

  ‘user_id’: ‘USER_ID’

});

You can take your developer’s help if you need to update the user ID for each user.

Measure activity across domains

Cross-domain measurement allows you to uniquely identify your user across the different domains you have.

With cross-domain tracking, activity is attributed to the same user regardless of the domain. This helps to keep the user count more accurate and give you a complete view of a user journey across domains.

When you set up cross-domain tracking, the enhanced measurement option to track outbound links is ignored when an outbound link leads to another domain. Hence the user is considered the same user across the different domains.

You need to configure the source domain and the destination domain when setting up cross-domain tracking. This can be achieved using gtag.js.

You can use the below code on the source domain:

gtag(‘config’, ‘UA-XXXXX-1’, {

  ‘linker’: {

    ‘domains’: [‘example.com’] // Destination Domain

  }

});

Now you need to update the gtag.js code on the destination domain as well. You can use the below code on the destination domain:

gtag(‘config’, ‘UA-XXXXX-2’, {

  ‘linker’: {

    ‘accept_incoming’: true

  }

});

Bi-directional cross-domain measurement

Suppose you have two domains and you are not sure which domain the user will visit first before navigating to another domain. You can set bi-directional cross-domain tracking using gtag.js.

Let’s say I have two websites one is samplewebsite1.com and another is samplewebsite2.com. Here you need to enable auto-linking on both websites, and also accept linker parameters.

You can use the below code on the first website (samplewebsite1.com) to set auto-linking:

gtag(‘config’, ‘UA-XXXXX-1’, {

  ‘linker’: {

    ‘domains’: [‘samplewebsite2.com’]

  }

});

Use the below code on the second website (samplewebsite2.com) to set auto-linking:

gtag(‘config’, ‘UA-XXXXX-2’, {

  ‘linker’: {

    ‘domains’: [‘samplewebsite1.com’]

  }

});

Now when it comes to setting up accept linker you can use the below code for the first website:

gtag(‘config’, ‘UA-XXXXX-1’, {

  ‘linker’: {

    ‘domains’: [‘example.com’, ‘example-pet-store.com’]

  }

});

Use below code for the second website:

gtag(‘config’, ‘UA-XXXXX-2’, {

  ‘linker’: {

    ‘domains’: [‘example.com’, ‘example-pet-store.com’]

  }

});

That is how you can set bi-directional cross-domain tracking.

Custom dimensions and metrics with gtag.js

When it comes to analytics implementations you will have probably come across a situation where you want to pass custom metrics and custom dimensions to an analytics property.

Well, you can do this using gtag.js. You have to use the ‘custom map’ parameter and define the custom dimension or custom metrics.

To define the custom definition or metric you can use the below code:

gtag(‘config’, ‘UA-XXXXX-2’, {

  ‘custom_map’: {‘dimension<Index>’: ‘dimension_name’}

{‘metric<Index>’: ‘metric_name’}

});

Let’s take an example. If you have to set the custom dimension ‘Country’ and the custom metrics ‘goal_value’, the code will look like below:

gtag(‘config’, ‘UA-XXXXX-2’, {

  ‘custom_map’: {‘dimension<1>’: ‘country’}

{‘metric<1>’: ‘goal_value’}

});

Once you have defined your custom dimensions and metrics, you can send them along with any event as below

gtag(‘event’, ”, {‘country’: US, ‘goal_value’: 200});

Disable advertising features

Advertising features in Google Analytics settings can be enabled through the admin settings.

There might be some cases where you would like to disable the advertising feature via the gtag.js code, in order to send this signal to connected tags, such as Google Ads or campaign manager.

In such cases, you can use the below code to disable the advertising feature:

gtag(‘set’, ‘allow_google_signals’, false);

If you are using multiple properties and want to disable the advertising feature for a specific property you can use the below code:

gtag(‘config’, ‘UA-XXXXX-2’, {‘allow_google_signals’: false});

Enhanced ecommerce using gatg.js

Enhanced ecommerce allows you to send impression data, product data, promotion data, and action data to Google Analytics. You can achieve this by configuring gtag.js code for every ecommerce event.

Note: You must have enhanced ecommerce enabled in the Google Analytics property.

Product/Item List Views/Impressions

This event will measure product impressions. It could be a single product or multiple products. You can use the below code to send product impressions to Google Analytics.

Fire this code whenever the products are displayed on the page:

gtag(‘event’, ‘view_item_list’, {

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: 2

    },

    {

      “id”: “P67890”,

      “name”: “Flame challenge TShirt”,

      “list_name”: “Search Results”,

      “brand”: “MyBrand”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Red”,

      “list_position”: 2,

      “quantity”: 1,

      “price”: 3

    }

  ]

});

Product/Item List Clicks

You can measure a product click by sending a ‘select_content’ event. You also need to specify ‘content_type’ as the product.

Fire the below code when the user clicks on any of the products on the website:

gtag(‘event’, ‘select_content’, {

  “content_type”: “product”,

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: 2

    }

  ]

});

 

  1. Product/Item Detail Views

Once a user has clicked on the product he will be redirected to a product detail page. You can measure product/item detail views using the below code.

Fire this code whenever the product page is loaded:

gtag(‘event’, ‘view_item’, {

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: ‘2.0’

    }

  ]

});

 

Add to Cart

To measure add to cart events you can send an ‘add_to_cart’ event along with product details. Fire the below code whenever a user clicks on add to cart button:

gtag(‘event’, ‘add_to_cart’, {

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: ‘2.0’

    }

  ]

});

Remove from Cart

To measure remove from cart events you can send a ‘remove_from_cart’ event along with product details.

Fire the below code whenever a user clicks on the remove from cart button:

gtag(‘event’, ‘remove_from_cart’, {

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: ‘2.0’

    }

  ]

});

 

Promotion Views/Impressions

You can measure both impressions and clicks on internal site promotions. These promotions could include banners displayed on the site or maybe an offer for free shipping.

To measure promotion view details, you need to send a ‘view_promotion’ event using gtag.

Fire the below code whenever a promotion banner is displayed on the webpage:

gtag(‘event’, ‘view_promotion’, {

  “promotions”: [

    {

      “id”: “abc123”,

      “name”: “summer_promo”

    },

    {

      “id”: “xyz987”,

      “name”: “spring savings”

    }

  ]

});

Promotion Clicks

To measure promotion clicks details, you need to send a ‘select_content’ event using gtag.

Fire the below code whenever the promotion banner is clicked by the user on the webpage:

gtag(‘event’, ‘select_content’, {

  “promotions”: [

    {

      “id”: “abc123”,

      “name”: “summer_promo”

    }

  ]

});

Checkouts

You can measure each step in a checkout process. You also get an option to measure the checkout option if it’s applicable for your website.

When the user clicks on the checkout button (starts the checkout process) you can fire the below code:

gtag(‘event’, ‘begin_checkout’, {

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: ‘2.0’

    }

  ],

  “coupon”: “”

});

 

If you have multiple steps in your checkout you can send a ‘checkout_progress’ event.

Use the below code on subsequent checkout steps:

gtag(‘event’, ‘checkout_progress’, {

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: ‘2.0’

    }

  ],

  “coupon”: “SUMMER_DISCOUNT”

});

In the case that you want to measure the checkout option selected by a user, you can send a ‘set_checkout_option’ event during the checkout step where users select the desired option, you can use the below code:

gtag(‘event’, ‘set_checkout_option’, {

  “checkout_step”: 1,

  “checkout_option”: “shipping method”,

  “value”: 3

});

 

Purchases

To measure purchases (transactions) on a website, you can send a ‘purchase’ event.

Fire the below code whenever the transaction happens:

gtag(‘event’, ‘purchase’, {

  “transaction_id”: “24.031608523954162”,

  “affiliation”: “Google online store”,

  “value”: 23.07,

  “currency”: “USD”,

  “tax”: 1.24,

  “shipping”: 0,

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: ‘2.0’

    },

    {

      “id”: “P67890”,

      “name”: “Flame challenge TShirt”,

      “list_name”: “Search Results”,

      “brand”: “MyBrand”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Red”,

      “list_position”: 2,

      “quantity”: 1,

      “price”: ‘3.0’

    }

  ]

});

 

Refunds

You can measure a complete refund or partial refund using gtag. This can be done by sending a ‘refund’ event with the transaction ID.

If you want to measure a complete refund you can use the below code.

gtag(‘event’, ‘refund’, { “transaction_id”: “T12345” })

If the user wants a partial refund, you can do this by sending a ‘refund’ event along with a transaction ID and details of the product to be refunded.

Use the below code in case of partial refund:

gtag(‘event’, ‘refund’, {

  “transaction_id”: “79.18502354114992”,

  “affiliation”: “Google online store”,

  “value”: 23.07,

  “currency”: “USD”,

  “tax”: 1.24,

  “shipping”: 0,

  “items”: [

    {

      “id”: “P12345”,

      “name”: “Android Warhol T-Shirt”,

      “list_name”: “Search Results”,

      “brand”: “Google”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Black”,

      “list_position”: 1,

      “quantity”: 2,

      “price”: ‘2.0’

    },

    {

      “id”: “P67890”,

      “name”: “Flame challenge TShirt”,

      “list_name”: “Search Results”,

      “brand”: “MyBrand”,

      “category”: “Apparel/T-Shirts”,

      “variant”: “Red”,

      “list_position”: 2,

      “quantity”: 1,

      “price”: ‘3.0’

    }

  ]

});

 

IP anonymization with gtag.js

The IP anonymization feature helps website users to retain and not share their data.

This feature also helps website owners to comply with local data protection authorities and legal regulations, like GDPR, which may prevent the storage of full IP address information.

To anonymize IP addresses for an analytics property, you need to set the value of the ‘anonymize_ip’ parameter to ‘true’ while configuring your GA property.

This can be done using the below command:

gtag(‘config’, ‘UA-XXXXX-1’, { ‘anonymize_ip’: true });

Renaming the gtag object

gtag() is the global object used to define several Google products, including Google Analytics, Google Ads, campaign manager, etc.

In some cases, if you may want to add a gtag() object and it’s already being used by some other tool. In this case, gtag.js gives you additional functionality to rename the gtag() object with another object.

To do so you need to replace the gtag() object with another object in the global site snippet (gtag.js). For example, if I want to replace the gtag() object with analytics() then my global snippet will look like below:

<!– Global Site Tag (gtag.js) – Google Analytics –>

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

<script>

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

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

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

 

  analytics(‘config’, ‘UA-XXXXX-1’);

</script>

See the image below and you will understand what changes need to be done.

Once you have renamed the gtag() object as analytics(), remember that if you want to send any event data to the analytics property you need to use the analytics() command, as below:

analytics(‘event’, ‘sign in’, {‘method’: ‘Google’}); // Sends Sign in event

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 a 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 maintains 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 (at the time of writing this) 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 shortly Google is going to introduce new features that 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 a mini-gtm.

I am not exactly sure, how they came up to this conclusion. This could be a lack of knowledge of the GA development environment, or the blind leading the blind, or something else. The 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 the same event data to multiple GA properties or Google products does not make it a substitute for GTM, not even remotely. For a 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, plus 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 the container tag? It is simply a JavaScript library called gtm.js

The container tag communicates 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 the 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 the GTM user interface. For an end-user and for the majority of GTM users, everything is going to look mostly the same.

I think sooner or later, the 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 and 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 they can deal with this new headache called ‘gtag.js’ : )

Register for the FREE TRAINING...

"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."



Here’s what we’re going to cover in this training…

#1 Why digital analytics is the key to online business success.

​#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.

#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.

#4 ​Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.

#5 The number 1 reason why conversion optimization is not working for your business.

#6 How to advertise on any marketing platform for FREE with an unlimited budget.

​#7 How to learn and master digital analytics and conversion optimization in record time.



   

My best selling books on Digital Analytics and Conversion Optimization

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.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade
error: Alert: Content is protected !!