Key takeaways
- Table of Contents
- 1. Google Analytics 4: What's Changed
- 2. Installation Methods: gtag.js vs GTM
Google Analytics is the most widely used web analytics tool in the world. Over 28 million websites use it, with a large majority being WordPress sites. Yet a poorly configured installation can not only skew your data but also expose your site to GDPR compliance and security risks.
This article guides you step by step to correctly install and configure Google Analytics 4 (GA4) on your WordPress site in 2026, while respecting privacy and performance requirements.
1. Google Analytics 4: What's Changed
Google Analytics 4 (GA4) replaced Universal Analytics (UA) on July 1, 2023. In 2026, GA4 is the only version available — Universal Analytics no longer collects data.
Key Differences Between UA and GA4
- Data model: GA4 uses an event-based model (instead of sessions/pageviews)
- Cross-platform tracking: GA4 tracks users across web and mobile apps
- Artificial intelligence: GA4 includes predictions and automated insights
- Data retention: 2 or 14 months (configurable)
- Privacy protection: IP anonymization by default, built-in consent mode
Moving to GA4 is not optional — it's necessary to continue measuring your WordPress site's audience.
2. Installation Methods: gtag.js vs GTM
There are two main methods for installing Google Analytics on WordPress:
Method 1: gtag.js (Google Tag)
Code to place in the header of every page:
<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>
Advantages:
- Simple and fast installation
- No additional tools required
- Ideal for individual sites
Disadvantages:
- Difficult to manage if using other tracking tools
- No granular tag control
- Source code modification needed for each change
Method 2: Google Tag Manager (GTM)
GTM is a centralized tag manager that lets you deploy Google Analytics and other tracking tools without modifying source code.
Advantages:
- Centralized management of all tags (GA, Facebook, Hotjar, etc.)
- User interface to add/modify tags
- Versioning and gradual deployment
- Built-in native consent mode
- Easier GDPR compliance
Disadvantages:
- Initial learning curve
- Additional third-party tag to load
- Potential conflicts with other plugins
Which Method to Choose?
| Criteria | gtag.js | GTM |
|---|---|---|
| Simplicity | One-time setup | More complex initial setup |
| Flexibility | Limited | Very high |
| Number of tools | Google only | Multi-tool |
| Performance | Lightweight | Slightly heavier |
| GDPR compliance | Manual | Built-in |
| Recommended for | Simple sites | Professional sites |
Recommendation: for a professional WordPress site, Google Tag Manager is the recommended method in 2026. It offers more flexibility and facilitates GDPR compliance.
3. Step-by-Step Installation
Create a Google Analytics Account
- Go to analytics.google.com
- Sign in with your Google account
- Click "Admin" (gear icon in bottom left)
- Click "+ Create Account"
- Name your account (company or site name)
- Create a GA4 property
- Configure property settings (timezone, currency)
Create a Web Data Stream
- In your GA4 property, go to "Data Streams"
- Click "Add stream" → "Web"
- Enter your WordPress site URL
- Enable "Enhanced Measurement" (recommended)
- Click "Create stream"
- Note your Measurement ID (format G-XXXXXXXXXX)
Installation on WordPress
gtag.js Method
- Install the Insert Headers and Footers or WPCode plugin
- In the plugin settings, paste the gtag.js code in the "Header" section
- Save
- Activate the plugin on all pages
GTM Method
- Create an account at tagmanager.google.com
- Create a container for your website
- Install the GTM4WP or WordPress Google Tag Manager plugin
- Configure the plugin with your GTM container ID (GTM-XXXXXXX)
- In GTM, create a "Google Analytics: GA4 Configuration" tag
- Set the "All Pages" trigger
- Test and publish your container
4. Consent Mode and Privacy Compliance
Google's Consent Mode is essential for GDPR compliance. It allows Google Analytics to work even without consent, while respecting the user's choices.
How Consent Mode Works
With Consent Mode, Google Analytics adapts to user consent:
- Consent granted: GA4 collects complete data (pageviews, events, conversions)
- Consent denied: GA4 collects only anonymized data (no cookies, no identification)
Consent Mode code:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
// Set default consent
gtag('consent', 'default', {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'wait_for_update': 500
});
// Update consent when user accepts
gtag('consent', 'update', {
'analytics_storage': 'granted',
'ad_storage': 'granted'
});
</script>
Configuration with GTM
- In GTM, go to "Variables" → "Consent Configuration"
- Add consent variables:
analytics_storage:deniedby defaultad_storage:deniedby default- Create a consent trigger to update when user accepts
- Associate this trigger with your GA4 tags
GDPR Best Practices
- IP anonymization: enabled by default in GA4 (no action required)
- Data retention: configure to maximum 14 months (or 2 months for more privacy)
- Disable sharing: disable data sharing with Google
- Data deletion: implement a mechanism for deletion on request
Consent mode is a GDPR requirement for any site using Google Analytics. If you haven't configured consent mode yet, WpDefender can help you achieve compliance.
5. Advanced GA4 Configuration
Beyond basic installation, certain advanced configurations improve data quality and compliance.
Goals and Conversions
- Contacts: create a custom event for form submissions
- Purchases: configure e-commerce tracking (if using WooCommerce)
- Signups: track newsletter subscriptions
- Downloads: track file downloads
Custom Events
To track specific actions, create custom events:
// Example: tracking a button click
<script>
document.querySelector('#contact-button').addEventListener('click', function() {
gtag('event', 'contact_button_click', {
'event_category': 'engagement',
'event_label': 'homepage'
});
});
</script>
Explorations and Custom Reports
- User journey analysis: identify drop-off points
- Cohort analysis: compare behavior of user groups
- Conversion report: track the conversion funnel
- Predictive analysis: use GA4 predictions (purchase probability, churn)
6. Verifying the Installation
After installation, verify that Google Analytics is correctly collecting data:
Real-Time Verification
- Open your WordPress site in a browser
- In GA4, go to "Realtime" in the left menu
- Verify that activity appears (at least 1 user currently)
- Navigate to multiple pages and verify that pages are being recorded
Verification with Google Tag Assistant
- Install the Tag Assistant extension for Chrome
- Visit your WordPress site
- Verify that the GA4 tag is detected and configured correctly
- Check the events being sent
Verification with GTM (if used)
- In GTM, go to "Preview"
- Enter your site URL
- Verify that the GA4 tag fires on all pages
- Inspect variables and triggers
Verification Tools
- GA4 DebugView: in debug mode, visualize each event in real time
- Google Analytics Debugger: Chrome extension to enable debug mode
- Redirect Path: check redirects that might prevent tracking
7. Common Mistakes to Avoid
Here are the most frequent mistakes when installing Google Analytics on WordPress:
Technical Errors
- Code misplaced: the code must be in the header (
<head>), not the footer - Wrong ID: verify that your Measurement ID (G-XXXXXXXXXX) is correct
- Double counting: don't install GA via both gtag.js AND GTM
- Blocked by plugin: some security plugins block third-party scripts
- Caching: browser cache can mask errors — test in incognito mode
Configuration Errors
- No consent mode: consent mode is mandatory in 2026 for GDPR
- No IP anonymization: enabled by default in GA4, but verify
- Double filtering: don't filter data both in GA4 and in a plugin
- Test data: exclude your own traffic from reports (filter by IP or opt-out)
Tracking Errors
- Missing events: configure essential conversion events
- Retention settings: configure data retention (2 or 14 months)
- No filters: use filters to exclude bots and internal traffic
- Enhanced Measurement disabled: enable for automatic click and scroll tracking
8. Alternatives to Google Analytics
For sites concerned with privacy or data sovereignty, there are alternatives to Google Analytics:
Matomo
- Open source — hosted on your own servers
- No data transfer to Google
- GDPR compliant by design
- Official WordPress plugin available
Plausible Analytics
- Lightweight — script under 1 KB
- Cookie-free — privacy-respecting by default
- GDPR compliant — no consent required
- WordPress plugin available
Fathom Analytics
- Simple — clean and intuitive dashboard
- Privacy-respecting
- GDPR compliant
- Lightweight and fast script
When to Choose an Alternative?
- Your audience is primarily European (data sovereignty)
- You want to avoid the complexity of consent mode
- You're looking for a simple and lightweight tool
- Your site doesn't need GA4's advanced features
The choice between Google Analytics and an alternative depends on your specific needs. WpDefender can advise you on the best solution for your site and compliance requirements.
Installation Checklist
| Step | Status | Priority |
|---|---|---|
| Create GA4 account | ☐ | High |
| Configure data stream | ☐ | High |
| Choose method (gtag.js or GTM) | ☐ | High |
| Install code on WordPress | ☐ | High |
| Configure Consent Mode | ☐ | High |
| Verify installation (real-time) | ☐ | High |
| Set up conversion goals | ☐ | Medium |
| Exclude internal traffic | ☐ | Medium |
| Verify GDPR compliance | ☐ | High |
| Document configuration | ☐ | Low |
Conclusion
A correct installation of Google Analytics 4 on WordPress is essential for measuring your site's effectiveness while respecting visitor privacy. In 2026, consent mode and GDPR compliance are no longer optional.
Take the time to properly configure GA4, or get professional help to avoid common mistakes.
Need help installing Google Analytics on your WordPress site? Contact WpDefender — we configure GA4 with consent mode, conversions, and GDPR compliance. Guaranteed 30-minute response, 7 days a week.