How to add Google Analytics to WordPress in 2026
There are four ways to add Google Analytics 4 (GA4) to a WordPress site in 2026, ranging from beginner-friendly to advanced. Each method has different trade-offs for control, compliance, and performance:
| Method | Difficulty | Best for | GDPR-ready |
|---|---|---|---|
| Plugin (Site Kit, Rank Math) | Easy | Beginners, simple sites | Depends on consent plugin |
| Google Tag Manager (GTM) | Medium | Professional sites, marketers | Yes, with Consent Mode v2 |
| Direct code (functions.php) | Medium | Developers, minimal setups | Manual implementation |
| Server-side tracking | Advanced | High-traffic, privacy-focused | Best compliance option |
If you are in the EU (including Poland), Consent Mode v2 is mandatory since March 2024. Any implementation method must integrate with a cookie consent banner (Cookiebot, Complianz, or similar) before firing analytics tags.
Best way to add Google Analytics 4 to a WordPress site
For most professional WordPress sites in 2026, Google Tag Manager (GTM) is the recommended method. It provides the best balance of control, flexibility, and compliance:
- You can manage GA4, conversion tracking, remarketing tags, and custom events from a single GTM dashboard without touching WordPress code.
- Consent Mode v2 integrates natively with GTM, respecting user privacy choices before firing any tracking tags.
- Server-side GTM takes this further by routing analytics through your own domain, reducing ad-blocker interference and improving data accuracy.
The plugin method (Site Kit) is faster to set up but gives less control over consent handling and event configuration. Direct code injection works but becomes hard to maintain as tracking requirements grow.
Analytics is the foundation of every digital strategy. But the way to add Google Analytics to WordPress has changed dramatically over the years. We used to paste a simple _gaq.push script. Today, in the era of GA4, GDPR and Consent Mode v2, things are significantly more complex.
This guide walks you through all GA4 integration methods with WordPress, from the simplest to the most advanced, including legal requirements and best practices for 2026.
Part 1: Plugin method (for beginners)
If you do not want to work with code, use a plugin. It is the simplest method but has limitations.
Site Kit by Google (official plugin)
Pros:
- Official Google product
- Shows statistics directly in the WP dashboard
- Automatic integration with Search Console, PageSpeed Insights
- Easiest setup (a few clicks)
Cons:
- Limited tag control
- No advanced tracking options
- May conflict with other plugins
Installation:
- Plugins → Add New → “Site Kit”
- Activate and connect to Google account
- Select services to connect (Analytics, Search Console)
- Done
RankMath / Yoast SEO
Many SEO plugins have built-in tracking ID options. A good choice if you already use one of these plugins.
RankMath:
- RankMath → General Settings → Analytics
- Paste Measurement ID (G-XXXXXXX)
Part 2: Google Tag Manager (professional method)
This is the recommended method in 2026. Instead of adding GA4 directly, add a GTM container that manages all tags.
Why GTM?
- Central management panel: All tags (GA4, Facebook Pixel, Hotjar, ads) in one place
- Consent Mode v2: Simple implementation required by EU law
- No code editing: Changes without modifying theme files
- Debugging: Built-in preview mode
- Versioning: Change history and rollback capability
Step by step: GTM integration with WordPress
1. Create GTM container:
- Go to tagmanager.google.com
- Create account and container (Web)
- Copy container code (GTM-XXXXXXX)
2. Paste code in WordPress:
// In functions.php or mu-plugin
add_action('wp_head', 'add_gtm_head', 1);
add_action('wp_body_open', 'add_gtm_body', 1);
function add_gtm_head() {
?>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-YOUR-ID');</script>
<!-- End Google Tag Manager -->
<?php
}
function add_gtm_body() {
?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-YOUR-ID"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<?php
}
3. Configure GA4 tag in GTM:
- In GTM: Tags → New → GA4 Configuration
- Paste Measurement ID (G-XXXXXXX)
- Trigger: All Pages
- Publish
Consent Mode v2 (required in the EU)
Since March 2024, Consent Mode v2 is mandatory in Europe. Without it, Google will not process data from your site.
What it does:
- Blocks analytics and advertising cookies by default
- After user acceptance: full tracking
- After rejection: anonymous pings (modeled data)
Implementation with GTM:
- Install cookie banner (Cookiebot, Complianz, CookieYes)
- Configure GTM integration
- Set tags to “Respect Consent State”
- Add consent variables (analytics_storage, ad_storage)
Part 3: Direct code (for developers)
If you want to avoid plugins and have a simple site, add the code directly.
Basic implementation
add_action('wp_head', 'add_ga4_code', 1);
function add_ga4_code() {
?>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOUR-ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-YOUR-ID');
</script>
<?php
}
With Consent Mode support
add_action('wp_head', 'add_ga4_with_consent', 1);
function add_ga4_with_consent() {
?>
<script>
// Default consent state - everything blocked
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'wait_for_update': 500
});
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOUR-ID"></script>
<script>
gtag('js', new Date());
gtag('config', 'G-YOUR-ID');
</script>
<?php
}
Part 4: Server-Side Tracking (advanced)
In 2026, ad blockers and ITP (Intelligent Tracking Prevention) significantly limit the effectiveness of client-side tracking. The solution is Server-Side Tracking.
How it works
- A script on the page sends data to your server (not to Google)
- Your server forwards the data to Google Analytics
- Blockers do not see the Google connection
Implementation options
1. Google Tag Manager Server-Side Container:
- Requires Cloud Run (GCP) or other hosting
- Full data control
2. Stape.io (SaaS):
- Hosted sGTM
- Easier setup
Server-Side benefits
- Ad blocker resistant: approximately 95% data vs approximately 70%
- Better accuracy: fewer conversion losses
- First-Party Cookies: longer cookie lifespan
- Privacy: data passes through your server
Part 5: E-commerce tracking (WooCommerce)
If you run a store, you need Enhanced E-commerce.
Automatic tracking with GTM
- Data Layer: WooCommerce sends product, cart and purchase data
- GA4 E-commerce tags: configure in GTM
- Events: view_item, add_to_cart, purchase
Part 6: Debugging and verification
Tools
- Google Tag Assistant: Chrome extension
- GTM Preview Mode: preview mode
- GA4 DebugView: real-time in GA4
- Network tab: check that requests are being sent
Common issues
1. Double tracking:
- Symptom: 200% page views
- Cause: two GA4 tags (for example, Site Kit + GTM)
- Solution: remove one source
2. No data:
- Symptom: 0 users
- Cause: consent blocked or wrong ID
- Solution: check Consent Mode and Measurement ID
Summary
| Method | Level | Consent Mode | Server-Side | E-commerce |
|---|---|---|---|---|
| Plugin (Site Kit) | Beginner | ❌ | ❌ | ❌ |
| GTM Client-Side | Intermediate | ✅ | ❌ | ✅ |
| GTM + Consent Mode | Professional | ✅ | ❌ | ✅ |
| sGTM Server-Side | Expert | ✅ | ✅ | ✅ |
Recommendation for 2026:
- Small sites: Site Kit or RankMath
- Business/agencies: GTM + Consent Mode v2
- E-commerce/Enterprise: sGTM Server-Side
Do not let your analytics data be incomplete. Learn more about professional WordPress development at WPPoland.

