This commit is contained in:
Nicholas Koben Kao
2024-01-19 19:38:35 -08:00
parent b2690ab2c5
commit a2d0e93172
6 changed files with 78 additions and 15 deletions
+29
View File
@@ -0,0 +1,29 @@
// app/PostHogPageView.tsx
'use client'
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import { usePostHog } from 'posthog-js/react';
export default function PostHogPageView() {
const pathname = usePathname();
const searchParams = useSearchParams();
const posthog = usePostHog();
// Track pageviews
useEffect(() => {
if (pathname && posthog) {
let url = window.origin + pathname
if (searchParams.toString()) {
url = url + `?${searchParams.toString()}`
}
posthog.capture(
'$pageview',
{
'$current_url': url,
}
)
}
}, [pathname, searchParams, posthog])
return null
}