Most tutorials for adding Google Analytics to WordPress point you straight to a plugin. That works fine, but it adds a dependency you do not necessarily need. The manual method — adding the GA4 tracking code directly to your theme — is straightforward, keeps your plugin stack lean, and gives you complete control over when and how the script loads.
This guide walks through the full setup: creating a GA4 property, finding your Measurement ID, adding the gtag.js snippet to WordPress, and confirming data is flowing through. No plugin required at any stage.
What You Need Before You Start
Before adding any code, you need a Google Analytics 4 property with a Measurement ID. If you have not set one up yet, go to analytics.google.com, sign in with your Google account, and create a new property. During setup, select Web as your platform and enter your site URL. Once the property is created, Google will show you the Measurement ID — it looks like G-XXXXXXXXXX. Keep that ID to hand.
You will also need access to your WordPress theme files, either through Appearance → Theme File Editor or via FTP. The code goes into your theme’s functions.php file. If you are using a child theme, add it there rather than in the parent theme.
Why Skip the Plugin
Analytics plugins do the same job — they inject the gtag.js script into your pages. But they also add overhead: database queries, admin UI, and sometimes additional scripts you did not ask for. For a simple GA4 implementation, a few lines in functions.php accomplish exactly the same thing without the extra weight.
The manual method also makes it easier to control where the script fires. You can exclude logged-in users, restrict tracking to specific post types, or adjust the script loading order — all without fighting a plugin’s settings panel. If you are already managing custom code in functions.php, adding GA4 there keeps everything in one place.
That said, if you prefer a plugin-based approach or want to connect Search Console and PageSpeed Insights alongside Analytics, Google Site Kit handles all of that in a single install.
How to Add GA4 to WordPress Without a Plugin
The tracking code needs to load on every page of your site, as early in the <head> as possible. WordPress provides a hook — wp_head — that fires inside the <head> tag on every page load. That is where you attach the GA4 snippet.
- In your WordPress dashboard, go to Appearance → Theme File Editor.
- Open functions.php from the file list on the right.
- Scroll to the bottom of the file and add the following code, replacing
G-XXXXXXXXXXwith your actual Measurement ID:
function veravix_add_ga4() {
?>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
<?php
}
add_action('wp_head', 'veravix_add_ga4');
- Click Update File to save.
- Open your site in a browser, then check the Google Analytics Realtime report to confirm your visit is being recorded.
The async attribute on the first script tag means GA4 loads without blocking your page render. This is the standard implementation recommended in Google’s developer documentation.
Excluding Logged-In Users from Tracking
If you are regularly logged into your own site, your admin visits will inflate your traffic data. A small adjustment to the function prevents that:
function veravix_add_ga4() {
if ( is_user_logged_in() ) return;
?>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
<?php
}
add_action('wp_head', 'veravix_add_ga4');
The is_user_logged_in() check exits the function early if WordPress detects an active login session, so the tracking script never loads for logged-in users. I use this on most sites I build — it keeps the analytics clean from day one.
Verifying Your GA4 Installation
After saving the code, open a browser tab where you are not logged into WordPress and visit your site. Then, in Google Analytics, go to Reports → Realtime. You should see one active user — yourself — appearing within 30 seconds.
If nothing appears, check these things first:
- Confirm the Measurement ID in the code matches the one in your GA4 property settings exactly.
- Make sure you saved
functions.phpsuccessfully — reload the file in the editor to confirm the code is there. - Check that a browser extension is not blocking the script. Ad blockers and privacy tools commonly block googletagmanager.com.
- If you have a caching plugin active, clear the cache after saving the functions.php changes.
You can also right-click the page and select View Page Source, then search for gtag to confirm the script is present in the HTML output.
Practical Tips
GA4 tracks page views automatically once the snippet is in place. You do not need to configure anything extra for basic traffic data. For more advanced tracking — form submissions, button clicks, scroll depth — you will need to set up events, which is best done through Google Tag Manager rather than adding more code to functions.php.
One thing worth checking: GA4 properties have a data retention setting that defaults to two months. If you want historical data available for longer, go to Admin → Data Settings → Data Retention in your GA4 property and change it to 14 months. Do this early — the setting only affects data collected going forward, not data already stored.
Common Mistakes
- Editing the parent theme’s functions.php — if you update your theme, this file gets overwritten and your tracking code disappears. Use a child theme, or add a site-specific plugin instead.
- Using a Universal Analytics (UA) ID — GA4 Measurement IDs start with
G-. If yours starts withUA-, it is a legacy property. Universal Analytics was shut down in 2024 and no longer collects data. - Not filtering your own traffic — without the logged-in user exclusion or an IP filter in GA4, your own sessions will show up in your reports and skew early-stage data significantly.
- Forgetting to connect Search Console — GA4 does not show keyword data on its own. Link your GA4 property to Google Search Console in the Admin settings to get organic search queries in your reports. See the conversion tracking guide for how to connect these tools effectively.
Plugin vs Manual: When Each Makes Sense
The manual method is the right choice if you are comfortable editing functions.php and want to keep your plugin count down. It is also the better option if you need fine-grained control over when and how the script loads.
A plugin makes more sense if you are setting up a site for a client who will manage it themselves, or if you want the added features that tools like Site Kit provide — Search Console integration, PageSpeed data, and a unified dashboard inside WordPress.
Conclusion
Adding GA4 to WordPress without a plugin takes about five minutes once you have your Measurement ID. Add the gtag.js snippet to functions.php via the wp_head hook, exclude logged-in users, and verify in the Realtime report. That is all the setup needed to start collecting accurate traffic data from day one.

Etienne Basson works with website systems, SEO-driven site architecture, and technical implementation. He writes practical guides on building, structuring, and optimizing websites for long-term growth.