Google Analytics 4 Integration Guide

Integrating Google Analytics 4 (GA4) with DigiConsent ensures your analytics tracking complies with privacy regulations while maintaining accurate data collection. GA4 is Google’s latest analytics platform, featuring enhanced privacy controls, cross-platform tracking, and native integration with Google Consent Mode v2—making it the ideal analytics solution for privacy-conscious websites.

This comprehensive guide walks you through every step of properly integrating GA4 with DigiConsent, from obtaining your measurement ID to configuring consent categories and testing your implementation. Whether you’re migrating from Universal Analytics or setting up GA4 for the first time, you’ll learn how to balance compliance with data collection effectiveness.

Understanding GA4 and Cookie Consent

Google Analytics 4 differs significantly from Universal Analytics in how it handles user tracking and privacy. GA4 uses an event-based data model rather than session-based tracking, and it’s designed to work with Google Consent Mode v2—a framework that adjusts tracking behavior based on user consent choices.

When integrated correctly with DigiConsent, GA4 operates in one of two modes depending on user consent. If users accept analytics cookies, GA4 collects full behavioral data including detailed user interactions, demographics, and conversion paths. If users reject analytics cookies, GA4 can still collect aggregated, anonymized data through Consent Mode’s modeling capabilities—giving you useful insights while respecting privacy preferences.

This dual-mode operation is crucial for GDPR compliance. You cannot simply load GA4 scripts regardless of consent—doing so violates privacy regulations in the EU and many other jurisdictions. DigiConsent blocks GA4 from loading until users provide appropriate consent, then enables tracking only for the data categories they’ve accepted.

Prerequisites: Setting Up Your GA4 Property

Before configuring DigiConsent, you need a Google Analytics 4 property. If you haven’t created one yet, follow these steps in your Google Analytics account:

  1. Log into Google Analytics at analytics.google.com
  2. Click Admin in the bottom left corner
  3. Under the Account column, select or create an account
  4. Under the Property column, click “Create Property”
  5. Enter your property name (typically your website name)
  6. Select your reporting time zone and currency
  7. Click “Next” and configure business details
  8. Accept the terms of service

