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.

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:

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.

Are You Adding Tracking To a New Website?YesNo
We recommend that you implement Google Tag Manager instead of using the gtag.js implementation instructions. If you can use GTM, then you do not need to worry about gtag!
You do not need to implement gtag.js! We still prefer Google Tag Manager and believe that tag management systems are the way to go because they provide far greater flexibility in comparison to on-page code.
If you need on-page code for a new site implementation, then you should consider the gtag.js instructions. This will load the latest version of Google Analytics (Universal) and will provide the most flexibility with other Google products.
Universal: No need to rush, but you have the option to upgrade. The analytics.js library is not going anywhere, and gtag.js is loading in this library anyway – so it will offer the same exact functionality with just a different style of implementation. Take your time reviewing the options and your current setup – if you feel you could benefit from the gtag.js style of implementation, start planning the switch!
Classic: Come on now… It’s well past time to upgrade to Universal Analytics, so you might as well do so through following the gtag.js implementation instructions!

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.

Amanda Schroeder is a Senior Consultant and comes from the marketing industry where she found a need for accurate, insightful data that could aid in making results-driven decisions. Amanda’s passion for building solid measurement strategies and connecting all the pieces of integrated digital and traditional marketing campaigns has led her to her current role at LunaMetrics.

  • Thanks for the overview, Amanda. This summer, all of my clients’ Google Analytics accounts were in beta – GST for all accounts – and it was hard to find anything about Global Site Tag (Twitter search saved the day). It’s all we have to work with, so implementation is happening quickly on our end. Google testing tools have finally caught up, too.

    Developers are implementing it in various parts of the section, even though they’re instructed to install it at the very beginning. We haven’t seen any problems so far, but could there be down the road?

    • Amanda

      Hi Janet,

      Installing gtag.js high up in the HTML is recommended as it allows the script to fire as quickly as possible (this is the same recommendation for all of the asynchronous tracking options, such as analytics.js as well). If you were to install it lower in the or even in the body, it would simply be loaded later on the page – the implications of this can vary depending on the complexity of your site. For instance, if you have a lot of custom scripts in the that are being evaluated prior to the gtag.js script, it may not fire as quickly as it could if it were higher up in the page.

      • Thanks for the info, Amanda. I suspect some of the installs are auto-placed by WordPress and plug-ins that aren’t configured for GST. It will take time for everything to catch up…meanwhile, will try to catch those that would be impacted by custom scripts. Thanks!

        • Janet, curious now that it’s been four months. Have you noticed any difference in where it’s placed? I’m moving pretty slow and have only recently decided to phase out Universal for Global tags. I refuse to use Tag Manager. Just wondering if you had an update or we should still just follow Google’s instructions. Thanks!

          • The only issues I see are with WordPress plug-ins that insert GA code and display data in the WP dashboard. They mess up the data badly. Wish they were outlawed! Otherwise, having the code within the head section seems to work just fine.

          • Hah! I completely agree with your thoughts on the plugins! And what stinks is, usually when a client comes to me they’ve already done that and it’s a bummer of a time to undo/explain/straighten out. Feel you there! Thanks for the update and I’ll continue to do it this way since you’ve had a good experience πŸ˜€

  • Hi Amanda,

    Very nice Post!
    Just to clear that only websites using universal script, should move to GTag but I rather prefer to move to GTM.

    All of my customers are using or moving to GTM

    Best Regards,
    Jorge Cunha

    • Amanda

      Yes, GTM is still the preferred approach for implementing Google Analytics.

      • Hi Amanda – Can you elaborate on this a bit, please?

        If gtag.js is __the__ solution to cover all current (and future?) Google products, does it not cover GTM as well?

        Maybe I need to reread the article above, but not that I’ve read your comment here, I’m getting mixed signals.


        p.s. Furthermore, given Google’s fondness (?) to sunset products, it would seem that if gtag.js is there current recommended then its life is likely longer than the older product it replaces, yes?

        • Amanda

          Gtag.js is the recommended on-page option. We still prefer tag management systems over on-page code, but there are many out there who either choose to or need to use on-page code, and this article was written to explain what gtag.js is all about for those individuals who have not yet migrated or do not want to migrate to GTM, for example.

          I can’t speak to how long gtag.js will be the recommended on-page recommendation, but it was designed to allow for future improvements in configuring Google products – so I don’t believe it will be replaced anytime soon.

          • OK. I think that helps.

            I’m not a fan – at all πŸ™‚ – of inline js (e.g., OnClick()) either. As a result, a couple+ years ago** I wrote myself some js that handles the send msg to GA instead. What I do is define the selectors (as you do with reg js / jQ) and the associated message args (in a json array) and my js does the rest. If you can define the rules of an “event” via js then the magic close to being done πŸ™‚

            FYI – My comment about sun-setting was in regards to the older GA js not gtag. Point being, if gtag is (more likely than not) The Future then I’d be apprehensive about using anything but that. The inline js / tagging problem is solvable so imho it’s a non-issue in terms of not using it πŸ™‚

            ** Truth be told, I haven’t looked at this code in too long. I’ll need to loop around to it when I have a moment. It was / is a side project. No one asked for it but me πŸ™‚ When it’s back in top form (read: works with gtag) I’ll try to come back and share the link to the repo.

            Thanks again!

          • Amanda

            Well, technically you could continue to use Urchin (the original analytics library) if you wanted to – it is still supported πŸ™‚ so I don’t believe Google will stop supporting Universal Analytics anytime soon either!

          • Stop? Oh I agree. None the less, “supported” is a pretty loose term when it comes to Google. They sacked Wave. They sack their RSS reader. Logic does not apply to their dumb decisions πŸ™‚

            With that said, which one are they developing, trying to grow, getting their best engineers, etc.? I’m confident the answer to that is gtag().

            Thanks again for your help.

  • kiquenet kiquenet

    how encrypt ‘UA-66848305-11’ value in front end ?

  • Kalyan Banga

    Thanks for the nice post! Indeed helpful…

    Best Regards,
    Kalyan Banga
    Founder, http://fusionanalyticsworld.com

  • Terry

    we launched a new site recently. I went ahead and implemented tag manager – when getting the GA code, I didn’t pay close attention and see that it was gtag, so our website has gtag and tag manager – will this be a problem?

    • Amanda

      Hi Terry,

      Did you put the gtag code inside of a custom HTML tag in GTM or did you put that code on the page?

      If you put the gtag inside a custom HTML tag in GTM, remove it and use a Universal Analytics tag instead. All you need from the Google Analytics interface is the UA ID# for your Universal Analytics tag, not the full gtag code.

      If you put the gtag code on the page, and you have GTM – and you have Universal Analytics tags inside GTM sending data to the same Google Analytics property as the gtag code on the page, you will have duplicate pageviews.

      • Terry

        Hi Amanda,

        I did put the UA ID into the GTM tag, and didn’t put the gtag code on the page, so I think I’m ok. I’m just now reading up on gtag and got confused! Thanks!

        • Amanda

          Haha no problem! This is happening a lot!

  • Thanks for wonderful article you have post

  • Raj Shekahr

    can you please suggest me.
    I’m trying to add Google Analytics to my Finance Website which one is best to add on website to track information and the project is build on Vuejs2.

    • Hi Raj,
      None is the best. They both work in different styles. You decide which one suits you the best.

      With a developer background, I still found easier to add code in page instead of using GTM. But with GTM, non developer people can interact directly to add some tracking alone.

  • Love the flow chart quiz and info! Sticking with Google Tag Manager for now.

    • Amanda

      Good choice πŸ™‚


    hii Nice article


  • Ashish Sharma

    Can I implement GTM code in this new gtag.js script like Analytics and Adword Code?

Contact Us.

Follow Us




We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187