TL;DR, Quick Answer
8 min readgtag() is the JavaScript function behind the Google tag (gtag.js). Each call pushes a command into the dataLayer, and the gtag.js library, loaded asynchronously from googletagmanager.com, reads that queue and sends data to Google Analytics, Google Ads and other Google products. The main commands are config, event, set, get and consent. Consent defaults must run before any config or event call, and Google points people who are not comfortable with JavaScript to Google Tag Manager instead.
What is gtag?
The gtag() function is the JavaScript API of the Google tag, and every call to it pushes a command such as config, event, set or consent into a queue called the dataLayer, which the gtag.js library reads to send data to Google products. Google's reference puts it in one line: "The Google tag (gtag.js) API uses the gtag() function to send data to Google products like Google Analytics and Google Ads." If a site's source loads gtag/js?id=, that site runs the Google tag directly in its own code rather than through a Tag Manager container.
Three names get mixed up here. gtag.js is the library file. gtag() is the function your code calls. The Google tag is the product name Google uses for the whole setup, which it describes as "a single tag you can add to your website to use Google measurement and advertising products like Google Ads, Google Analytics, Campaign Manager, Display & Video 360, and Search Ads 360."
How does gtag.js load on a page?
gtag.js loads through a two-part snippet that Google tells you to place "immediately after the opening <head> HTML tag on every page you want to measure." The first part downloads the library, the second part defines the gtag() function and queues the first commands.
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'TAG_ID');
</script>The order matters less than it looks. The library loads with async, so the inline script runs first. Because gtag() does nothing except dataLayer.push(arguments), every command you call before the library arrives waits in the dataLayer array, and gtag.js processes the queue once it has loaded. That is why you can call gtag('event', ...) anywhere on the page without checking whether the library is ready.
The id in the script URL is a Google tag ID. Google gives "GT-XXXXXXXXX, G-XXXXXXXXX, and AW-XXXXXXXXX" as examples and notes that "the IDs can be used interchangeably when adding the Google tag to your website." One Google tag can carry several IDs and send to several destinations, such as a Google Analytics property and a Google Ads account.
What do the gtag() commands do?
The gtag() commands are five verbs, each passed as the first argument: config, event, set, get and consent. The table uses the syntax and wording from Google's API reference.
| Command | Syntax | What Google says it does |
|---|---|---|
| config | gtag('config', '<TARGET_ID>', {...}) | "Lets you add additional configuration information to targets." |
| event | gtag('event', '<event_name>', {...}) | "Use the event command to send event data." |
| set | gtag('set', {...}) | "Lets you define parameters that will be associated with every subsequent event on the page." |
| get | gtag('get', '<target>', '<field_name>', callback) | "Lets you get various values from gtag.js including values set with the set command." |
| consent | gtag('consent', 'default' or 'update', {...}) | "Use the consent command to configure consent." |
config does more than its description suggests. Google states that "The config command is needed to send data to Google advertisement products," and for Google Analytics the default config call also sends a page_view event. To stop that, Google documents send_page_view: false, which you need when you send pageviews yourself, for example in a single-page app.
set is the one to use sparingly. Google adds: "We recommend using config or event commands wherever possible." Values set at event scope override config values, and config values override anything set globally with set.
consent takes two modes. 'default' sets the starting state before the visitor answers your banner, and 'update' changes it after they do. The four parameters in consent mode v2 are ad_storage, analytics_storage, ad_user_data and ad_personalization; the Google consent mode v2 guide covers what the two newer signals added.

In which order should gtag commands run?
gtag commands run in the order you queue them, and three of them only work if they come before the first config or event call.
- Opt-out flag.
window['ga-disable-MEASUREMENT_ID'] = true"must be set before any calls togtag()are made, and it must be set on each page for which you want to turn off Analytics." - Consent defaults. Google says to set the default for each consent type "on every page of your site before any commands that send measurement data (such as
configorevent)." If your consent tool loads asynchronously, addwait_for_updatewith a value in milliseconds. - Global settings.
setcalls such asallow_google_signals: falsego beforeconfigso the first hit already carries them. - config. One call per tag ID.
- event. Everything after that.
A consent default placed after config arrives too late for the first pageview, which is one of the quieter ways a site ends up sending hits it meant to hold back.
How does gtag relate to the Google tag and Google Tag Manager?
gtag.js and Google Tag Manager are two ways to deploy the same Google tag: with gtag.js you write the commands in your page code, and with Tag Manager you configure them in a web interface and the container script, gtm.js, loads them for you. Google's gtag.js guide says that "if you're unfamiliar with JavaScript, we recommend using Google Tag Manager instead of gtag.js."
| gtag.js in the page | Google Tag Manager | |
|---|---|---|
| Script loaded | gtag/js?id=G-... | gtm.js?id=GTM-... |
| Where commands live | Your site's code | Tags and triggers in the GTM container |
| Who changes tracking | Developers, through a deploy | Anyone with container access, through a publish |
| ID used | Google tag ID (G-, AW-, GT-) | Container ID (GTM-), which then loads the Google tag |
The two share the dataLayer, so a site can run both. The risk is counting things twice. Google warns, for example, that if you configure tags through Tag Manager you should not also enable automatic history tracking in Google Analytics, "as this can lead to double-counting page views." The same logic applies to a hard-coded gtag snippet and a GTM Google tag that both point at the same measurement ID: each one sends its own page_view. The Google Tag Manager privacy impact guide covers the other side of that choice, which is how much code a container lets other people add to your pages.

Flowsery
Start Your 14-Day Free Trial
Real-time dashboard
Goal tracking
Cookie-free tracking
Why do ad blockers stop gtag?
Ad blockers stop gtag by blocking the request for the library at www.googletagmanager.com or the hits it sends to Google's collection endpoints. When the script request fails, gtag() still exists, since the inline snippet defined it, but nothing ever reads the dataLayer queue, so no data leaves the page. The ad blockers and analytics accuracy guide explains how that gap shows up in reports. Google tag gateway for advertisers serves the tag from your own domain, though Google's gateway documentation does not mention ad blockers.
How do you check what gtag is sending?
You check what gtag sends by watching the network requests and Google's debugging tools, not by reading the snippet. Google's own guides use two: Tag Assistant, which shows the commands and the hits each one produced, and DebugView in Google Analytics, which shows events as they arrive once debug mode is on. In the browser, filter the Network panel for the collection requests and confirm that each pageview or event appears once, with the parameters you expect. The analytics verification checklist walks through the same checks for any tracker.
How does Flowsery's script compare with gtag?
Flowsery's tracker is one script tag with a website ID, so there is no command queue to configure before data flows. Paste <script defer data-fl-website-id="flid_******" src="https://cdn.flowsery.com/main.js"></script> into the head and pageviews are tracked automatically. Custom goals use a data-fs-goal attribute on the element or a call such as window.flowsery('signup'), and Flowsery's docs show a small queue stub, window.flowsery.q, that holds calls made before the script loads, the same pattern gtag uses with the dataLayer. The script also has a cookieless option that uses a daily-rotating visitor hash and stores no personal data. The lightweight script is under 10 KB and loads asynchronously. If your team already manages tags in Google Tag Manager, Flowsery's docs include a Custom HTML tag setup for GTM, so the two can sit side by side during a migration.
Frequently asked questions
Is gtag the same as Google Tag Manager?
No. gtag.js is the library behind the Google tag, and you call gtag() from your own page code. Google Tag Manager is a separate container, loaded through gtm.js, where tags are configured in a web interface. Both can deploy the same Google tag.
Can I call gtag() before gtag.js has loaded?
Yes. The inline snippet defines gtag() as a function that pushes its arguments into the dataLayer array. Calls made before the library arrives wait in that queue, and gtag.js processes them in order once it loads.
Can one gtag snippet send data to Google Analytics and Google Ads?
Yes. One Google tag can connect to several destinations, such as a Google Analytics property and a Google Ads account. A single tag can carry several tag IDs, and Google notes that those IDs can be used interchangeably when adding the tag to your site.
Why does gtag send a page_view without an event call?
The default config call for a Google Analytics ID sends a page_view event on its own. Set send_page_view: false if you want to send pageviews manually. On sites with enhanced measurement, history-based pageviews still fire unless you also turn off that option in the data stream.
Where should the gtag snippet go?
Google says to place it immediately after the opening <head> tag on every page you want to measure. An opt-out flag goes in a script before the snippet, since Google says it must be set before any gtag() call. Consent defaults go inside the snippet, before the config call, since Google says they must run before any command that sends measurement data.
Does gtag work if the script is blocked?
No data is sent. The inline part of the snippet still defines gtag() and fills the dataLayer, but without the gtag.js library nothing processes that queue. That is why blocked visitors are missing from Google Analytics rather than showing up as errors.
What is the difference between gtag.js, gtag() and the Google tag?
gtag.js is the library file that loads from googletagmanager.com. gtag() is the function your code calls to push commands into the dataLayer. The Google tag is Google's name for the whole setup, a single tag that covers products like Google Ads and Google Analytics.
Which tag ID formats can I use in the gtag snippet?
Google gives GT-XXXXXXXXX, G-XXXXXXXXX and AW-XXXXXXXXX as examples and says the IDs can be used interchangeably when adding the Google tag to your website. The ID goes into the id parameter of the script URL and into your config call. One Google tag can carry several IDs.
When should I use gtag('set')?
Use set for parameters that should be attached to every subsequent event on the page. Google recommends config or event wherever possible. Event values override config values, and config values override anything set globally. If you do use set, place it before config so the first hit already carries the values.
How do I turn off Google Analytics with a gtag opt-out flag?
Set window['ga-disable-MEASUREMENT_ID'] = true on the page. Google says it must be set before any calls to gtag() are made, and it must be set on each page for which you want to turn off Analytics.
Was This Article Helpful?
Let us know what you think!
See us more often in Google
One click marks Flowsery as a preferred source, so our articles sit higher in your Top Stories, AI Mode, and AI Overviews.
Before you go...
Flowsery
Revenue-first analytics for your website
Track every visitor, source, and conversion in real time. Simple, powerful, and cookie-free.
Real-time dashboard
Goal tracking
Cookie-free tracking
Related Glossary Terms