After creating your property, you’ll need to set up a data stream. GA4 uses data streams to collect information from websites, iOS apps, or Android apps. For WordPress integration:

  1. In your new GA4 property, click “Data Streams” under Property settings
  2. Click “Add stream” and select “Web”
  3. Enter your website URL (including https://)
  4. Enter a stream name (your website name works well)
  5. Enable “Enhanced measurement” to automatically track scrolls, outbound clicks, site search, video engagement, and file downloads
  6. Click “Create stream”

Once created, you’ll see your Measurement ID at the top of the stream details page. It looks like G-XXXXXXXXXX where the X’s are alphanumeric characters. Copy this ID—you’ll need it for DigiConsent configuration.

Configuring Google Consent Mode v2

Before adding GA4 scripts to DigiConsent, configure Google Consent Mode v2 in the plugin settings. This ensures GA4 receives consent signals correctly.

Navigate to DigiConsent → Settings → Google Consent Mode in your WordPress admin. You should see options for enabling Consent Mode and configuring default consent states. Here’s how to configure it for GA4:

  • Enable Google Consent Mode v2: Toggle this ON. This activates the consent framework that GA4 uses to adjust its behavior.
  • Analytics Storage (Default): Set to Denied if using opt-in consent mode (required for GDPR). This tells Google that analytics cookies are blocked by default until consent is given.
  • Ad Storage (Default): Set to Denied for privacy-first approach. This controls advertising and remarketing cookies.
  • Ad User Data: Set to Denied by default. This controls whether user data can be sent to Google for advertising purposes.
  • Ad Personalization: Set to Denied by default. This controls whether ads can be personalized using collected data.

These default settings create a privacy-first configuration. When users accept analytics cookies through DigiConsent, these consent states automatically update to “Granted,” allowing GA4 to collect full data. When users reject or don’t interact with the banner (in opt-in mode), these states remain “Denied,” and GA4 operates in a limited, privacy-preserving mode.

Important: If you’re using DigiConsent Pro with geolocation targeting, you can configure different Consent Mode defaults for different regions. For example, set stricter defaults (all Denied) for EU visitors while using less restrictive settings for regions without GDPR requirements. This regional configuration is found in the location-based rules section of Pro settings.

Adding GA4 Tracking Code to DigiConsent

With Consent Mode configured, you’re ready to add your GA4 tracking code. Navigate to DigiConsent → Settings → Cookie Categories and select the Analytics Cookies tab. This is where you’ll add the GA4 script.

In the script area for Analytics Cookies, add the following code, replacing G-XXXXXXXXXX with your actual Measurement ID:

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

This is the standard GA4 installation code. When you paste it into the Analytics Cookies script field, DigiConsent automatically blocks these scripts from executing until users accept analytics cookies. The blocking happens through DigiConsent’s script management system, which prevents JavaScript execution by modifying the script type attribute until consent is provided.

Alternative: Using DigiConsent’s Script Templates

DigiConsent includes built-in templates for common tracking services. Instead of manually pasting the code, you can use the template system:

  1. In the Analytics Cookies script field, click the “Templates” button (if available)
  2. Select “Google Analytics 4” from the template list
  3. When prompted, enter your Measurement ID (G-XXXXXXXXXX)
  4. The template automatically generates the correct code

Using templates ensures you have the most up-to-date tracking code format and reduces the risk of syntax errors from manual code entry.

Advanced Configuration Options

The basic GA4 integration works well for most websites, but Google Analytics 4 offers advanced configuration options you can add to enhance tracking while maintaining privacy compliance.

Anonymizing IP Addresses

GA4 automatically anonymizes IP addresses in the European Economic Area and Switzerland, but you can explicitly enable IP anonymization globally by modifying your configuration:

gtag('config', 'G-XXXXXXXXXX', {
  'anonymize_ip': true
});

While GA4 handles IP anonymization automatically in regions where required, explicitly enabling it demonstrates privacy commitment and can be useful for compliance documentation.

Custom Dimensions and Events

If you use custom dimensions or track custom events, you can include these configurations in your GA4 script. For example, tracking user consent choices as a custom dimension:

gtag('config', 'G-XXXXXXXXXX', {
  'custom_map': {'dimension1': 'consent_analytics'},
  'consent_analytics': 'granted'
});

This creates a custom dimension showing whether users granted analytics consent, allowing you to segment your GA4 reports based on consent status and understand how consent choices affect user behavior patterns.

Debugging Configuration

During initial setup, enable debug mode to verify GA4 is receiving data correctly:

gtag('config', 'G-XXXXXXXXXX', {
  'debug_mode': true
});

With debug mode enabled, open your browser’s developer console (F12 in most browsers) and look for GA4 debug messages. These messages show exactly what data GA4 is collecting and help identify configuration issues. Remember to remove 'debug_mode': true after testing, as it increases the amount of data sent to Google and should not run in production.

Testing Your GA4 Integration

Proper testing ensures GA4 tracks correctly based on consent choices. Testing should cover three scenarios: acceptance, rejection, and consent withdrawal.

Testing Consent Acceptance

  1. Clear all cookies in your browser or use a private/incognito window
  2. Visit your website and open the browser developer console (F12)
  3. Go to the Network tab in developer tools
  4. Accept analytics cookies in the DigiConsent banner
  5. Look for network requests to google-analytics.com or googletagmanager.com
  6. If you see these requests, GA4 is loading correctly after consent

Additionally, check the Console tab for any JavaScript errors. If GA4 is working properly, you should see no errors, and the Network tab should show successful requests to Google’s analytics servers.

Testing Consent Rejection

  1. Clear cookies and return to your site in a private window
  2. Open browser developer tools Network tab
  3. Reject analytics cookies in the DigiConsent banner
  4. Browse several pages on your site
  5. Verify that NO requests go to Google Analytics servers
  6. Check that the GA4 script itself is not loaded or executed

If you see any GA4 network requests after rejecting cookies, something is wrong with your configuration. The most common issue is having GA4 code added outside DigiConsent (perhaps in theme files or another plugin), which bypasses consent management. Search your theme files and other plugins for GA4 code and remove any instances found outside DigiConsent’s control.

Using GA4 DebugView

Google Analytics 4 includes DebugView, a real-time debugging tool that shows events as they’re received. To use DebugView:

  1. Enable debug mode in your GA4 configuration (shown earlier)
  2. In your GA4 property, navigate to Configure → DebugView
  3. Accept analytics cookies on your website
  4. Browse your site and interact with content
  5. Watch DebugView in real-time to see events appearing

DebugView shows every event GA4 receives, including automatically collected events (page views, scrolls, clicks) and any custom events you’ve configured. If events appear in DebugView after accepting cookies but not after rejecting them, your integration is working correctly.

Verifying Data in GA4 Reports

After testing, verify that data flows into your GA4 reports correctly. It can take 24-48 hours for data to appear in standard reports, but real-time data appears immediately.

Check the Realtime report in GA4 (Reports → Realtime). With analytics cookies accepted on your site, you should see:

  • Active users count increasing when you browse
  • Your page views appearing in the “View item list by Page title and screen name” section
  • Events appearing in the “Event count by Event name” section
  • Your geographic location if you’re using geolocation features

If real-time data appears correctly, your integration is successful. Standard reports will populate as more users visit and interact with your site over the following days.

Common GA4 Integration Issues

GA4 Loading Before Consent

If GA4 loads regardless of consent choices, you likely have duplicate tracking code outside DigiConsent’s control. Check these locations:

  • Theme files: Search header.php, footer.php, and functions.php for GA4 code
  • Other plugins: SEO plugins often include GA4 integration. Disable GA4 in those plugins if enabled
  • Google Site Kit: If using Google’s official Site Kit plugin, it may add GA4 independently. Configure Site Kit to work with consent management or disable its GA4 module
  • Tag managers: If using Google Tag Manager or another tag management system, ensure tags fire only after consent

Use your browser’s developer tools to inspect the page source. Search (Ctrl+F or Cmd+F) for your Measurement ID. It should only appear once, within DigiConsent’s consent-controlled scripts. If you find multiple instances, locate and remove the duplicate entries.

Consent Mode Not Working

If GA4 loads but doesn’t respect consent settings, verify that:

  • Google Consent Mode v2 is enabled in DigiConsent settings
  • Default consent states are set to “Denied” for analytics_storage
  • The consent banner appears and users can accept/reject cookies
  • You’ve cleared all browser caches after configuration changes

Check the browser console for consent-related errors. Google Consent Mode errors typically include “gtag” or “consent” in the error message and indicate misconfiguration of the consent framework.

No Data in GA4 Reports

If testing succeeds but no data appears in GA4 reports after 48 hours:

  • Verify your Measurement ID is correct—even one wrong character breaks the connection
  • Confirm your GA4 property is properly configured with an active data stream
  • Check that you’re looking at the correct property in GA4 (easy to confuse if you have multiple properties)
  • Ensure your website generates sufficient traffic—GA4 may not show reports for sites with very low traffic
  • Confirm the date range in GA4 reports includes recent dates when testing occurred

Optimizing GA4 for Consent-Based Tracking

When users reject analytics cookies, you lose detailed tracking data. However, you can optimize your GA4 configuration to maximize insights even in consent-restricted scenarios.

Conversion Modeling

Google Consent Mode v2 includes conversion modeling, which uses machine learning to estimate conversions from users who denied consent. This modeling fills data gaps while respecting privacy choices. Conversion modeling works automatically when Consent Mode is enabled—no additional configuration needed.

You’ll see modeled conversions indicated in GA4 reports with a special icon. These modeled conversions are estimates based on users who did provide consent, extrapolated to represent the full audience. While not as accurate as direct tracking, modeling provides valuable directional insights.

Server-Side Tracking

For advanced users, server-side GA4 tracking (using Google Tag Manager Server-Side) can improve data collection accuracy and privacy compliance. Server-side tracking processes data on your server before sending it to Google, giving you more control over what data is shared and reducing browser-side tracking reliance.

Server-side tracking still requires user consent for cookie-based tracking but can collect aggregated, anonymized data more effectively. It’s more complex to implement and typically suited for high-traffic sites with technical resources.

Best Practices for GA4 and Consent Management

  • Be transparent: Clearly explain in your consent banner that accepting analytics cookies enables Google Analytics tracking
  • Link to privacy policy: Ensure your privacy policy details exactly what data GA4 collects and how it’s used
  • Regular testing: Test your integration monthly to ensure consent management continues working after WordPress or plugin updates
  • Monitor acceptance rates: Use DigiConsent’s analytics dashboard to track cookie acceptance rates and optimize your consent messaging
  • Document configuration: Keep records of your GA4 Measurement ID, configuration settings, and consent setup for compliance audits
  • Review data retention: Configure GA4’s data retention settings appropriately for your jurisdiction’s requirements (Settings → Data Settings → Data Retention)

Working with Multiple GA4 Properties

Some websites need to send data to multiple GA4 properties—for example, one property for the website owner and another for a client or partner. DigiConsent supports this by allowing you to add multiple tracking codes.

To track with multiple properties, add all Measurement IDs to your configuration:

<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'); // First property
  gtag('config', 'G-YYYYYYYYYY'); // Second property
</script>

Both properties receive the same data, and consent management applies to both. When users accept analytics cookies, both properties begin tracking. When users reject cookies, both properties are blocked.

Troubleshooting Consent Mode Signals

Google provides a tool to verify Consent Mode signals are being sent correctly. Install the Google Tag Assistant Chrome extension and use it to inspect your site:

  1. Install Tag Assistant from the Chrome Web Store
  2. Visit your website in Chrome
  3. Click the Tag Assistant extension icon
  4. Click “Connect” to start debugging
  5. Refresh your page with analytics cookies rejected
  6. Check Tag Assistant for consent status signals
  7. Accept cookies and verify consent signals update to “Granted”

Tag Assistant shows exactly what consent signals GA4 receives, making it easy to identify if Consent Mode is configured correctly or if signals aren’t being sent as expected.

Integrating GA4 with DigiConsent creates a privacy-compliant analytics setup that respects user choices while providing valuable data insights. By following this guide and properly configuring both GA4 and Google Consent Mode v2, you’ll maintain GDPR compliance and other privacy regulations while building a comprehensive understanding of your website’s performance and user behavior.