What Is Gtag.js With Google Analytics And Do I Need It?
If you have attempted to add Google Analytics tracking code to a website recently, you may have noticed something new – a completely different Google Analytics snippet that looks like the code snippet above.
There has been some confusion around what exactly this ‘gtag’ snippet is for. This article should help clear things up and help you decide if you need gtag.
What is A Global Site Tag (gtag.js)?
Over the years, Google Analytics has released several versions of its JavaScript library, with each one introducing new features and becoming the new recommended implementation. However, this is not quite the same thing. It’s not just another analytics library – the “Global Site Tag” is designed to simplify and streamline website tagging for all Google products.
Geared towards websites that are using on-page tracking code for various tools like Google Analytics and Google AdWords, gtag.js marks a fundamental shift in the way we collect data. It is a new style of implementation where the ultimate goal is to have one centralized script tag that is capable of sending data to all of your Google marketing and analytics tools at the same time – so you no longer need to spend hours configuring tags on your site.
It is not an entirely new library for Google Analytics alone – in fact, behind the scenes it is merely loading in the existing analytics.js library, along with other libraries depending on what tools you configure, such as conversions.js for Google AdWords.
In the past we talked about upgrading from Classic Analytics (ga.js) to Universal Analytics (analytics.js) and compared that to switching out the engine of a car; exchanging a v6 (Classic) for a v8 (Universal), for example. Migrating to gtag.js is completely different – you’re still working with the same v8 engine of the analytics.js library, you’re just using it differently. Maybe we can compare this to simply tuning or reprogramming the existing v8 engine…
There are a lot of interesting opportunities that come with gtag.js, and you may want to plan your migration so that you can take advantage of those as they become available, but there is no need to rush into this migration.
But Wait – Didn’t We Just Start Using Google Tag Manager?
It’s important to note that using gtag.js is not necessary if you are already using Google Tag Manager. If you are using GTM already, continue to do so. In fact, we still recommend upgrading to GTM from on-page code for a number of reasons, linked below, but if you must on use on-page code, read on.
Published: March 23, 2017
How Does it Work?
Take this example, where a typical website may have the following installed:
- Google Analytics pageview tracking code on every page
- Google Analytics event tracking for certain interactions of interest
- Google Analytics ecommerce tracking for purchases, checkout steps, and more
- Google AdWords conversion tracking for purchases or lead form completions
- Google AdWords remarketing tags for building audiences in AdWords
That’s FIVE different script tags for Google products alone!
Gtag.js makes this entire process much easier by:
- Encouraging you to keep many of the tags for Google products in one place in your code
- Simplifying installation to get you up and running with Google products sooner
- Opening up the opportunity to send data to multiple tools at once, rather than requiring separate tags
For example, if you’re using Google Analytics and Google AdWords, your gtag.js snippet would look like this:
CODE HERE
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-1234567-8');
gtag('config', 'AW-1234567-8');
</script>
The gtag.js code would be placed on each page of your website. config
is a command that pulls in the corresponding library for the product configured, such as analytics.js for Google Analytics or conversion.js for Google AdWords.
These config
commands also take care of some initial setup items like triggering a pageview hit for Google Analytics or a remarketing hit for AdWords. You can prevent this default behavior by updating the config
line as such:
gtag('config', 'UA-66848305-11', { 'send_page_view': false });
Be careful of this if you choose to add new AdWords conversion tracking to your website, for example, as AdWords is now providing gtag-style setup instructions!
There will also be some changes in the way you set up event tracking, ecommerce, cross-domain tracking, etc. for Google Analytics, as well as conversion tracking for AdWords. All of that can be found in the documentation for gtag.js for Google Analytics and Google AdWords.
Do I Need Gtag?
So should you upgrade? We suggest using the following flow chart to see if you should consider using gtag.js.
Remember: If you are currently using on-page analytics.js, there is no need to rush into migrating to gtag.js. The amount of work it will take to do so really depends on your implementation.
Also, you can still install analytics.js if you need to. Visit the analytics.js documentation.