Setting GA4 User Properties and Reporting on Them
Set GA4 user properties with gtag or Tag Manager, register them as user-scoped custom dimensions and stay inside the 25-property limit.


Setting Up GA4 Content Grouping With One Parameter
Set up GA4 content grouping with the content_group parameter in gtag or Tag Manager, read the Content group dimension and keep (not set) rows out.


How a Google Analytics Opt Out Works, and What Each Method Blocks
A Google Analytics opt out can be a browser add-on, a ga-disable window flag or denied consent. What each one blocks, who controls it, and where it stops.


Why Your GA4 Bounce Rate Is Just Engagement Rate Flipped
Your GA4 bounce rate is the share of sessions that were not engaged. How Google defines it, how it differs from Universal Analytics, and where to add it.


What the GA4 Landing Page Report Counts and Where It Goes Blank
Google's GA4 landing page report lists the first page of every session. Where to find it, its default metrics, why (not set) rows appear and how to add source.


How GA4 Site Search Tracking Records What Visitors Type
Set up GA4 site search with enhanced measurement, add your own query parameters, find the Search term dimension and track searches without a URL.
Related Articles


How to Link Search Console to GA4 and Publish Its Reports
Learn how to link Search Console to GA4 in Admin, which roles you need, how to publish the two reports in Library and what the data can't join.


How to Build GA4 Calculated Metrics Inside a Five-Slot Quota
Google caps GA4 calculated metrics at 5 per standard property. Learn the formula syntax, the units, where they appear and what a formula can't use.


How to Add Google Analytics Annotations in GA4 and Keep a Change Log
In GA4, Google Analytics annotations pin a dated note to line graphs. Who can add them, the 60 and 150 character limits, the 1,000 cap, and change log use.

