Thursday, February 22, 2024
No menu items!

GTAG vs Analytics.js

When you want to install Google Analytics on your website, one of the options is to add its code directly to your website. That JavaScript library is called gtag.js (a.k.a. global site tag).

But what if you are working with an older project and you see that the GA code it’s using is not gtag.js? Instead, you see a code that looks like this:

This code is using an older version of the JavaScript library called analytics.js

Just by looking at that code, you can see that it is analytics.js.

So what is the difference between GTAG vs analytics.js? Should you migrate to gtag.js instead? In this blog post, I will explain that.


What is analytics.js?

There are several versions of Google Analytics. Different versions used different tracking codes. Universal Analytics (a.k.a. GA3) used a JavaScript library called analytics.js and it looked like this:

<!– Google Analytics –>
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
ga(‘create’, ‘UA-XXXXX-Y’, ‘auto’);
ga(‘send’, ‘pageview’);
<!– End Google Analytics –>

You can recognize it from parts like:


Google Analytics URL that loads the analytics.js file.

And ga() commands (like ga(‘create’) or ga(‘send’)).

So if you see some codes on your website that use commands such as: ga(‘send’, ‘pageview’); or ga(‘send’, ‘event’, ‘Videos’, ‘play’, ‘Fall Campaign’); it means that it is using analytics.js.


What is gtag.js?

In 2017, Google decided to create a more unified tracking library called Global Site Tag (GTAG) that would be applied not only to Google Analytics but also to Google Ads. This was supposed to mitigate limitations related to 3rd party cookies that Google Ads was using. Instead of using 3rd party cookies to track conversions, Google switched to first-party cookies.

And in general, having a single library looks more convenient for developers than having different ones.

Here’s what that code looks like:

<!– Global site tag (gtag.js) – Google Analytics –>
<script async src=””></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){window.dataLayer.push(arguments);}
  gtag(‘js’, new Date());

  gtag(‘config’, ‘GA_MEASUREMENT_ID’);

You’ll notice that the code is loaded from By the way, if you want to learn the differences between GTAG and Google Tag Manager, read this.

Also, instead of ga() command, we now have gtag().


analytics.js vs gtag.js

Let’s take a look at general differences.


#1. Different code structure

The quick way to identify that the code is from the analytics.js library is to look for “analytics.js” in the source code. Also, all commands (e.g. to send a pageview or event) are using a ga() command.

When it comes to gtag, the URL of the library contains the word gtag and all commands are gtag().


#2. gtag.js loads analytics.js

When gtag.js was introduced for Universal Analytics, sudden deprecation of the analytics.js library would be a nightmare for Google and website owners. Thus Google created gtag.js as a wrapper for analytics.js.

In other words, if you use gtag.js for GA3 (I mean if you have added gtag.js directly to the source code of the website), gtag.js will still load analytics.js on a site.

You can see that by using the Tag Assistant (legacy) Chrome extension. Enable the extension and then refresh the page where gtag is installed (of Universal Analytics). You will see two rows: one for the global site tag and one for Google Analytics (analytics.js). Both of them will use the same GA tracking ID (UA-XXXXXX-X).

It was an easier way to do so rather than try to completely replace the analytics.js library.


#3. analytics.js is no longer updated

Since GTAG is the newest version of the Google Analytics tracking code, it is maintained and updated. The latest version, Google Analytics 4, also uses GTAG thus you can rest assured that Google will keep it up-to-date.

Analytics.js, on the other hand, does not receive any meaningful updates (just like Universal Analytics in general). This is now part of the GA history and it does not make sense to add new features to it.

So if you want to install Google Analytics by adding its code directly to the source code of your website, always go with GTAG. In fact, this is the library that is displayed in the interface of Google Analytics.

But if you ask me, I would always recommend installing Google Analytics with Google Tag Manager. It will give you more power and flexibility. In the long run, that’s a huge win. Also, there are more benefits of using GTM.


Should you migrate to gtag.js if you use analytics.js?

If you are working on a new project and you want to install Google Analytics, you should use either GTAG or Google Tag Manager. There isn’t even a slight reason why you should use analytics.js instead. At least, I cannot think of anything.

But what if you already have a project that uses analytics.js? Maybe it was configured like in 2015 or so?

Ideally, if you have a budget for the migration, I would highly recommend moving your Google Analytics setup to Google Tag Manager (instead of migrating to gtag.js from analytics.js). It will allow you to add new tracking codes much faster. In many cases, a developer might not be even needed. And there are more benefits.

I explain a smooth migration process in my GTM course for beginners.

But if for some reason, you don’t want to use Google Tag Manager, then the question is should you migrate to gtag.js or not?

If you are also running Google Ads and you have linked Universal Analytics with it, then I would recommend migration to gtag.js. It will make sure that cookies (needed for attribution and conversion tracking) will be stored properly, in a 1st party context.

But if you don’t run Google Ads and you use Google Analytics just for analysis of your website traffic, events, then you can continue using analytics.js. At least for now. Who knows what will happen in two years. Maybe one day, Google will send you an email that they decided to deprecate the analytics.js library.

So in the short term, analytics.js can still be an option for you. But in the long run, I would recommend migrating from it. Either to gtag or to Google Tag Manager (GTM is a recommended option). That way, you will avoid panic and additional work at some point in the future.


GTAG vs analytics.js: Final words

This was a quick overview of GTAG vs analytics.js. GTAG is a newer and maintained version of the Google Analytics tracking code. Analytics.js is part of history. I don’t know when/if Google will decide to deprecate this older library but at some point in the future, that can happen.

To be prepared for that, it is recommended to migrate away from analytics.js. Either to GTAG or Google Tag Manager.

Personally, I always recommend migration to Google Tag Manager because it will give you more flexibility in the future. Want to track a form? Click on a particular element? Something else? With Google Tag Manager, you will be able to do that on your own (in many cases).

The post GTAG vs Analytics.js appeared first on Analytics Mania.

Read MoreAnalytics Mania



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments