BlogTechnical Compliance
Technical Compliance7 min read2025-11-28

How to Stop Google Analytics Before Consent (GDPR Fix)

Step-by-step guide to properly blocking Google Analytics until users give consent. Avoid €20M GDPR fines with this complete implementation guide.

G
Guardian Team
Privacy Compliance Expert
How to Stop Google Analytics Before Consent (GDPR Fix)

Loading Google Analytics before user consent is the #1 GDPR violation on small business websites. It's also one of the easiest to fix. This guide shows you exactly how to implement consent-based Google Analytics in 2025.

Why This Matters

Under GDPR, you cannot track users without their explicit consent. Yet the default Google Analytics installation starts tracking immediately when your page loads - before users even see your cookie banner.

The Legal Risk

  • GDPR violations: Up to €20M or 4% of revenue
  • Recent precedents: Austria, France, and Italy have ruled Google Analytics violates GDPR
  • User complaints: Anyone can report your site to Data Protection Authorities
  • Business impact: Loss of trust, legal costs, operational disruption

What Counts as "Tracking"?

Under GDPR, these all require consent:

  • Setting cookies on user's device
  • Collecting IP addresses
  • Recording user behavior
  • Sending data to third-party servers (like Google)
  • Creating user profiles or fingerprints

Standard Google Analytics does ALL of these by default.

How Google Analytics Currently Violates GDPR

The Default Installation Problem

When you install Google Analytics using the standard code:

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'GA_MEASUREMENT_ID');
</script>

This happens immediately:

  1. Script loads when page loads
  2. Cookies are set (_ga, _gid, _gat)
  3. User IP address is collected
  4. Page view is sent to Google
  5. User tracking begins

All before your cookie banner even appears.

What Regulators See

Data Protection Authorities have explicitly stated:

  • "Consent must be obtained BEFORE tracking begins"
  • "Pre-loaded analytics cookies violate Article 5(3) ePrivacy Directive"
  • "IP addresses are personal data under GDPR"
  • "Google Analytics transfers data to US without adequate safeguards"

3 Ways to Fix Google Analytics Consent

Method 1: Google Consent Mode (Recommended)

Google's official solution that delays tracking until consent.

Step 1: Update Your Analytics Code

<!-- Google Consent Mode Setup -->
<script>
  // Set default consent to 'denied' 
  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
  });
  
  gtag('js', new Date());
  gtag('config', 'GA_MEASUREMENT_ID');
</script>

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

Step 2: Update Consent When User Accepts

// When user clicks "Accept All" in cookie banner
function grantAnalyticsConsent() {
  gtag('consent', 'update', {
    'analytics_storage': 'granted',
    'ad_storage': 'granted',
    'ad_user_data': 'granted',
    'ad_personalization': 'granted'
  });
}

// When user clicks "Reject All"
function denyAnalyticsConsent() {
  gtag('consent', 'update', {
    'analytics_storage': 'denied',
    'ad_storage': 'denied',
    'ad_user_data': 'denied',
    'ad_personalization': 'denied'
  });
}

Pros and Cons

Pros:

  • Official Google solution
  • Maintains some basic analytics even without consent
  • Works with Google Ads and other Google products
  • Easy to implement

Cons:

  • Still sends ping to Google (some DPAs consider this non-compliant)
  • Modeled conversions may not be accurate enough
  • Requires trust in Google's implementation

Method 2: Conditional Script Loading (Most Compliant)

Don't load Google Analytics script AT ALL until consent is given.

Step 1: Remove Standard Analytics Code

Delete the auto-generated Google Analytics code from your site.

Step 2: Load Script Only After Consent

// Check if user has consented (from cookie banner)
function loadGoogleAnalytics() {
  // Only run if user gave consent
  if (!hasAnalyticsConsent()) return;
  
  // Create script element
  const script = document.createElement('script');
  script.async = true;
  script.src = 'https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID';
  
  // Add to page
  document.head.appendChild(script);
  
  // Initialize after script loads
  script.onload = function() {
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'GA_MEASUREMENT_ID');
  };
}

// Call this when user accepts cookies
document.getElementById('accept-cookies').addEventListener('click', function() {
  setAnalyticsConsent(true); // Save consent
  loadGoogleAnalytics(); // Load GA
});

Pros and Cons

Pros:

  • 100% GDPR compliant - zero tracking before consent
  • No communication with Google until consent
  • Complete control over when tracking starts
  • Preferred by strict Data Protection Authorities

Cons:

  • No analytics data from users who reject
  • Slightly more complex implementation
  • Can't use Google's modeled conversions

Method 3: Google Tag Manager with Consent (For Advanced Users)

Use GTM to manage all tracking with built-in consent controls.

Step 1: Set Up Consent Mode in GTM

  1. Go to Google Tag Manager
  2. Create new tag: "Consent Initialization"
  3. Tag Type: "Consent Initialization - Google tags"
  4. Set default values to "Denied"
  5. Trigger: "Consent Initialization - All Pages"

Step 2: Configure Analytics Tag

  1. Edit your Google Analytics tag
  2. Advanced Settings → Consent Settings
  3. Require "Analytics Storage" consent
  4. Tag will only fire when consent is granted

Step 3: Update Consent from Cookie Banner

// When user accepts cookies
function updateGTMConsent(analyticsAllowed, adsAllowed) {
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    'event': 'consent_update',
    'analytics_storage': analyticsAllowed ? 'granted' : 'denied',
    'ad_storage': adsAllowed ? 'granted' : 'denied'
  });
}

Pros and Cons

