Skip to content

Commit 72febde

Browse files
feat(website): add scroll depth tracking
1 parent 82679db commit 72febde

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

website/components/PostHogProvider.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import posthog from "posthog-js";
44
import { PostHogProvider as PHProvider } from "posthog-js/react";
5-
import { useEffect, useState } from "react";
5+
import { useEffect, useState, useRef } from "react";
66
import { CookieConsent, getConsentStatus, type ConsentStatus } from "./CookieConsent";
77

88
function initPostHog() {
@@ -25,9 +25,39 @@ function initPostHog() {
2525
}
2626
}
2727

28+
function useScrollDepthTracking(enabled: boolean) {
29+
const trackedDepths = useRef<Set<number>>(new Set());
30+
31+
useEffect(() => {
32+
if (!enabled || typeof window === "undefined") return;
33+
34+
const handleScroll = () => {
35+
const scrollTop = window.scrollY;
36+
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
37+
if (docHeight <= 0) return;
38+
39+
const scrollPercent = Math.round((scrollTop / docHeight) * 100);
40+
const thresholds = [25, 50, 75, 100];
41+
42+
for (const threshold of thresholds) {
43+
if (scrollPercent >= threshold && !trackedDepths.current.has(threshold)) {
44+
trackedDepths.current.add(threshold);
45+
posthog.capture("scroll_depth", { depth: threshold });
46+
}
47+
}
48+
};
49+
50+
window.addEventListener("scroll", handleScroll, { passive: true });
51+
return () => window.removeEventListener("scroll", handleScroll);
52+
}, [enabled]);
53+
}
54+
2855
export function PostHogProvider({ children }: { children: React.ReactNode }) {
2956
const [consentStatus, setConsentStatus] = useState<ConsentStatus>("pending");
3057

58+
// Track scroll depth when consent is granted
59+
useScrollDepthTracking(consentStatus === "granted");
60+
3161
useEffect(() => {
3262
const status = getConsentStatus();
3363
setConsentStatus(status);

0 commit comments

Comments
 (0)