Skip to content

Commit 346b4de

Browse files
committed
Attemot at compliant, cookie-less anonymous tracking for the website
1 parent 4a096e1 commit 346b4de

File tree

3 files changed

+49
-46
lines changed

3 files changed

+49
-46
lines changed

apps/web-roo-code/src/components/CookieConsentWrapper.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ReactCookieConsent from "react-cookie-consent"
55
import { Cookie } from "lucide-react"
66
import { getDomain } from "tldts"
77
import { CONSENT_COOKIE_NAME } from "@roo-code/types"
8-
import { dispatchConsentEvent } from "@/lib/analytics/consent-manager"
8+
import { handleConsentAccept, handleConsentReject } from "@/lib/analytics/consent-manager"
99

1010
/**
1111
* GDPR-compliant cookie consent banner component
@@ -23,11 +23,11 @@ export function CookieConsentWrapper() {
2323
}, [])
2424

2525
const handleAccept = () => {
26-
dispatchConsentEvent(true)
26+
handleConsentAccept()
2727
}
2828

2929
const handleDecline = () => {
30-
dispatchConsentEvent(false)
30+
handleConsentReject()
3131
}
3232

3333
const extraCookieOptions = cookieDomain

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

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import { usePathname, useSearchParams } from "next/navigation"
44
import posthog from "posthog-js"
55
import { PostHogProvider as OriginalPostHogProvider } from "posthog-js/react"
6-
import { useEffect, Suspense, useState } from "react"
7-
import { hasConsent, onConsentChange } from "@/lib/analytics/consent-manager"
6+
import { useEffect, Suspense } from "react"
87

98
function PageViewTracker() {
109
const pathname = usePathname()
@@ -28,11 +27,9 @@ function PageViewTracker() {
2827
}
2928

3029
export function PostHogProvider({ children }: { children: React.ReactNode }) {
31-
const [isInitialized, setIsInitialized] = useState(false)
32-
3330
useEffect(() => {
34-
// Initialize PostHog only on the client side AND when consent is given
35-
if (typeof window !== "undefined") {
31+
// Initialize PostHog immediately on the client side
32+
if (typeof window !== "undefined" && !posthog.__loaded) {
3633
const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY
3734
const posthogHost = process.env.NEXT_PUBLIC_POSTHOG_HOST
3835

@@ -52,48 +49,29 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
5249
)
5350
}
5451

55-
const initializePosthog = () => {
56-
if (!isInitialized) {
57-
posthog.init(posthogKey, {
58-
api_host: posthogHost || "https://us.i.posthog.com",
59-
capture_pageview: false,
60-
loaded: (posthogInstance) => {
61-
if (process.env.NODE_ENV === "development") {
62-
posthogInstance.debug()
63-
}
64-
},
65-
respect_dnt: true, // Respect Do Not Track
66-
})
67-
setIsInitialized(true)
68-
}
69-
}
70-
71-
// Check initial consent status
72-
if (hasConsent()) {
73-
initializePosthog()
74-
}
75-
76-
// Listen for consent changes
77-
const unsubscribe = onConsentChange((consented) => {
78-
if (consented && !isInitialized) {
79-
initializePosthog()
80-
}
52+
// Initialize PostHog with cookieless mode support
53+
posthog.init(posthogKey, {
54+
api_host: posthogHost || "https://us.i.posthog.com",
55+
capture_pageview: false, // We handle pageview tracking manually
56+
loaded: (posthogInstance) => {
57+
if (process.env.NODE_ENV === "development") {
58+
posthogInstance.debug()
59+
}
60+
},
61+
save_referrer: true, // Save referrer information
62+
save_campaign_params: true, // Save UTM parameters
63+
respect_dnt: true, // Respect Do Not Track
64+
persistence: "memory", // Default persistence with cookies
65+
opt_out_capturing_by_default: false, // Start tracking immediately
8166
})
82-
83-
return () => {
84-
unsubscribe()
85-
}
8667
}
87-
}, [isInitialized])
68+
}, [])
8869

89-
// Only provide PostHog context if it's initialized
9070
return (
9171
<OriginalPostHogProvider client={posthog}>
92-
{isInitialized && (
93-
<Suspense fallback={null}>
94-
<PageViewTracker />
95-
</Suspense>
96-
)}
72+
<Suspense fallback={null}>
73+
<PageViewTracker />
74+
</Suspense>
9775
{children}
9876
</OriginalPostHogProvider>
9977
)

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { getCookieConsentValue } from "react-cookie-consent"
77
import { CONSENT_COOKIE_NAME } from "@roo-code/types"
8+
import posthog from "posthog-js"
89

910
export const CONSENT_EVENT = "cookieConsentChanged"
1011

@@ -45,3 +46,27 @@ export function onConsentChange(callback: (consented: boolean) => void): () => v
4546
window.addEventListener(CONSENT_EVENT, handler)
4647
return () => window.removeEventListener(CONSENT_EVENT, handler)
4748
}
49+
50+
/**
51+
* Handle user accepting cookies
52+
* Opts PostHog back into cookie-based tracking
53+
*/
54+
export function handleConsentAccept(): void {
55+
if (typeof window !== "undefined" && posthog.__loaded) {
56+
// User accepted - ensure cookie tracking is enabled
57+
posthog.opt_in_capturing()
58+
posthog.set_config({
59+
persistence: "localStorage+cookie",
60+
})
61+
}
62+
dispatchConsentEvent(true)
63+
}
64+
65+
/**
66+
* Handle user rejecting cookies
67+
* Switches PostHog to cookieless (memory-only) mode
68+
*/
69+
export function handleConsentReject(): void {
70+
// User rejected - stick to cookieless mode
71+
dispatchConsentEvent(false)
72+
}

0 commit comments

Comments
 (0)