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
- Go to analytics.google.com and sign in
- Click Admin (gear icon in bottom left)
- Under “Account,” select or create your account
- Under “Property,” click Create Property
- Enter your website name, timezone, and currency
- Select Web as your data stream type
- Enter your website URL and name the stream
- 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:
- Log in to your WordPress dashboard
- Go to Appearance > Theme File Editor
- Select
header.php
under your active theme - Paste the tracking code just before the closing
</head>
tag - 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() {
?>
<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>
<?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!