Step-by-step guide. Where to paste the G-XXXXXXX code? GTM vs direct integration. Consent Mode v2, Server-Side Tracking and GDPR compliance.
EN

Adding Google Analytics 4 (GA4) to WordPress – complete guide 2026

5.00 /5 - (29 votes )
Last verified: May 1, 2026
7min read
Case study
500+ WP projects

#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:

MethodDifficultyBest forGDPR-ready
Plugin (Site Kit, Rank Math)EasyBeginners, simple sitesDepends on consent plugin
Google Tag Manager (GTM)MediumProfessional sites, marketersYes, with Consent Mode v2
Direct code (functions.php)MediumDevelopers, minimal setupsManual implementation
Server-side trackingAdvancedHigh-traffic, privacy-focusedBest 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:

  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. 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?

  1. Central management panel: All tags (GA4, Facebook Pixel, Hotjar, ads) in one place
  2. Consent Mode v2: Simple 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 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:

  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 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
}
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

  1. A script on the page sends data to your server (not to Google)
  2. Your server forwards the data to Google Analytics
  3. 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

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

#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 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

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

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.

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
How long does the implementation take?
Most readers complete this guide in 30 to 60 minutes. Complex implementations may require 2 to 3 hours including testing.
What prerequisites do I need?
You need a WordPress site (version 6.0+ recommended), admin access, and basic familiarity with the WordPress dashboard. Some steps require FTP access.
Can I undo the changes if something goes wrong?
Yes, always back up your site before making changes. Most steps include rollback instructions or reversible actions.

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 properly add GA4 tracking in WordPress? Plugin, GTM, or code? Plus Consent Mode v2 and Server-Side Tracking.
analytics

Adding Google analytics 4 (ga4) to WordPress – Complete guide 2026

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

CRA covers products with digital elements. NIS2 covers entities. DORA covers financial entities. When all three apply at once, headless WordPress sits at the intersection. I sketch what the joint evidence package looks like in 2026.
wordpress

Cyber Resilience Act + NIS2 + DORA: the 2026 compliance stack for headless WordPress

CRA covers products with digital elements. NIS2 covers entities. DORA covers financial entities. When all three apply at once, headless WordPress sits at the intersection. I sketch what the joint evidence package looks like in 2026.

Article 28 of Regulation 2022/2554 makes financial entities responsible for the ICT risk of every third-party they touch. I walk through the supplier due-diligence checklist I ship with WordPress engagements for banks and insurers in 2026.
wordpress

DORA Article 28 ICT third-party risk: WordPress hosting and WAF supplier audit

Article 28 of Regulation 2022/2554 makes financial entities responsible for the ICT risk of every third-party they touch. I walk through the supplier due-diligence checklist I ship with WordPress engagements for banks and insurers in 2026.