How to Add Google Analytics Code in Google Tag Manager

Cody Schneider8 min read

Adding Google Analytics to your website shouldn't feel like a chore, but for many, it involves fumbling with website code. The cleanest and most powerful way to manage this is through Google Tag Manager (GTM). This approach not only installs Google Analytics 4 but also sets you up for much easier tracking management in the future. This article will walk you through a clear, step-by-step process to add your GA4 tracking code using GTM.

Why Use Google Tag Manager for Google Analytics?

While you can add the Google Analytics tracking script directly to your website's code, routing it through GTM is the industry-standard best practice for several important reasons. Think of GTM as a toolbox that holds all your tracking scripts (or "tags"). Instead of installing each tool individually on your site, you just install the toolbox (GTM) once.

Key Benefits:

  • Centralized Tag Management: Google Analytics is likely just one of many tools you use. With GTM, you can manage your GA4 tag, Facebook Pixel, Google Ads conversion tags, and others all from a single dashboard. This keeps your tracking organized and easy to maintain.
  • No More Constant Code Edits: Once GTM is installed, your marketing team can add, edit, or remove tracking tags without needing to ask a developer to edit the website code. This massively speeds up the process of launching new campaigns and tracking initiatives.
  • Simplified Event Tracking: Want to track who clicks on a specific button, downloads a PDF, or submits a form? Setting up this kind of detailed event tracking is vastly simpler with GTM's built-in triggers and variables compared to writing custom JavaScript for every interaction.
  • Built-in Testing and Debugging: GTM's powerful Preview mode lets you see exactly which tags are firing on your site in real-time before you publish them. This helps you catch errors and ensure your tracking is working correctly, preventing bad data from ever reaching your analytics reports.
  • Version Control: Every time you publish changes in GTM, a new version is saved. If you accidentally break something, you can quickly roll back to a previous, working version. This provides a safety net that hardcoding scripts just can't offer.
  • Potential Performance Gains: GTM loads your tracking scripts asynchronously. This means your tracking tags won’t block your main website content from loading, which can contribute to a faster perceived page load time for your visitors.

What You'll Need Before You Start

To make this process as smooth as possible, make sure you have the following ready to go. Don't worry, we'll walk through how to find what you need.

  • A Google Analytics 4 Property: You need an active GA4 property for your website. If you don't have one, you can create one for free inside the Google Analytics platform.
  • A Google Tag Manager (GTM) Account: You also need a GTM account and a "container" set up for your website. Like GA4, this is also a free tool from Google.
  • Access to Your Website's Code (One Time): You will need one-time access to either your website's header file or a plugin/setting that allows you to add scripts to the <head> of your site. This is only for installing the GTM container itself.

How to Find Your GA4 Measurement ID

The key piece of information we need from Google Analytics is the Measurement ID. This unique ID tells GTM which specific GA4 property to send data to.

  1. Log into your Google Analytics account.
  2. Click the Admin gear icon in the bottom-left corner.
  3. In the Property column, make sure your desired GA4 property is selected.
  4. Click on Data Streams, then select the data stream for your website.
  5. On the top right, you will see your Measurement ID, which always starts with "G-". Copy this ID to your clipboard, you will need it in the next section.

Step-by-Step Guide to Adding Google Analytics in GTM

With your Measurement ID copied, it's time to head over to Google Tag Manager and set everything up. The process involves creating two components: a "Tag" that holds your tracking code and a "Trigger" that tells the tag when to fire.

Step 1: Create a New Tag in Google Tag Manager

First, we'll create the main tag that loads the GA4 script and configures it for your site.

  1. Log into your Google Tag Manager account and select your website's container.
  2. Navigate to Tags from the left-hand menu and click the New button.
  3. Click on the Tag Configuration box to choose a tag type. From the list that appears, select Google Analytics: GA4 Configuration.
  4. In the Measurement ID field, paste the "G-..." ID you copied earlier from your Google Analytics account.
  5. It's best practice to give your tag a clear, descriptive name. For this one, a name like "GA4 - Configuration - All Pages" works well.
  6. Leave the box checked for "Send a page view event when this configuration loads." This setting automatically handles standard pageview tracking for you, which is the most fundamental part of web analytics.

