|
1 | 1 | "use client" |
2 | 2 |
|
3 | | -import { useEffect, useState } from "react" |
| 3 | +import { useEffect } from "react" |
4 | 4 | import Script from "next/script" |
5 | 5 | import { hasConsent, onConsentChange } from "@/lib/analytics/consent-manager" |
6 | 6 |
|
7 | | -// Google Tag Manager ID |
| 7 | +// Google Ads Conversion ID |
8 | 8 | const GTM_ID = "AW-17391954825" |
9 | 9 |
|
10 | 10 | /** |
11 | | - * Google Analytics Provider |
12 | | - * Only loads Google Tag Manager after user gives consent |
| 11 | + * Google Analytics Provider with Consent Mode v2 |
| 12 | + * Implements advanced consent mode with cookieless pings |
13 | 13 | */ |
14 | 14 | export function GoogleAnalyticsProvider({ children }: { children: React.ReactNode }) { |
15 | | - const [shouldLoad, setShouldLoad] = useState(false) |
16 | | - |
17 | 15 | useEffect(() => { |
18 | | - // Check initial consent status |
19 | | - if (hasConsent()) { |
20 | | - setShouldLoad(true) |
21 | | - initializeGoogleAnalytics() |
| 16 | + // Initialize dataLayer and gtag immediately for consent mode |
| 17 | + if (typeof window !== "undefined") { |
| 18 | + window.dataLayer = window.dataLayer || [] |
| 19 | + window.gtag = function (...args: unknown[]) { |
| 20 | + window.dataLayer.push(args) |
| 21 | + } |
| 22 | + |
| 23 | + // Set default consent state for Consent Mode v2 |
| 24 | + // This must be called before any Google tags load |
| 25 | + window.gtag("consent", "default", { |
| 26 | + ad_storage: "denied", |
| 27 | + ad_user_data: "denied", |
| 28 | + ad_personalization: "denied", |
| 29 | + analytics_storage: "denied", |
| 30 | + functionality_storage: "denied", |
| 31 | + personalization_storage: "denied", |
| 32 | + security_storage: "granted", // Always granted for security |
| 33 | + wait_for_update: 2000, // Wait up to 2 seconds for consent update |
| 34 | + // Enable cookieless pings for conversion measurement |
| 35 | + // This allows Google Ads to measure conversions without cookies |
| 36 | + url_passthrough: true, // Pass click information via URL parameters |
| 37 | + ads_data_redaction: true, // Redact ads data when consent is denied |
| 38 | + }) |
| 39 | + |
| 40 | + // Check initial consent status and update if already consented |
| 41 | + if (hasConsent()) { |
| 42 | + updateConsentState(true) |
| 43 | + } |
22 | 44 | } |
23 | 45 |
|
24 | 46 | // Listen for consent changes |
25 | 47 | const unsubscribe = onConsentChange((consented) => { |
26 | | - if (consented && !shouldLoad) { |
27 | | - setShouldLoad(true) |
28 | | - initializeGoogleAnalytics() |
29 | | - } |
| 48 | + updateConsentState(consented) |
30 | 49 | }) |
31 | 50 |
|
32 | 51 | return unsubscribe |
33 | | - }, [shouldLoad]) |
| 52 | + }, []) |
34 | 53 |
|
35 | | - const initializeGoogleAnalytics = () => { |
36 | | - // Initialize the dataLayer and gtag function |
37 | | - if (typeof window !== "undefined") { |
38 | | - window.dataLayer = window.dataLayer || [] |
39 | | - window.gtag = function (...args: GtagArgs) { |
40 | | - window.dataLayer.push(args) |
41 | | - } |
42 | | - window.gtag("js", new Date()) |
43 | | - window.gtag("config", GTM_ID) |
44 | | - } |
45 | | - } |
| 54 | + const updateConsentState = (consented: boolean) => { |
| 55 | + if (typeof window !== "undefined" && window.gtag) { |
| 56 | + // Update consent state based on user choice |
| 57 | + window.gtag("consent", "update", { |
| 58 | + ad_storage: consented ? "granted" : "denied", |
| 59 | + ad_user_data: consented ? "granted" : "denied", |
| 60 | + ad_personalization: consented ? "granted" : "denied", |
| 61 | + analytics_storage: consented ? "granted" : "denied", |
| 62 | + functionality_storage: consented ? "granted" : "denied", |
| 63 | + personalization_storage: consented ? "granted" : "denied", |
| 64 | + }) |
46 | 65 |
|
47 | | - // Only render Google Analytics scripts if consent is given |
48 | | - if (!shouldLoad) { |
49 | | - return <>{children}</> |
| 66 | + console.log(`Google Consent Mode updated: ${consented ? "granted" : "denied"}`) |
| 67 | + } |
50 | 68 | } |
51 | 69 |
|
52 | 70 | return ( |
53 | 71 | <> |
54 | | - {/* Google tag (gtag.js) - Only loads after consent */} |
| 72 | + {/* Google tag (gtag.js) - Loads immediately for Consent Mode v2 */} |
55 | 73 | <Script |
56 | 74 | src={`https://www.googletagmanager.com/gtag/js?id=${GTM_ID}`} |
57 | 75 | strategy="afterInteractive" |
58 | 76 | onLoad={() => { |
59 | | - console.log("Google Analytics loaded with consent") |
| 77 | + console.log("Google Analytics loaded with Consent Mode v2") |
60 | 78 | }} |
61 | 79 | /> |
62 | | - <Script id="google-analytics-init" strategy="afterInteractive"> |
| 80 | + <Script id="google-analytics-consent-mode" strategy="afterInteractive"> |
63 | 81 | {` |
64 | 82 | window.dataLayer = window.dataLayer || []; |
65 | 83 | function gtag(){dataLayer.push(arguments);} |
66 | 84 | gtag('js', new Date()); |
67 | | - gtag('config', '${GTM_ID}'); |
| 85 | + |
| 86 | + // Configure with enhanced measurement and cookieless pings |
| 87 | + gtag('config', '${GTM_ID}', { |
| 88 | + allow_google_signals: false, // Disable by default, enabled when consent granted |
| 89 | + allow_ad_personalization_signals: false, // Disable by default |
| 90 | + // Enable enhanced conversions for better measurement |
| 91 | + enhanced_conversions: { |
| 92 | + automatic: true |
| 93 | + }, |
| 94 | + // Send page view for cookieless measurement |
| 95 | + send_page_view: true |
| 96 | + }); |
68 | 97 | `} |
69 | 98 | </Script> |
70 | 99 | {children} |
71 | 100 | </> |
72 | 101 | ) |
73 | 102 | } |
74 | 103 |
|
75 | | -// Type definitions for Google Analytics |
76 | | -type GtagArgs = ["js", Date] | ["config", string, GtagConfig?] | ["event", string, GtagEventParameters?] |
77 | | - |
78 | | -interface GtagConfig { |
79 | | - [key: string]: unknown |
80 | | -} |
81 | | - |
82 | | -interface GtagEventParameters { |
83 | | - [key: string]: unknown |
84 | | -} |
85 | | - |
86 | 104 | // Declare global types for TypeScript |
87 | 105 | declare global { |
88 | 106 | interface Window { |
89 | | - dataLayer: GtagArgs[] |
90 | | - gtag: (...args: GtagArgs) => void |
| 107 | + dataLayer: unknown[] |
| 108 | + gtag: (...args: unknown[]) => void |
91 | 109 | } |
92 | 110 | } |
0 commit comments