Sub topic 7
Install the Google tag through Google Tag Manager
This chapter assumes you already have a GA4 property with a web data stream and know where to find its Measurement ID (the G-XXXXXXXXXX value). It also assumes you can edit your site's HTML, or ask someone who can, to add one container snippet. You should know what a script tag in the page <head> is.
What Google Tag Manager changes
Google Tag Manager (GTM) is a separate product from GA4. It hosts a container, which is a bundle of tags, triggers, and variables that you manage in a web interface. You paste the container snippet into your site once. After that, you add, change, and remove tags in GTM without editing the site's code again.
In the earlier chapters you pasted the Google tag directly into the page. With GTM, the page loads GTM, and GTM loads the Google tag. The result in GA4 is the same, because the Google tag still sends the same events to the same Measurement ID. What differs is where the tag lives and who controls it.
| Aspect | Manual gtag.js | Tag Manager |
|---|---|---|
| Code on the page | The Google tag snippet | The container snippet |
| Where you change tracking | In the site's code | In the GTM web interface |
| Publishing a change | Deploy the site | Publish a container version |
| Preview and debugging | DebugView, Tag Assistant | GTM Preview mode, plus DebugView |
| Best fit | One property, few changes | Many tags, several teams, frequent changes |
How the pieces connect
Step 1: Create a container
- Go to tagmanager.google.com and sign in with your Google account.
- Select Create Account. Use your company name as the account name.
- Name the container after the site, for example
example.com. - Set the target platform to Web and create the container.
- Accept the terms of service.
GTM then shows an install dialog with two snippets. The container ID looks like GTM-XXXXXXX. This is not your Measurement ID, and the two are easy to mix up. The container ID goes into the page snippet. The Measurement ID goes into the Google tag inside GTM.
Step 2: Add the container snippet to your site
GTM gives you two parts. The first is a script that goes as high in the <head> as possible. The second is a <noscript> iframe that goes immediately after the opening <body> tag. It supports visitors who have JavaScript disabled.
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
<!-- End Google Tag Manager -->
<title>Example page</title>
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<h1>Hello</h1>
</body>
</html>Replace both GTM-XXXXXXX values with your container ID. Use the exact snippet GTM shows you, since Google may update it.
Do not also paste the standalone gtag.js snippet from the earlier chapters on the same pages. That causes duplicate tagging, covered below.
Step 3: Create the Google tag in GTM
Inside the container:
- Open Tags and select New.
- Select Tag Configuration and choose Google tag. This is the current tag type for GA4 setup. Older tutorials mention "GA4 Configuration"; the Google tag has replaced that for new setups.
- In Tag ID, enter your Measurement ID, for example
G-XXXXXXXXXX. - Under Triggering, choose Initialization - All Pages.
- Name the tag clearly, for example
Google tag - GA4 - G-XXXXXXXXXX, and save.
Why the Initialization trigger
The Google tag sets up the connection to GA4 and applies settings such as consent and configuration parameters. Other tags, such as event tags you add later, depend on that setup being done first. The Initialization - All Pages trigger fires earlier than All Pages, so the setup is ready before other tags run. Using All Pages also works for simple sites, but Initialization is the safer default and is what Google's documentation recommends.
Setting configuration parameters
The Google tag in GTM has a Configuration settings section. This is where you put the same kind of values you would pass to gtag('config', ...) in a manual install. For example, to send debug events for a test, add a parameter:
| Parameter name | Value |
|---|---|
| `debug_mode` | `true` |
Remove it when testing ends. Leaving it on makes every visitor's events appear in DebugView and adds noise for you.
Step 4: Preview before you publish
Nothing you build in GTM affects your live site until you publish. Use that to test first.
- Select Preview at the top right of the workspace.
- Enter your site URL and connect. Tag Assistant opens and your site loads in a new tab with a debug connection.
- In the Tag Assistant window, look at the Summary. On the Initialization stage, your Google tag should be listed under Tags Fired.
- Select the tag to confirm the Tag ID is the Measurement ID you expected.
If the tag appears under Tags Not Fired, open it and check the Triggering section, which explains which condition did not match.
Step 5: Submit and publish
- Select Submit.
- Choose Publish and Create Version.
- Enter a version name and a description, for example
Add Google tag for GA4. - Select Publish.
Each published version is saved. If a later change breaks something, you can open Versions and publish an earlier one again. This rollback is a practical benefit over editing code on the page.
Step 6: Verify data in GA4
Publishing is not the same as data arriving. Confirm each stage:
- Open your site in a normal browser window, outside Preview mode.
- In GA4, open Reports > Realtime. Your visit should appear within a minute or so.
- For event-level detail, open Admin > DebugView. It shows events from devices that are in debug mode. Preview mode in GTM turns debug mode on for the session, so your preview traffic appears there.
- In the browser's developer tools, open the Network tab and filter for
collect. You should see requests to agoogle-analytics.com/g/collectURL containingtid=G-XXXXXXXXXX.
Avoiding duplicate tags
The most common GTM error is sending the same events twice. The page-view count roughly doubles, and engagement metrics become unreliable. Check for these causes:
- The page still contains a hard-coded
gtag.jssnippet with the same Measurement ID, and GTM also has a Google tag for it. - A CMS or e-commerce plugin adds its own GA4 tag, and you also installed GTM.
- Two Google tags in the same container point at the same Measurement ID, both firing on every page.
- The GTM container snippet is included twice, for example in both a theme file and a plugin.
To find them, use Tag Assistant on the live site without Preview mode. Add the site URL and look for more than one Google tag with the same ID. In the Network tab, a single page load should produce one page_view request for each Measurement ID. Then remove the extra source. Remove the older hard-coded snippet when GTM takes over, rather than disabling the GTM tag.
Consent and Tag Manager
If privacy rules apply to your visitors, the Google tag must know the consent state before it sends data. In GTM this is done with consent settings and a consent default, which mirrors the manual gtag('consent', 'default', ...) call. A consent management platform (CMP) template usually sets the default early and updates it when the visitor chooses.
The key rule is order. The default must be set before the Google tag fires. If your CMP loads late, the tag may send data before consent is recorded. Check this in Preview mode by looking at the Consent tab for each event in Tag Assistant.
Troubleshooting a GTM install
| Symptom | Likely cause | What to check |
|---|---|---|
| Nothing in Realtime | Container not published | **Versions** shows a live version; Submit was completed |
| Tag Assistant cannot connect | Snippet missing or blocked | View page source for `GTM-` ID; test without ad blockers |
| Tag listed under Not Fired | Trigger condition not met | The tag's Triggering section and the Preview details |
| Events appear under the wrong property | Wrong Measurement ID in the tag | Tag ID value against the data stream in GA4 |
| Page views are doubled | Duplicate tagging | A hard-coded gtag.js or plugin alongside GTM |
| Data only for some pages | Snippet missing from some templates | Source of pages that did not report |
| Data missing for some visitors | Consent denied, ad blockers, or privacy tools | Consent state and browser extensions |
Work from the outside in. First confirm the snippet is on the page, then the container is published, then the tag fires, then a collect request leaves the browser, and finally that GA4 shows the event.
Choosing GTM
Pick GTM when you expect to add more tags later, such as conversion tags, event tags for button clicks, or tags from other vendors. It also suits teams where marketers and developers share control, since GTM has user permissions and a version history. Prefer a direct snippet or a platform integration when the site is small, you only need default measurement, and nobody will manage a container. An unmanaged container is one more thing that can break without anyone noticing.
Exercise
A site has a published GTM container with a Google tag using the correct Measurement ID. After launch, Realtime shows each page view twice. The page source contains both the GTM snippet and a standalone gtag.js snippet with the same Measurement ID. What is the cause, what should you remove, and how do you confirm the fix?
Show answer
The cause is duplicate tagging: the hard-coded gtag.js snippet and the GTM Google tag both send a pageview to the same property. Remove the hard-coded gtag.js snippet from the page, since GTM now owns the tag and the container can be rolled back through versions. Confirm by loading the live page with the Network tab filtered for collect. You should see one pageview request for the Measurement ID per page load. Tag Assistant should list only one Google tag for that ID, and Realtime should show a single hit per visit.
Exercise
Why is the Google tag in GTM given the Initialization - All Pages trigger, and where do you enter the Measurement ID versus the container ID?
Show answer
Initialization - All Pages fires before the All Pages trigger, so the Google tag's setup and configuration, including consent, are ready before other tags run. The container ID (GTM-XXXXXXX) goes in the site's container snippet. The Measurement ID (G-XXXXXXXXXX) goes in the Tag ID field of the Google tag inside GTM.