Implementing a Compliant GDPR & CCPA Cookie Consent Banner with Google Consent Mode v2

The Quick Answer

Google Consent Mode v2 requires dispatching a default consent state before tag execution (`gtag("consent", "default", {...})`) and updating granted parameters (`analytics_storage`, `ad_storage`, `ad_user_data`, `ad_personalization`) via `gtag("consent", "update", {...})` upon user acceptance.

Interactive Demo Live Preview
🍪 Privacy & Cookie Preferences

We use essential cookies and Google Consent Mode v2 signals to measure site performance responsibly.

Step 1: Initialize Consent Mode v2 Defaults in Document Head

Declare default denied states before any analytics or third-party ad scripts are parsed.

consent-head.html
<!-- Google Tag Manager & Consent Mode v2 -->
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){ dataLayer.push(arguments); }

  (function() {
    try {
      var saved = JSON.parse(localStorage.getItem('user_cookie_consent'));
      if (saved) {
        gtag('consent', 'default', {
          'analytics_storage': saved.analytics ? 'granted' : 'denied',
          'ad_storage': saved.marketing ? 'granted' : 'denied',
          'ad_user_data': saved.marketing ? 'granted' : 'denied',
          'ad_personalization': saved.marketing ? 'granted' : 'denied'
        });
      } else {
        gtag('consent', 'default', {
          'analytics_storage': 'denied',
          'ad_storage': 'denied',
          'ad_user_data': 'denied',
          'ad_personalization': 'denied',
          'wait_for_update': 500
        });
      }
    } catch(e) {}
  })();
  gtag('js', new Date());
</script>

Step 2: Build the Floating Privacy Consent Banner

Create the floating dialog with Quick Accept, Reject Non-Essential, and Custom Preferences options.

cookie-banner.html
<aside class="cookie-banner" id="cookieBanner" role="dialog" aria-modal="true" aria-labelledby="consentTitle">
  <div class="banner-content">
    <h3 id="consentTitle">🍪 Cookie & Privacy Preferences</h3>
    <p>We use essential cookies to maintain security and optional analytics cookies to enhance performance. Learn more in our <a href="/privacy/">Privacy Policy</a>.</p>
  </div>
  <div class="banner-actions">
    <button type="button" class="btn-reject" id="btnReject">Reject Optional</button>
    <button type="button" class="btn-customize" id="btnCustomize">Preferences</button>
    <button type="button" class="btn-accept" id="btnAccept">Accept All</button>
  </div>
</aside>

Step 3: Store Preferences and Dispatch Consent Mode Updates

Save choice payload into localStorage and notify Google Tag Manager via gtag consent update.

cookie-controller.js
const banner = document.getElementById('cookieBanner');

function updateConsent(analytics, marketing) {
  const payload = {
    analytics_storage: analytics ? 'granted' : 'denied',
    ad_storage: marketing ? 'granted' : 'denied',
    ad_user_data: marketing ? 'granted' : 'denied',
    ad_personalization: marketing ? 'granted' : 'denied'
  };

  if (typeof gtag === 'function') {
    gtag('consent', 'update', payload);
  }

  localStorage.setItem('user_cookie_consent', JSON.stringify({
    analytics, marketing, timestamp: Date.now()
  }));

  banner.classList.add('is-hidden');
}

document.getElementById('btnAccept').addEventListener('click', () => updateConsent(true, true));
document.getElementById('btnReject').addEventListener('click', () => updateConsent(false, false));

Next-Level Addons: Elevating Your Implementation

Take this technique further by combining it with complementary design token studios and animation tools:

gtag('consent', 'update') [OK]

1. Google Consent Mode v2 gtag Dispatch

Explore in Cookie Consent Banner Studio →

Emit gtag("consent", "update") events updating ad_storage, analytics_storage, ad_user_data, and ad_personalization.

ANALYTICS: ON

2. Granular Category Toggles (Analytics / Ads)

Explore in Cookie Consent Banner Studio →

Provide modal check toggles allowing visitors to grant analytics consent while declining personalized marketing pixels.

🍪

3. Mini Re-open Floating Badge

Explore in Cookie Consent Banner Studio →

Render a non-intrusive bottom corner badge allowing users to review and change their cookie preferences at any time.

Chaam
Chaam's Pro Tip

Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.

Why I Built the Cookie Consent Manager Studio

I got tired of guessing values and testing browser quirks in DevTools, so I built this tool to tweak and preview values in real time with 1-click code export.

Open in Cookie Consent Manager Studio ↗

Frequently Asked Questions

Practical answers to common implementation questions, browser quirks, and performance tips.

What is the penalty for not implementing Google Consent Mode v2?

Google Ads accounts operating in the European Economic Area (EEA) without Consent Mode v2 lose audience measurement, remarketing capability, and automated conversion bidding.

What are the 4 key parameters in Consent Mode v2?

The parameters are `analytics_storage` (Google Analytics 4), `ad_storage` (ad cookies), `ad_user_data` (sending user data to Google for ads), and `ad_personalization` (remarketing).

Can strictly necessary cookies be rejected by visitors?

No, under GDPR and ePrivacy regulations, strictly necessary cookies (authentication sessions, CSRF protection, shopping carts) do not require prior consent.

How long should cookie consent persistence last in localStorage?

European data protection authorities generally recommend expiring consent records after 6 to 12 months, prompting returning visitors to renew preferences.

Does this banner require external consent management platforms (CMPs)?

No, this implementation is 100% zero-dependency vanilla JS and CSS, eliminating heavy CMP scripts that degrade Core Web Vitals.

← Back to all blog guides Launch Generator Studio →