How to Add Google Analytics 4 to WordPress

Cody Schneider8 min read

Connecting your WordPress site to Google Analytics 4 is one of the first and most important steps in understanding your audience and measuring your website's performance. This guide will walk you through the simplest and most effective methods to get GA4 up and running, whether you prefer using a plugin or want to add the code manually.

First, Find Your GA4 Measurement ID

Before you can use any of the methods below, you need a crucial piece of information: your Google Analytics 4 "Measurement ID." This unique ID tells Google which data stream to send the tracking information to. Think of it as the mailing address for your website's data.

Here’s how to find it:

  1. Log in to your Google Analytics account.
  2. If you have multiple GA accounts, make sure you've selected the correct one.
  3. Click on the Admin gear icon in the bottom-left corner of the screen.
  4. In the Property column, ensure you have the correct GA4 property selected. Then, click on Data Streams.
  5. You should see your website listed as a web data stream. Click on it.
  6. A new panel will open showing your stream details. In the top-right corner, you'll see your Measurement ID, which starts with "G-" followed by a string of characters (e.g., G-XYZ123ABC).

Copy this ID and keep it handy. You’ll need it no matter which installation method you choose.

Method 1: Using a Plugin (The Easiest Way for Beginners)

For most WordPress users, the quickest and safest way to add GA4 is through a plugin. You won’t have to touch a single line of code, and you reduce the risk of accidentally breaking your site. Plugins often come with bonus features, like displaying basic analytics reports directly inside your WordPress dashboard.

Our recommendation for a straightforward setup is the official Site Kit by Google plugin.

Step-by-Step: Adding GA4 with Site Kit by Google

Site Kit is Google's own plugin for WordPress, designed to connect your site to various Google services like Analytics, Search Console, and AdSense. It’s free, secure, and handles the setup process for you.

1. Install and Activate the Plugin

  • From your WordPress dashboard, navigate to Plugins > Add New.
  • In the search bar, type "Site Kit by Google".
  • Wait for the search results to appear. When you see the plugin, click Install Now.
  • Once the installation is complete, the button will change to Activate. Click it.

2. Run the Setup Wizard

  • After activation, you’ll see a banner prompting you to start the setup. Click the "Start Setup" button.
  • The wizard will guide you through connecting your Google account. Click "Sign in with Google."
  • Choose the Google account that you use for Google Analytics.
  • Google will ask for several permissions to allow Site Kit to view and manage your data. Grant these permissions to continue. This step is secure, it's simply giving the plugin on your site the ability to talk to the Google services on your account.
  • Site Kit will then verify that you own the website, usually by placing a small verification tag in your site's code automatically.

3. Connect Your Analytics Service

  • Once verified, Site Kit will prompt you to connect services. It will likely find your Google Analytics account automatically.
  • Ensure the correct GA4 Property and Web Data Stream are selected from the dropdown menus. If you only have a GA4 property for this site, Site Kit will usually select it by default.
  • Click "Configure Analytics."

And that’s it! Site Kit has now placed the necessary GA4 tracking code on every page of your WordPress site. The best part is that it will keep the code updated for you, and you can now see a simplified overview of your traffic directly within your WordPress admin area.

Method 2: Manually Adding the GA4 Tracking Code

If you prefer to limit the number of plugins on your site or are comfortable with making some technical modifications, you can add the GA4 tracking code manually. This method gives you full control, but it's important to do it correctly to avoid issues with theme updates.

First, you need to grab the full tracking script, not just the Measurement ID.

  1. Go back to your GA4 account's Admin > Data Streams > [Your Web Stream].
  2. Scroll down and click on View tag instructions.
  3. Under the "Install your Google tag" screen, select the Install manually tab.
  4. You will see a block of JavaScript code. This is your Global Site Tag (gtag.js). Copy the entire script. It should look like this:
<!-- Google tag (gtag.js) -->
<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>

Now, you need to place this code in the <head> section of your website's HTML on every page. Here are three safe ways to do that.

A word of warning: Avoid directly editing your theme's header.php file. While it seems like an easy fix, any changes you make will be completely erased the next time you update your theme, and your tracking will stop.

