WordPress

How to Add Google Analytics 4 to WordPress Without a Plugin

How to Add Google Analytics 4 to WordPress Without a Plugin

Introduction

Google Analytics 4 (GA4) is the latest version of Google’s web analytics platform, offering deeper insights and event-based tracking. While plugins make integration easy, many users prefer a lightweight site with no extra plugins.

In this guide, you’ll learn how to add GA4 tracking code to your WordPress site manually — no plugin required.

Step 1: Create a Google Analytics 4 Property

  1. Go to analytics.google.com and sign in
  2. Click Admin (gear icon in bottom left)
  3. Under “Account,” select or create your account
  4. Under “Property,” click Create Property
  5. Enter your website name, timezone, and currency
  6. Select Web as your data stream type
  7. Enter your website URL and name the stream
  8. Copy the Measurement ID (it starts with G-XXXXXXX)

Step 2: Copy the Global Site Tag (gtag.js)

Once your property is created, you’ll be provided with a global site tag (gtag.js). It will look like this:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-XXXXXXX');
</script>

Copy the entire block. Replace G-XXXXXXX with your actual Measurement ID.

Step 3: Paste Code Into Your WordPress Header

To add GA4 manually, you’ll need to paste the code into your site’s header:

  1. Log in to your WordPress dashboard
  2. Go to Appearance > Theme File Editor
  3. Select header.php under your active theme
  4. Paste the tracking code just before the closing </head> tag
  5. Click Update File to save your changes

Important: If you’re using a child theme (recommended), edit header.php in the child theme to preserve changes during theme updates.

Alternative Method: Add Code via functions.php

If you prefer not to edit header.php, you can inject the tracking code via functions.php:


function add_ga4_tracking_code() {
  ?>
  &lt;script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"&gt;&lt;/script&gt;
  &lt;script&gt;
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'G-XXXXXXX');
  &lt;/script&gt;
  &lt;?php
}
add_action('wp_head', 'add_ga4_tracking_code');

Replace G-XXXXXXX with your own GA4 Measurement ID.

Step 4: Verify That It’s Working

To confirm GA4 is active:

  • Visit your site in an incognito browser tab
  • Go to Google Analytics
  • Click Realtime on the left sidebar
  • You should see active users if your code is working

Bonus Tips

  • Use the Chrome extension Tag Assistant to troubleshoot tracking issues
  • Set up custom events and conversions inside your GA4 dashboard for deeper tracking

Conclusion

Adding Google Analytics 4 manually to WordPress gives you full control without bloating your site with plugins. Whether you’re managing a personal blog or a WooCommerce store, GA4 provides the data you need to grow — and now you’ve got it installed!