From df48b3e6d55afe0589e8bb5befad207762706a74 Mon Sep 17 00:00:00 2001 From: xiften <108333567+not-ani@users.noreply.github.com> Date: Wed, 20 Nov 2024 09:30:50 -0700 Subject: [PATCH] fix(website): posthog analytics --- apps/website/src/app/_analytics/capture.tsx | 26 +++++++++++++++++++++ apps/website/src/app/layout.tsx | 14 ++++++++--- apps/website/src/app/provides/posthog.tsx | 5 +++- 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 apps/website/src/app/_analytics/capture.tsx diff --git a/apps/website/src/app/_analytics/capture.tsx b/apps/website/src/app/_analytics/capture.tsx new file mode 100644 index 0000000..02ce1c6 --- /dev/null +++ b/apps/website/src/app/_analytics/capture.tsx @@ -0,0 +1,26 @@ +// app/PostHogPageView.jsx +"use client"; + +import { useEffect } from "react"; +import { usePathname, useSearchParams } from "next/navigation"; +import { usePostHog } from "posthog-js/react"; + +export default function PostHogPageView() { + const pathname = usePathname(); + const searchParams = useSearchParams(); + const posthog = usePostHog(); + useEffect(() => { + // Track pageviews + 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; +} diff --git a/apps/website/src/app/layout.tsx b/apps/website/src/app/layout.tsx index 171fabc..70c47de 100644 --- a/apps/website/src/app/layout.tsx +++ b/apps/website/src/app/layout.tsx @@ -1,6 +1,7 @@ import "~/app/globals.css"; import type { Metadata } from "next"; +import dynamic from "next/dynamic"; import { GeistSans } from "geist/font/sans"; import { CSPostHogProvider } from "./provides/posthog"; @@ -69,6 +70,10 @@ export const metadata: Metadata = { ], }; +const PostHogPageView = dynamic(() => import("./_analytics/capture"), { + ssr: false, +}); + export default function RootLayout({ children, }: Readonly<{ @@ -76,9 +81,12 @@ export default function RootLayout({ }>) { return ( -
-