Google Analytics 4 in WordPress: GA4, GTM and consent mode
EN

Google Analytics 4 in WordPress: GA4, GTM and consent mode

Last verified: May 1, 2026
5min read
Case study
500+ WP projects

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.

Learn more about WordPress development services at WPPoland. This guide will walk you through all GA4 integration methods with WordPress, from the simplest to the most advanced, including legal requirements and 2026 best practices.

#Part 1: Plugin method (for beginners)

If you don’t want to mess with code, use a plugin. It’s the simplest method but has limitations.

#Site kit by Google (official plugin)

Pros:

  • Official Google product
  • Shows stats directly in 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:

  1. Plugins → Add New → “Site Kit”
  2. Activate and connect to Google account
  3. Select services to connect (Analytics, Search Console)
  4. Done

#Rankmath / Yoast SEO

Many SEO plugins have built-in tracking ID options. 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?

  1. Central Management Panel: All tags (GA4, Facebook Pixel, Hotjar, ads) in one place
  2. Consent Mode v2: Easy implementation required by EU law
  3. No Code Editing: Changes without modifying theme files
  4. Debugging: Built-in preview mode
  5. Versioning: Change history and rollback capability

#Step by step: Gtm integration with WordPress

1. Create GTM Container:

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

Since March 2024, Consent Mode v2 is mandatory in Europe. Without it, Google won’t process data from your site.

What it does:

  • Blocks analytics/advertising cookies by default
  • After user acceptance: full tracking
  • After rejection: anonymous pings (modeled data)

Implementation with GTM:

  1. Install cookie banner (Cookiebot, Complianz, CookieYes)
  2. Configure GTM integration
  3. Set tags to “Respect Consent State”
  4. 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 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
}
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 client-side tracking effectiveness. The solution is Server-Side Tracking.

#How it works

  1. Script on page sends data to your server (not Google)
  2. Your server forwards data to Google Analytics
  3. Blockers don’t see the Google connection

#Implementation options

1. Google Tag Manager Server-Side Container:

  • Requires Cloud Run (GCP) or other hosting
  • Cost: ~$50-100/month for medium traffic
  • Full data control

2. Stape.io (SaaS):

  • Hosted sGTM
  • Easier setup
  • From $20/month

#Server-Side benefits

  • Ad Blocker Resistant: ~95% data vs ~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 have a store, you need Enhanced E-commerce.

#Automatic tracking with gtm

  1. Data Layer: WooCommerce sends product, cart, purchase data
  2. GA4 E-commerce Tags: Configure in GTM
  3. Events: view_item, add_to_cart, purchase

#WooCommerce plugin

GTM4WP (Premium) or Monster Insights (Pro):

  • Automatic Data Layer integration
  • Ready-made tag templates
  • Conversion tracking without coding

#Part 6: Debugging and verification

#Tools

  1. Google Tag Assistant: Chrome Extension
  2. GTM Preview Mode: Preview mode
  3. GA4 DebugView: Real-time in GA4
  4. Network Tab: Check if requests are sent

#Common issues

1. Double Tracking:

  • Symptom: 200% pageviews
  • Cause: Two GA4 tags (e.g., Site Kit + GTM)
  • Solution: Remove one source

2. No Data:

  • Symptom: 0 users
  • Cause: Consent blocked / wrong ID
  • Solution: Check Consent Mode and Measurement ID

3. GDPR Non-Compliance:

  • Symptom: Tracking before consent
  • Cause: No Consent Mode
  • Solution: Implement Consent Mode v2

#Summary

MethodLevelConsent ModeServer-SideE-commerce
Plugin (Site Kit)Beginner
GTM Client-SideIntermediate
GTM + Consent ModeProfessional
sGTM Server-SideExpert

2026 Recommendation:

  • Small sites: Site Kit or RankMath
  • Business/Agencies: GTM + Consent Mode v2
  • E-commerce/Enterprise: sGTM Server-Side

Don’t let your analytics data be incomplete. Invest in proper configuration – it’s the foundation of all marketing decisions.

Next step

Turn the article into an actual implementation

This block strengthens internal linking and gives readers the most relevant next move instead of leaving them at a dead end.

Want this implemented on your site?

If you want to convert the article into a working site improvement, redesign, or build plan, I can define the scope and implement it.

Related cluster

Explore other WordPress services and knowledge base

Strengthen your business with professional technical support in key areas of the WordPress ecosystem.

Article FAQ

Frequently Asked Questions

Practical answers to apply the topic in real execution.

SEO-ready GEO-ready AEO-ready 3 Q&A
Should GA4 be added with a plugin, GTM or code? #
For simple sites, a focused plugin can be enough. GTM is better when consent rules, advertising tags or multiple events must be managed without editing theme code every time.
What should be verified after adding GA4 to WordPress? #
Check DebugView, Tag Assistant, consent state, duplicate page_view events, excluded admin traffic, form events and whether cached pages still output the expected tag.
Can GA4 setup affect performance or compliance? #
Yes. Poorly loaded tags can add script weight, and analytics without consent handling can create legal risk. The setup should respect Consent Mode, local privacy rules and page-speed budgets.

Need an FAQ tailored to your industry and market? We can build one aligned with your business goals.

Let’s discuss

Related Articles

Old methods (ua.js) no longer work. How to correctly add GA4 tracking in WordPress? Plugin, GTM, or code? Plus Consent Mode v2 and Server-Side Tracking.
analytics

Google Analytics 4 in WordPress: GA4, GTM and consent mode

Old methods (ua.js) no longer work. How to correctly add GA4 tracking in WordPress? Plugin, GTM, or code? Plus Consent Mode v2 and Server-Side Tracking.

The initial port from WordPress to Astro took weeks. The other eleven months went to redirects, hreflang, six-locale parity, and a build that outgrew Cloudflare's own runner. A migration field report.
headless

Twelve months migrating from WordPress to Astro on Cloudflare Pages

The initial port from WordPress to Astro took weeks. The other eleven months went to redirects, hreflang, six-locale parity, and a build that outgrew Cloudflare's own runner. A migration field report.

Metorik founder Bryce Adams told WP Product Talk that the company's MCP integration drew 500 users within days of a quiet preview launch, faster than any feature he has shipped in ten years. He also said customers churning out of Metorik have an average MRR 40 percent lower than retained ones, suggesting AI is taking the commodity use cases, not the core ones. GravityKit just open-sourced Block MCP for block-level WordPress edits. The pattern is clear: in 2026, the plugin that ships an MCP server is the one that compounds. The plugin that bolts a chat box onto its admin is the one that gets cannibalised.
wordpress

Why shipping an MCP server in your WordPress plugin is the AI move that survives

Metorik founder Bryce Adams told WP Product Talk that the company's MCP integration drew 500 users within days of a quiet preview launch, faster than any feature he has shipped in ten years. He also said customers churning out of Metorik have an average MRR 40 percent lower than retained ones, suggesting AI is taking the commodity use cases, not the core ones. GravityKit just open-sourced Block MCP for block-level WordPress edits. The pattern is clear: in 2026, the plugin that ships an MCP server is the one that compounds. The plugin that bolts a chat box onto its admin is the one that gets cannibalised.