Skip to content

Commit 1d131e4

Browse files
committed
refactor: improve Google Consent Mode v2 implementation based on review
- Renamed GTM_ID to GADS_ID for clarity - Moved url_passthrough and ads_data_redaction to gtag set commands - Added conditional logging for development mode only - Fixed beforeInteractive script strategy issue in Next.js App Router
1 parent 807ea2d commit 1d131e4

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

apps/web-roo-code/src/components/providers/google-analytics-provider.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Script from "next/script"
55
import { hasConsent, onConsentChange } from "@/lib/analytics/consent-manager"
66

77
// Google Ads Conversion ID
8-
const GTM_ID = "AW-17391954825"
8+
const GADS_ID = "AW-17391954825"
99

1010
/**
1111
* Google Analytics Provider with Consent Mode v2
@@ -31,12 +31,13 @@ export function GoogleAnalyticsProvider({ children }: { children: React.ReactNod
3131
personalization_storage: "denied",
3232
security_storage: "granted", // Always granted for security
3333
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
3834
})
3935

36+
// Enable cookieless pings for conversion measurement
37+
// These must be set separately with gtag 'set' command
38+
window.gtag("set", "ads_data_redaction", true) // Redact ads data when consent is denied
39+
window.gtag("set", "url_passthrough", true) // Pass click information via URL parameters
40+
4041
// Check initial consent status and update if already consented
4142
if (hasConsent()) {
4243
updateConsentState(true)
@@ -63,28 +64,28 @@ export function GoogleAnalyticsProvider({ children }: { children: React.ReactNod
6364
personalization_storage: consented ? "granted" : "denied",
6465
})
6566

66-
console.log(`Google Consent Mode updated: ${consented ? "granted" : "denied"}`)
67+
if (process.env.NODE_ENV === "development") {
68+
console.log(`Google Consent Mode updated: ${consented ? "granted" : "denied"}`)
69+
}
6770
}
6871
}
6972

7073
return (
7174
<>
72-
{/* Google tag (gtag.js) - Loads immediately for Consent Mode v2 */}
75+
{/* Google tag (gtag.js) - Loads with Consent Mode v2 */}
7376
<Script
74-
src={`https://www.googletagmanager.com/gtag/js?id=${GTM_ID}`}
77+
src={`https://www.googletagmanager.com/gtag/js?id=${GADS_ID}`}
7578
strategy="afterInteractive"
7679
onLoad={() => {
77-
console.log("Google Analytics loaded with Consent Mode v2")
80+
if (process.env.NODE_ENV === "development") {
81+
console.log("Google Analytics loaded with Consent Mode v2")
82+
}
7883
}}
7984
/>
80-
<Script id="google-analytics-consent-mode" strategy="afterInteractive">
85+
<Script id="google-ads-config" strategy="afterInteractive">
8186
{`
82-
window.dataLayer = window.dataLayer || [];
83-
function gtag(){dataLayer.push(arguments);}
84-
gtag('js', new Date());
85-
86-
// Configure with enhanced measurement and cookieless pings
87-
gtag('config', '${GTM_ID}', {
87+
// Configure Google Ads with enhanced measurement
88+
gtag('config', '${GADS_ID}', {
8889
allow_google_signals: false, // Disable by default, enabled when consent granted
8990
allow_ad_personalization_signals: false, // Disable by default
9091
// Enable enhanced conversions for better measurement

apps/web-roo-code/src/lib/analytics/consent-manager.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ export function handleConsentAccept(): void {
7272
functionality_storage: "granted",
7373
personalization_storage: "granted",
7474
})
75-
console.log("User accepted cookies - Google Consent Mode updated to granted")
75+
76+
if (process.env.NODE_ENV === "development") {
77+
console.log("User accepted cookies - Google Consent Mode updated to granted")
78+
}
7679
}
7780
}
7881
dispatchConsentEvent(true)
@@ -94,7 +97,10 @@ export function handleConsentReject(): void {
9497
functionality_storage: "denied",
9598
personalization_storage: "denied",
9699
})
97-
console.log("User rejected cookies - Google Consent Mode updated to denied")
100+
101+
if (process.env.NODE_ENV === "development") {
102+
console.log("User rejected cookies - Google Consent Mode updated to denied")
103+
}
98104
}
99105
}
100106
// User rejected - stick to cookieless mode

apps/web-roo-code/src/lib/analytics/google-ads.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export function trackGoogleAdsConversion() {
2323
transaction_id: `${Date.now()}`, // Unique ID to prevent duplicates
2424
})
2525

26-
console.log("Google Ads conversion tracked with Consent Mode v2")
26+
if (process.env.NODE_ENV === "development") {
27+
console.log("Google Ads conversion tracked with Consent Mode v2")
28+
}
2729
}
2830
}
2931

0 commit comments

Comments
 (0)