Pros:

  • Manage all tags (GA, Facebook, etc.) in one place
  • Easy to add/remove tracking tools
  • Built-in consent management
  • Version control and testing

Cons:

  • Requires learning GTM
  • More complex initial setup
  • Another tool to maintain

How to Test Your Implementation

Before Testing

  1. Clear all cookies
  2. Open incognito/private window
  3. Open Developer Tools (F12)

Test 1: Check Cookies Before Consent

  1. Open your website (don't click cookie banner)
  2. Go to Application → Cookies in DevTools
  3. Should NOT see: _ga, _gid, _gat cookies
  4. If you see these cookies = FAILED (still violating GDPR)

Test 2: Check Network Requests

  1. Open Network tab in DevTools
  2. Filter by "google-analytics.com" or "googletagmanager.com"
  3. Reload page WITHOUT clicking cookie banner
  4. Should NOT see: Any requests to Google
  5. If you see requests = FAILED

Test 3: Verify Consent Works

  1. Click "Accept All" in your cookie banner
  2. Check Application → Cookies again
  3. Should NOW see: _ga, _gid cookies
  4. Check Network tab
  5. Should NOW see: Requests to google-analytics.com
  6. If both appear = PASSED

Test 4: Verify Rejection Works

  1. Clear cookies, reload in incognito
  2. Click "Reject All" in cookie banner
  3. Check cookies and network
  4. Should STILL see: NO Google Analytics activity
  5. Navigate to other pages
  6. Should remain: No tracking

Common Mistakes to Avoid

1. Cookie Banner Loads After Analytics

Problem: Analytics script is in <head>, cookie banner loads at end of <body>.

Fix: Either load analytics script at end, or use consent mode to block by default.

2. Consent Mode Set to "Granted" by Default

Problem: Consent mode defaults to 'granted' waiting for banner to deny.

Fix: Always default to 'denied', update to 'granted' on consent.

3. Consent Not Saved Across Pages

Problem: Banner appears on every page load.

Fix: Save consent choice in cookie/localStorage, check on page load.

4. Multiple Analytics Snippets

Problem: Old GA code still in theme, new code added to header.

Fix: Search entire codebase for "gtag" and "google-analytics" - remove duplicates.

5. Tag Manager AND Hard-Coded Analytics

Problem: GA loaded both via GTM and direct script.

Fix: Choose one method, remove the other completely.

Platform-Specific Instructions

WordPress

If using a plugin like MonsterInsights or GA Google Analytics:

  1. Go to plugin settings
  2. Look for "GDPR" or "Cookie Consent" section
  3. Enable "Wait for consent before tracking"
  4. Connect to your cookie consent plugin

Or use a dedicated cookie consent plugin that integrates with analytics.

Shopify

  1. Go to Settings → Customer Privacy
  2. Enable "Cookie banner"
  3. Shopify automatically delays analytics until consent
  4. For custom GA code, use consent mode method above

Webflow

  1. Remove GA code from Project Settings
  2. Add consent mode code to <head> custom code
  3. Add consent update function to cookie banner buttons
  4. Test thoroughly

React/Next.js

// useEffect to load GA after consent
useEffect(() => {
  if (hasUserConsented()) {
    loadGoogleAnalytics();
  }
}, []);

function loadGoogleAnalytics() {
  const script = document.createElement('script');
  script.src = `https://www.googletagmanager.com/gtag/js?id=${GA_ID}`;
  script.async = true;
  document.head.appendChild(script);
  
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', GA_ID);
}

What About Google Analytics 4?

GA4 has the same consent requirements as Universal Analytics:

  • Still requires consent before tracking
  • Still sets cookies before consent by default
  • Consent Mode works with GA4
  • All the fixes above apply to GA4

GA4's "privacy-focused" features don't exempt you from GDPR consent requirements.

Alternatives to Google Analytics

If Google Analytics consent management is too complex, consider privacy-first alternatives:

No Consent Required

  • Plausible: No cookies, doesn't require consent
  • Fathom: Privacy-first, GDPR compliant by default
  • Simple Analytics: Cookieless tracking
  • Matomo (self-hosted with config): Can be GDPR-exempt

Why These Don't Need Consent

  • Don't use cookies or fingerprinting
  • Don't track across sites
  • Don't share data with third parties
  • Anonymize IP addresses
  • Data stored in EU

Trade-off: Less detailed user insights, but zero compliance headaches.

Maintaining Compliance Long-Term

Monthly Checks

  • Test analytics in incognito mode
  • Verify no tracking before consent
  • Check for plugin/theme updates that break consent

After Updates

  • Theme updates can restore default analytics code
  • Plugin updates might reset consent settings
  • Always test after major site changes

Documentation

Keep records of:

  • How you implemented consent
  • When you made changes
  • Test results confirming compliance
  • Consent rates and user choices

This demonstrates good faith effort if ever audited.

Conclusion

Fixing Google Analytics consent is non-negotiable for GDPR compliance. The good news: it's straightforward with the right approach.

Quick Recommendation:

  • Small sites: Use Consent Mode (Method 1)
  • Strict compliance: Use Conditional Loading (Method 2)
  • Multiple tools: Use Google Tag Manager (Method 3)
  • Want simple: Switch to privacy-first analytics

Whichever method you choose, test it thoroughly. Your analytics might take a small hit from users who reject cookies, but that's the price of compliance - and it's far cheaper than a €20M fine.

Need help implementing? Guardian of Compliance automatically handles consent for Google Analytics, Facebook Pixel, and all other tracking scripts. A few lines of code, complete compliance.

Need Help with Compliance?

Use my free tool to check your website's compliance status.

Related Articles