How to Update Google Analytics Code in WordPress

Cody Schneider••9 min read

Switching your Google Analytics code in WordPress can feel a bit like performing surgery on your website. Whether you're finally migrating to GA4, fixing a broken tracking setup, or upgrading to Google Tag Manager, the process requires care. This guide will walk you through the safest and most effective methods to update your GA code, so you can be confident your data is accurate without breaking your site in the process.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Why You Might Need to Update Your GA Code

Most website owners don’t touch their tracking code unless something forces them to. If you’ve found your way here, it’s likely for one of these key reasons:

  • The GA4 Migration: The biggest reason by far is the transition from Universal Analytics (UA) to Google Analytics 4. Old "UA-XXXX" tracking IDs and their associated analytics.js code are obsolete. To continue collecting data, you must replace them with the new "G-XXXX" Measurement ID and the new gtag.js script.
  • Fixing an Error: Sometimes, the original tracking code was installed incorrectly. You might notice duplicate pageviews, data not showing up at all, or a notice from Google that your tag is not firing. An update is really a re-installation to fix the initial problem.
  • Switching to Google Tag Manager (GTM): Many growing businesses move from putting GA code directly on their site to managing it through Google Tag Manager. GTM acts as a container for all your marketing and analytics scripts, making it far easier to add or update tracking code in the future without ever touching your WordPress site's code again. This requires removing the old GA code and installing the GTM code instead.
  • Changing to a new GA Property: You might create a new Google Analytics property for a site redesign, a business pivot, or just to start fresh with clean data. This requires swapping the old property’s tracking ID for the new one.

Before You Begin: Back Up Your Site and Find Your New Code

Before making any changes, especially ones that involve code, always back up your WordPress site. A simple mistake can cause a "white screen of death" or other site-breaking errors. Use a trusted backup plugin like UpdraftPlus or ask your hosting provider about creating a one-click backup. It only takes a minute and can save you hours of panic.

Next, you’ll need the new code or ID you're adding. Go to your Google Analytics 4 property and find this information:

  1. Log into Google Analytics and go to the Admin section (the gear icon in the bottom-left).
  2. In the Property column, select your GA4 property.
  3. Click on Data Streams and then select the appropriate web stream for your site.
  4. Your Measurement ID (e.g., "G-XXXXXXXXXX") will be in the top right. If you're a plugin user, this ID is all you'll need.
  5. If you are manually adding code, scroll down to "View tag instructions." Under the "Install manually" tab, you'll find the full JavaScript snippet, also known as the Global Site Tag (gtag.js).

With a backup ready and your new code in hand, you're ready to proceed.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Method 1: Updating Through a WordPress Plugin (The Easiest Way)

Using a dedicated plugin is the simplest and safest way for most WordPress users to manage Google Analytics. It keeps you from having to look at a single line of code. If you're already using a plugin like Site Kit by Google, MonsterInsights, or another analytics plugin, updating is usually as simple as swapping one ID for another.

Let's use a standard analytics plugin as an example:

  1. In your WordPress dashboard, navigate to the settings page for your analytics plugin. This is often under Settings > [Plugin Name] or its own top-level menu item.
  2. Look for a field labeled "Tracking ID," "Measurement ID," or something similar. You will likely see your old "UA-XXXXXX-X" ID sitting there.
  3. Delete the old Universal Analytics ID.
  4. Copy your new GA4 Measurement ID ("G-XXXXXXXXXX") from your Google Analytics account.
  5. Paste the new GA4 Measurement ID into the field in your plugin’s settings.
  6. Scroll down and click "Save Changes" or "Update Settings."

That's it! The plugin will handle removing the old script and adding the new one in the right place on your website. This method avoids the risk of code errors and ensures the tracking code is properly formatted.

Method 2: Using the functions.php File (For Advanced Users)

If you prefer to keep plugins to a minimum and are comfortable with PHP, you can add the tracking snippet directly to your theme's functions.php file. This method programmatically hooks your GA4 code into your site's <head> section.

A strong word of caution: Editing functions.php directly is risky. A single misplaced comma or typo can bring down your entire site. For this reason, you should only do this using a child theme. If you edit the main theme's functions.php file, your changes will be completely erased the next time your theme developer releases an update.

Here’s how to do it safely with a child theme:

  1. From your WordPress dashboard, go to Appearance > Theme File Editor.
  2. Be sure you have your child theme selected from the dropdown menu in the top right corner.
  3. In the file list on the right, click on Theme Functions (functions.php).
  4. First, look for any existing functions adding an old analytics.js script. If you find one, carefully delete that entire block of PHP code.
  5. Scroll to the bottom of the file and paste in the following code. Make sure to replace YOUR_MEASUREMENT_ID with your own GA4 Measurement ID.
