feat: Implement analytics tracking with cookie consent and enhance SEO with backlink management.
This commit is contained in:
@@ -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 (
|
||||
<>
|
||||
|
||||
Reference in New Issue
Block a user