Step 2: Set Up the Trigger

Now that the tag is ready, we need to tell GTM when to run it. For the main GA4 tag, we want it to load on every single page of the website as early as possible.

  1. Below the Tag Configuration box, click on the Triggering box.
  2. From the list of available triggers, select Initialization - All Pages.
  3. Click Save in the top-right corner to save your new tag and trigger combination.

Quick Tip: You might see an "All Pages" trigger and wonder what the difference is. The "Initialization" trigger is specifically designed to fire before all other types of triggers (except for consent-related ones). Using it for your GA4 configuration ensures Analytics is fully loaded before any other tracking GTM tags (like specific event tags) try to send data to it. This is considered the modern best practice.

Step 3: Test Your Setup with Preview Mode

Before making your new tag live, it's crucial to test it to make sure it's working as expected. GTM's Preview Mode is a fantastic tool for this.

  1. In the top-right of your GTM workspace, click the Preview button.
  2. A new browser tab will open. Enter your website’s URL and click Connect.
  3. Your website will load in another new tab, and you'll see a small "Tag Assistant Connected" badge in the corner. Go back to the Tag Assistant tab.
  4. On the summary screen in Tag Assistant, you should see your "GA4 - Configuration - All Pages" tag listed under the Tags Fired section. If you see it there, congratulations! It's working correctly. The tag has successfully loaded on your page.

Step 4: Submit and Publish a New Version

Once you’ve confirmed the tag is firing correctly in Preview mode, you're ready to make it live for all your website visitors.

  1. Go back to your GTM workspace and click the blue Submit button in the top-right corner.
  2. You'll be prompted to create a version. It's a great habit to give each version a descriptive name (e.g., "Added GA4 Base Tag") and add a brief description of the changes you made. This is incredibly helpful if you ever need to troubleshoot or revert your changes.
  3. Click Publish to push your changes live.

That's it! Your Google Analytics 4 tracking is now active on your website, managed entirely through Google Tag Manager.

Important: Remove Any Old Hardcoded Analytics Code

This is a step people often forget, and it can ruin your data. If you previously had Google Analytics installed by pasting a script (with either analytics.js, gtag.js, or a GA4 code snippet) directly into your website's HTML, you must remove it.

Leaving both the old hardcoded script and the new GTM-managed one in place means that every pageview will be counted twice. This will inflate your user and session counts and make your data highly inaccurate. Check your website's main template files (often header.php) or your site's header script injection settings for any old Google Analytics code and delete it.

Next Steps: Tracking Events

You’ve laid the foundation. The real power of combining GA4 and GTM comes from tracking specific user interactions called "events." Now that your configuration tag is in place, you can create new tags to track practically anything.

For example, to track a click on a "Request a Demo" button, your process would look like this:

  • Create a new Trigger: Set up a "Click - All Elements" trigger that only fires when the Click Text equals "Request a Demo."
  • Create a new Tag: Choose a "GA4 Event" tag type. Connect it to your existing configuration tag, give the event a name (like request_demo_click), and assign the new click trigger to it.

This modular approach allows you to build a comprehensive analytics setup that measures what truly matters to your business, all without ever touching your website's code again.

Final Thoughts

By using Google Tag Manager to install Google Analytics, you’ve not only enabled pageview tracking but also adopted a modern, flexible system for managing all your future marketing and analytics needs. This method gives you more control, allows for faster implementation of tracking, and keeps your website code clean.

Once you have all that rich data flowing into Google Analytics, the challenge shifts to analysis and reporting. At Graphed, we've built a solution to connect directly to data sources like Google Analytics, so you can transform that data into actionable insights instantly. Instead of getting stuck building reports, you can use plain English to create live dashboards and ask questions like, "what were my top 10 landing pages by sessions last month?" Graphed turns hours of manual analysis into a 30-second task, helping you find and act on insights faster than ever.

Related Articles

How to Connect Facebook to Google Data Studio: The Complete Guide for 2026

Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.

Appsflyer vs Mixpanel​: Complete 2026 Comparison Guide

The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.