add_action('wp_head', 'add_ga4_tracking_code'),
function add_ga4_tracking_code() { ?> 
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOUR_MEASUREMENT_ID"></script>
<script>
    window.dataLayer = window.dataLayer || [],
    function gtag(){dataLayer.push(arguments),}
    gtag('js', new Date()),

    gtag('config', 'G-YOUR_MEASUREMENT_ID'),
</script>
<?php }
  1. Click "Update File" to save your changes.

This add_action hook tells WordPress to run your function and inject the GA4 script right before the closing </head> tag on every page of your site.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Method 3: Switching to Google Tag Manager (The Smartest Way Forward)

If you manage more than just a Google Analytics tag (like a Facebook Pixel, LinkedIn Insight Tag, etc.), this update is the perfect opportunity to implement Google Tag Manager (GTM). GTM acts as a single container for all your scripts. You install a single GTM snippet on your site, and then manage all your other tags from the GTM interface. It declutters your site and makes future updates almost instant.

Part 1: Update Your WordPress Site

  1. Completely remove your old Google Analytics code. Check your plugins, functions.php, and header.php to make sure all previous tracking snippets are gone. Having multiple GA tags will lead to inflated and incorrect data.
  2. Install and activate a GTM plugin like “GTM4WP” by Thomas Geiger. This is the easiest way to add the container script correctly.
  3. Open your Google Tag Manager account and copy your Container ID (e.g., "GTM-XXXXXXX").
  4. Go to the plugin's settings in WordPress, paste your Container ID, and save. The plugin will place the necessary code snippets in your site's <head> and <body> sections automatically.

Part 2: Add Your GA4 Tag Inside GTM

  1. In your GTM Workspace, navigate to Tags and click "New".
  2. Give your tag a descriptive name, like "GA4 - Configuration Tag".
  3. Click on Tag Configuration and choose "Google Analytics: GA4 Configuration" from the list.
  4. In the Measurement ID field, paste your GA4 Measurement ID ("G-XXXXXXXXXX").
  5. Next, click on Triggering. Select the "Initialization - All Pages" trigger. This ensures your GA code loads early on every page of your website.
  6. Click "Save" on the tag configuration.
  7. Finally, in the top-right corner of your GTM workspace, click "Submit", give your version a name (e.g., "Added GA4 Tag"), and then click "Publish".

Your GA4 tag is now live! The major benefit is that you will never need to touch WordPress code to update Analytics again, all future changes can happen directly within GTM.

How to Verify Your New Google Analytics Code is Working

After you’ve updated your code, you must confirm that it’s working. Don't just assume it is! Here are two quick ways to check.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

1. Use GA4's Real-Time Report

This is the most straightforward method.

  1. Log into your Google Analytics account.
  2. In the left-hand menu, navigate to Reports > Real-Time.
  3. Open a new incognito or private window in your browser and visit your own website. Using an incognito window helps ensure you're not logged into WordPress, which could block tracking.
  4. Click around on a few pages.
  5. Look back at the Real-Time report in GA4. Within about 30-60 seconds, you should see your visit appear in the "Users in Last 30 Minutes" card and on the world map. If you see activity, your base tag has been installed correctly.

2. Use the Tag Assistant Legacy Chrome Extension

For a slightly more technical check, Google’s own browser extension is perfect.

  1. Install the Tag Assistant Legacy (by Google) extension from the Chrome Web Store.
  2. Navigate to your website.
  3. Click the Tag Assistant icon in your browser's toolbar and click Enable.
  4. Reload the page.
  5. Click the Tag Assistant icon again. It should show the tags it found. You are looking for a “Global site tag (gtag.js)” tag associated with your GA4 Measurement ID. A green or blue tag icon indicates it’s firing without issues. A red or yellow icon means there’s a problem with the implementation that needs further investigation.

Final Thoughts

Updating your Google Analytics code in WordPress involves carefully removing the old snippet and inserting the new one using the method that best fits your technical skill set. Whether you use a simple plugin, edit a child theme's functions.php file, or take the opportunity to adopt Google Tag Manager, the most important step is always to verify that your new tag is firing and collecting data.

Of course, getting clean data into Google Analytics is just the first step - making sense of it is the real challenge. The GA4 interface can be confusing and cumbersome. At Graphed, we’ve made it our mission to make analytics easy for everyone. After connecting your Google Analytics account, you can build real-time dashboards and reports using simple, natural language. Instead of spending hours digging, you can just ask questions like "Which campaign brought in the most revenue last month?" and get an instant visualization. We help you turn your hard-won data into actual insights that can grow your business, letting you focus on the "what," not the "how."

https://www.graphed.com/register

Related Articles