Sub topic 3
What the Google tag is and what it does
Prerequisites
This chapter assumes you have already created a GA4 property and a web data stream, and that you can find the Measurement ID (the G-XXXXXXXXXX value). It also assumes you can read a basic HTML page and know what a <script> tag in the <head> does. You do not need to install anything yet. This chapter explains the piece of code you will install in the next chapters, so you can recognize it, edit it, and debug it.
The Google tag in one paragraph
The Google tag is a small piece of JavaScript on your page. It loads a library from Google, reads your configuration, and sends measurement data from the visitor's browser to Google's servers. GA4 stores that data under the Measurement ID you gave the tag. Without the tag, or something that does the same job, your property stays empty. The tag is the only link between your website and your property.
Google uses one tag for several products, including Google Analytics and Google Ads. You install the tag once and attach it to one or more destinations. A destination is a product account, identified by an ID such as a GA4 Measurement ID.
Related names that are easy to confuse
People use several terms for overlapping things. The table separates them.
| Term | What it is | Where you meet it |
|---|---|---|
| Google tag | The overall concept: the tag that sends data to Google products | GA4 Admin, Tag Assistant, Google's documentation |
| `gtag.js` | The JavaScript library file the tag loads from `googletagmanager.com` | The `src` of the script in your page |
| `gtag()` | A small function defined in your page that queues commands | Inline script and any custom event code |
| `dataLayer` | A JavaScript array that holds queued commands and data | `window.dataLayer` in the browser console |
| Google Tag Manager (GTM) | A separate product: a container that deploys tags without editing site code | A `GTM-XXXXXXX` container ID |
The practical distinction is between gtag.js and GTM. With gtag.js, the code is in your page and you edit the page to change behavior. With GTM, your page loads one container and you manage the tags in a web interface. A GTM container can deploy the Google tag too, and it is configured in GTM instead of in your HTML. A later chapter covers that route. The Google tag is the same idea in both cases.
Reading the standard snippet
This is the snippet GA4 gives you for a web data stream. You paste it as high in the <head> of every page as possible.
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>It has two script blocks. Here is what each part does.
- The external script. The first
<script>downloads thegtag.jslibrary. Theidin the URL tells Google which destination's configuration to fetch. Theasyncattribute lets the browser keep parsing the page while the library downloads, so the tag does not block rendering. window.dataLayer = window.dataLayer || [];This creates thedataLayerarray if it does not exist yet. The||keeps any existing array, which matters when other code has already created one.function gtag(){dataLayer.push(arguments);}This defines thegtag()function. It pushes its arguments ontodataLayer. Because the commands are queued, you can callgtag()before the library has finished loading. The library reads the queue when it arrives.gtag('js', new Date());This records when the tag started on this page.gtag('config', 'G-XXXXXXXXXX');This attaches the page to a destination and applies its settings. For a GA4 Measurement ID, it also triggers the firstpage_viewevent.
Each of the gtag() calls takes a command name first (js, config, event, set, or consent). The remaining arguments depend on the command.
Why `arguments` matters
The function pushes the arguments object, not an array of its parameters. The library recognizes commands in that specific form. If you "simplify" the function to dataLayer.push([...]), your commands may be silently ignored. Copy the function exactly as Google gives it.
What happens when a page loads
The sequence below shows how the pieces interact from the first request to the first event in GA4.
Two points follow from this order. First, because commands are queued, the order of your gtag() calls matters. A consent default must be queued before config, or the tag may already have sent data. Second, nothing reaches GA4 until the library has loaded and run. If the library is blocked or fails to load, the queue is never processed.
What the tag does for you
Identifies the visitor's browser
On first load, the tag sets first-party cookies on your domain. The _ga cookie holds a client ID, a random identifier for that browser. A second cookie named _ga_<container-id> (the container ID is derived from your Measurement ID) holds session state. GA4 uses the client ID to recognize returning users and the session data to group events into sessions. The cookies are why a visit from one browser stays connected across pages. They are also why consent rules affect the tag, which a later chapter covers.
Collects page and context data
With each event, the tag attaches information the browser already knows: the page URL, the page title, the referrer, the language, the screen size, and the browser and device. GA4 uses these values for reports such as traffic source and device category. You do not code any of this.
Sends events
An event is a single recorded interaction, such as page_view. GA4 is event-based, so everything the tag sends is an event with a name and optional parameters. The configuration call sends page_view automatically. The enhanced measurement feature, which you turn on or off in the data stream settings, adds events such as scrolls, outbound clicks, and file downloads. Those events come from the same tag, and the stream settings decide which are active. Enhanced measurement has its own chapter.
You can also send your own events from code using the event command:
gtag('event', 'sign_up', {
method: 'email'
});Here sign_up is the event name and method is a parameter. This only works on a page where the tag has already been installed and defined gtag().
Applies settings and manages destinations
The tag also has settings that apply to the whole tag, not to a single event. Examples include whether a config call sends page_view automatically, which domains count as the same site, and which internal traffic to exclude. Many are managed in the GA4 interface under the data stream's Configure tag settings, so you can change them without editing your pages. Others are set as parameters on config.
One tag can serve several destinations. For example, you might add a second config call for a Google Ads account.
gtag('config', 'G-XXXXXXXXXX');
gtag('config', 'AW-XXXXXXXXX');Both calls use the same gtag() function and queue. You load the library once, with the first ID in the URL, and add the second ID with another config call. This is different from pasting the whole snippet twice, which is a common cause of duplicate data (see the duplicate tags chapter).
What the tag does not do
The tag collects data from the browser. It does not clean, filter, or report on that data. Several things happen later or elsewhere.
- Processing and reporting. GA4 applies data filters, channel grouping, and attribution after the data arrives. A bug in a report is rarely a bug in the tag.
- Server-side collection. The tag only runs in the browser. Purchases that happen on your server, or in an app, need another method, such as the Measurement Protocol or an app SDK.
- Overriding the visitor. If the visitor blocks scripts or cookies, or the browser limits storage, the tag cannot send what it cannot collect. No setup fully avoids this loss, so expect GA4 numbers to be an estimate and not a perfect count.
- Consent decisions. The tag respects consent state when you set it. It does not decide what the law requires of you.
Common errors to recognize now
These problems all come from misunderstanding what the tag is. Later chapters give detailed fixes.
- The wrong ID. Using a Universal Analytics ID (
UA-) or a Google Ads ID where a GA4 Measurement ID (G-) belongs. The tag loads but the data lands nowhere you expect. - The snippet in the body or on only some pages. The tag only measures pages that include it. A template that skips the checkout page means no checkout data.
gtag()called before it is defined. Custom event code that runs earlier in the page than the snippet throwsgtag is not defined. Put the snippet first, or definegtag()in your own earlier code.- Two copies of the tag. A hard-coded snippet plus a plugin or GTM container each sending the same
page_viewdouble counts. Treat "one tag per destination per page" as the rule. - Changing the snippet for no reason. The snippet is the same for every site except the ID. Leave the rest alone.
Check your understanding
Exercise
A developer removes function gtag(){dataLayer.push(arguments);} from the snippet and replaces it with window.gtag = (...args) => dataLayer.push(args);. Pages still load without errors, but no data appears in GA4. What is the likely cause?
Show answer
The arrow function pushes an ordinary array of parameters, but the library recognizes commands only as the arguments object that the original function pushes. The library therefore ignores the queued commands, so nothing is sent and no error appears. The fix is to restore function gtag(){dataLayer.push(arguments);} exactly.
Exercise
Your page includes the standard Google tag with a GA4 Measurement ID. You also want to send data to a Google Ads account (AW-XXXXXXXXX). Which approach avoids duplicate data: pasting the full snippet a second time with the Ads ID, or adding another call? Write the call.
Show answer
Add another call and keep the single snippet: gtag('config', 'AW-XXXXXXXXX');. The library loads once and the same gtag() function and dataLayer queue serve both destinations. Pasting the full snippet twice loads the library twice and risks duplicate events.
Exercise
Name the part of the snippet that triggers the first page_view, and explain why a consent default must come before it.
Show answer
The gtag('config', 'G-XXXXXXXXXX'); call triggers the first page_view. Commands run in the order they were queued, so a consent default placed after config would arrive after the tag had already started collecting. The default must be queued first so the tag respects it from the first page load.