Option A: Use Your Theme's Header/Scripts Feature

Many modern WordPress themes come with a built-in feature to add scripts to your header or footer. This is the safest manual option as it doesn't require extra plugins or direct code edits.

  • In your WordPress dashboard, navigate to Appearance > Customize.
  • Look for a section named "Custom Scripts," "Header/Footer Scripts," "Code Injection," or something similar. The exact name and location will vary depending on your theme (e.g., Astra, GeneratePress, and Kadence all have this feature).
  • Find the box for adding scripts to the header (<head>).
  • Paste the entire GA4 Global Site Tag you copied earlier into this box.
  • Click Publish or Save to make the changes live.

Option B: Use a Code Snippets Plugin

If your theme doesn't have a built-in script manager, a lightweight code snippets plugin is the next best choice. It gives you a dedicated place to add scripts without risk of them being deleted by theme updates.

  • Install and activate the WPCode Lite - Insert Headers and Footers plugin.
  • From your dashboard, go to Code Snippets > Header & Footer.
  • You’ll see a few boxes. In the box labeled "Header," paste your entire GA4 Global Site Tag.
  • Click Save Changes at the top of the page.

This plugin isolates your custom code from your theme, making it a reliable and future-proof way to manage scripts.

Option C: Use a Child Theme (The Developer Method)

For those who are comfortable with PHP and WordPress development, the "correct" way to add custom code is via a child theme's functions.php file. A child theme inherits all the functionality and styling of its "parent" theme, but it lets you make your own modifications without touching the parent theme's files. That way, when the parent theme is updated, your custom code remains safe.

Many themes offer a one-click child theme generator, or you can use a plugin to create one.

Once your child theme is created and activated, follow these steps:

  1. Navigate to Appearance > Theme File Editor.
  2. On the right side, under "Theme Files," make sure your child theme is selected and click on the functions.php file to open it.
  3. Add the following PHP code to the very bottom of the file:
add_action('wp_head', 'my_custom_add_ga4_code'),
function my_custom_add_ga4_code() {
?>
<!-- Google tag (gtag.js) -->
<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
}

Important: Remember to replace both instances of G-YOUR_MEASUREMENT_ID with your actual Measurement ID.

This code hooks into WordPress's wp_head action, which programmatically prints the GA4 script inside the <head> HTML tag on every page load.

How to Verify Your GA4 Installation Is Working

Once you've implemented one of the methods above, don't just assume it's working. You should always verify that Google is receiving data from your site.

Check the Realtime Report

This is the quick and definitive way to see if your tag is firing.

  1. Open your WordPress site in a new browser tab or on your phone (make sure you aren't logged in, or use an incognito window, to ensure your visit is not filtered out).
  2. In Google Analytics, go to Reports > Realtime.
  3. Within a minute or two, you should see at least "1" in the "Users in last 30 minutes" card. You can also see your visit show up on the map and in other cards.

If you see your activity, congratulations! Your GA4 installation is successful and is actively collecting data.

Check Page Source

You can also quickly confirm that the script has been added to your site's HTML.

  • Visit any page on your website.
  • Right-click on the page and select "View Page Source".
  • Use your browser’s find function (Ctrl + F or Cmd + F) and search for your Measurement ID (e.g., "G-XYZ123ABC") or "gtag.js".
  • If you find the Google tag script on the page, the code has been successfully added.

Final Thoughts

Getting Google Analytics 4 connected to your WordPress site is a critical setup for any website owner. Whether you opt for the simplicity of a plugin like Site Kit or the control of adding the code manually, the process is straightforward with the right steps. Now you're ready to start collecting the data that will help you grow your audience and improve your website.

Of course, once your data starts pouring in, the new challenge becomes making sense of it all. Instead of spending hours digging through GA4 reports or wrangling spreadsheets, we built Graphed to do the heavy lifting for you. We connect directly to your Google Analytics account and other data sources, letting you create dashboards and get insights just by asking questions in plain English. This way, you can get back to focusing on strategy instead of struggling with report-building.

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.