ThisCalculator

Everyday

Add Google Analytics 4 to WordPress with GTM

Updated · Standard formula, shown below

This guide walks you through adding Google Analytics 4 (GA4) to your WordPress site using Google Tag Manager (GTM). You’ll set up a GA4 property, create a GTM container, add the GA4 configuration tag, and publish it—all without editing your theme’s code. This method keeps your tracking tags in one place, making future changes easier and faster.

What You’ll Need

  • A WordPress site with admin access (you’ll need to install a plugin if you don’t have a theme that supports code snippets).
  • A Google account (for both Analytics and Tag Manager).
  • Ability to edit your WordPress theme’s header.php file OR install a plugin like “Insert Headers and Footers” or “WPCode” (recommended for safety).

Step-by-Step

  1. Create a GA4 property

    • Go to analytics.google.com and sign in with your Google account.
    • Click Admin (gear icon) in the bottom-left corner.
    • In the Property column, click Create Property.
    • Enter a name (e.g., “My WordPress Site”), select your reporting time zone and currency, then click Next.
    • Choose your industry category and business size, then click Create.
    • Follow the prompts to add a data stream: select Web, enter your site URL and name, then click Create stream.
    • Copy the Measurement ID (starts with G-). You’ll need it later.
  2. Create a Google Tag Manager container

    • Go to tagmanager.google.com and sign in.
    • Click Create Account (or use an existing account). Enter an account name (e.g., “My Website”).
    • In Container name, enter something like “WordPress Site”. For Target platform, select Web.
    • Click Create and accept the terms of service.
    • You’ll see a popup with two code snippets: one for the <head> and one for the <body>. Copy both—you’ll need them in the next step.
  3. Install GTM on your WordPress site

    • If you’re using a plugin like WPCode, go to Code Snippets → Add Snippet → Add Your Custom Code.
    • Paste the first GTM snippet (the one for <head>) into a new snippet, set the location to Site Wide Header, and activate it.
    • Create another snippet for the second GTM snippet (the one for <body>) and set the location to Site Wide Body.
    • If you’re editing your theme directly, open header.php in your theme editor and paste the head snippet right after the opening <head> tag. Then paste the body snippet right after the opening <body> tag. Save the file.
  4. Add the GA4 configuration tag in GTM

    • Go back to your GTM workspace. Click Tags in the left menu, then New.
    • Name the tag (e.g., “GA4 Configuration”).
    • Click Tag Configuration and choose Google Analytics: GA4 Configuration.
    • Paste your GA4 Measurement ID (the G- code) into the Measurement ID field.
    • Leave the other settings as default for now. Click Triggering and choose All Pages.
    • Click Save.
  5. Publish your GTM container

    • In GTM, click Submit in the top-right corner.
    • Enter a version name (e.g., “Initial GA4 setup”) and a description if you like.
    • Click Publish.
  6. Verify GA4 is working

    • Open your WordPress site in a new browser tab.
    • In GA4, go to Reports → Realtime (or Realtime from the left menu).
    • You should see your own visit appear within a few seconds. If you don’t, see the Troubleshooting section below.

Common Mistakes

  • Using the wrong Measurement ID: Double-check that you copied the G- code from GA4, not the old UA- code from Universal Analytics.
  • Adding GTM to both header and body incorrectly: The head snippet must go in the <head>, and the body snippet must go right after the opening <body> tag. Putting them in the wrong places can break the tag.
  • Forgetting to publish the GTM container: You can save tags without publishing, but they won’t go live until you click Submit and then Publish.
  • Using a caching plugin without purging: If you have a caching plugin (like WP Rocket or W3 Total Cache), clear the cache after adding GTM so the snippets appear on your live site.
  • Testing while logged in as admin: GA4 by default excludes traffic from logged-in admins if you’ve enabled that setting. Check your GA4 data filters or test in an incognito window.

Tips

  • Use a child theme if you’re editing header.php directly, so theme updates don’t wipe your changes.
  • Use GTM’s Preview mode (the Preview button in GTM) to test your tags before publishing. It shows you which tags fire on your page and lets you debug.
  • Add an event tag for form submissions or outbound links later—GTM makes it easy without touching code again.
  • Keep GTM container versions documented so you can roll back if something breaks.

Troubleshooting

No data in GA4 Realtime

  • Clear your WordPress cache and browser cache.
  • Use GTM Preview mode to see if the GA4 tag fires. If it doesn’t, check that the trigger is set to All Pages.
  • Make sure you’ve published the container after saving.
  • Check that you’re looking at the correct GA4 property and data stream.

GTM snippet not appearing in page source

  • If using a plugin, confirm the snippet is active and the location is correct.
  • If editing the theme, check that you saved the file and that the theme is not using a cached version.
  • Try a different browser or incognito window to rule out caching.

GTM shows “Container not found”

  • This usually means the GTM ID in the snippet is wrong. Copy the exact GTM-XXXX code from your container’s installation instructions.

GA4 shows pageviews but no events

  • Events like scroll depth or clicks need separate tags. The configuration tag only sends pageviews. Add event tags as needed.

Frequently Asked Questions

Do I need both GA4 and GTM?

No, you can add GA4 directly to WordPress without GTM by pasting the GA4 snippet into your theme. GTM is recommended if you plan to add many tracking tags or want to manage them without editing code each time.

Will this slow down my site?

GTM and GA4 add a small amount of JavaScript. The impact is minimal if you keep tags lean and use asynchronous loading (which GTM does by default).

Can I use GTM to track conversions?

Yes, you can create conversion events in GA4 and then use GTM to fire tags based on those events (e.g., form submissions, button clicks). You’ll need to set up event triggers in GTM.

What if I already have Universal Analytics installed?

You can run both UA and GA4 simultaneously. Just add the GA4 tag in GTM alongside your existing UA tag. Google will eventually sunset UA, so it’s wise to migrate fully to GA4.

How do I exclude my own visits from GA4?

In GA4, go to Admin → Data Streams → your web stream → Configure tag settings → Show all → Define internal traffic. Add your IP address or a rule to exclude internal traffic.

Why do my pageview numbers seem low?

GA4 uses a different counting method than Universal Analytics. It counts pageviews per page load, but if you have a single-page app or use client-side routing, you may need to configure enhanced measurement for accurate counts.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.