@@ -13,35 +13,29 @@ const GADS_ID = "AW-17391954825"
1313 */
1414export function GoogleAnalyticsProvider ( { children } : { children : React . ReactNode } ) {
1515 useEffect ( ( ) => {
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- url_passthrough : true , // Pass click information via URL parameters
36- ads_data_redaction : true , // Redact ads data when consent is denied
37- } )
16+ const updateConsentState = ( consented : boolean ) => {
17+ if ( typeof window !== "undefined" && window . gtag ) {
18+ // Update consent state based on user choice
19+ window . gtag ( "consent" , "update" , {
20+ ad_storage : consented ? "granted" : "denied" ,
21+ ad_user_data : consented ? "granted" : "denied" ,
22+ ad_personalization : consented ? "granted" : "denied" ,
23+ analytics_storage : consented ? "granted" : "denied" ,
24+ functionality_storage : consented ? "granted" : "denied" ,
25+ personalization_storage : consented ? "granted" : "denied" ,
26+ } )
3827
39- // Check initial consent status and update if already consented
40- if ( hasConsent ( ) ) {
41- updateConsentState ( true )
28+ if ( process . env . NODE_ENV === "development" ) {
29+ console . log ( `Google Consent Mode updated: ${ consented ? "granted" : "denied" } ` )
30+ }
4231 }
4332 }
4433
34+ // Check initial consent status and update if already consented
35+ if ( hasConsent ( ) ) {
36+ updateConsentState ( true )
37+ }
38+
4539 // Listen for consent changes
4640 const unsubscribe = onConsentChange ( ( consented ) => {
4741 updateConsentState ( consented )
@@ -50,24 +44,6 @@ export function GoogleAnalyticsProvider({ children }: { children: React.ReactNod
5044 return unsubscribe
5145 } , [ ] )
5246
53- const updateConsentState = ( consented : boolean ) => {
54- if ( typeof window !== "undefined" && window . gtag ) {
55- // Update consent state based on user choice
56- window . gtag ( "consent" , "update" , {
57- ad_storage : consented ? "granted" : "denied" ,
58- ad_user_data : consented ? "granted" : "denied" ,
59- ad_personalization : consented ? "granted" : "denied" ,
60- analytics_storage : consented ? "granted" : "denied" ,
61- functionality_storage : consented ? "granted" : "denied" ,
62- personalization_storage : consented ? "granted" : "denied" ,
63- } )
64-
65- if ( process . env . NODE_ENV === "development" ) {
66- console . log ( `Google Consent Mode updated: ${ consented ? "granted" : "denied" } ` )
67- }
68- }
69- }
70-
7147 return (
7248 < >
7349 { /* Google tag (gtag.js) - Loads with Consent Mode v2 */ }
@@ -82,6 +58,24 @@ export function GoogleAnalyticsProvider({ children }: { children: React.ReactNod
8258 />
8359 < Script id = "google-ads-config" strategy = "afterInteractive" >
8460 { `
61+ // Initialize dataLayer and gtag function first (must be before any gtag calls)
62+ window.dataLayer = window.dataLayer || [];
63+ function gtag(){dataLayer.push(arguments);}
64+
65+ // Set default consent state for Consent Mode v2 (must be before gtag.js loads)
66+ gtag('consent', 'default', {
67+ ad_storage: 'denied',
68+ ad_user_data: 'denied',
69+ ad_personalization: 'denied',
70+ analytics_storage: 'denied',
71+ functionality_storage: 'denied',
72+ personalization_storage: 'denied',
73+ security_storage: 'granted',
74+ wait_for_update: 2000,
75+ url_passthrough: true,
76+ ads_data_redaction: true
77+ });
78+
8579 gtag('js', new Date());
8680
8781 // Configure Google Ads with enhanced measurement
0 commit comments