feat: Implement analytics tracking with cookie consent and enhance SEO with backlink management.

This commit is contained in:
Timo Knuth
2026-02-09 16:07:39 +01:00
parent e871c820f7
commit f3774f3068
7 changed files with 264 additions and 69 deletions

View File

@@ -12,15 +12,29 @@ export function PostHogPageView() {
useEffect(() => {
const cookieConsent = localStorage.getItem('cookieConsent');
if (cookieConsent === 'accepted' && pathname && (posthog as any)._loaded) {
let url = window.origin + pathname;
if (searchParams && searchParams.toString()) {
url = url + `?${searchParams.toString()}`;
}
if (cookieConsent === 'accepted' && pathname) {
// If posthog is loaded, capture immediately
if ((posthog as any)._loaded) {
let url = window.origin + pathname;
if (searchParams && searchParams.toString()) {
url = url + `?${searchParams.toString()}`;
}
posthog.capture('$pageview', {
$current_url: url,
});
posthog.capture('$pageview', {
$current_url: url,
});
} else {
// If not loaded yet, wait for it
posthog.onFeatureFlags(() => {
let url = window.origin + pathname;
if (searchParams && searchParams.toString()) {
url = url + `?${searchParams.toString()}`;
}
posthog.capture('$pageview', {
$current_url: url,
});
});
}
}
}, [pathname, searchParams]);
@@ -28,13 +42,7 @@ export function PostHogPageView() {
}
export function PostHogProvider({ children }: { children: React.ReactNode }) {
const [initializationAttempted, setInitializationAttempted] = useState(false);
// Initialize PostHog once
useEffect(() => {
if (initializationAttempted) return;
setInitializationAttempted(true);
const cookieConsent = localStorage.getItem('cookieConsent');
if (cookieConsent === 'accepted') {
@@ -62,7 +70,7 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
}
}
}
}, [initializationAttempted]);
}, []);
return (
<>