📈 Enable page view analytics (#1434)

This commit is contained in:
Luke Vella 2024-11-10 14:49:05 +00:00 committed by GitHub
parent ee68d80026
commit 4f98661a12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 38 additions and 2 deletions

View file

@ -0,0 +1,24 @@
"use client";
import { usePathname, useSearchParams } from "next/navigation";
import { usePostHog } from "posthog-js/react";
import { useEffect } from "react";
export 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;
}

View file

@ -12,6 +12,8 @@ import { trpcConfig } from "@/trpc/client/config";
import type { AppRouter } from "@/trpc/routers";
import { ConnectedDayjsProvider } from "@/utils/dayjs";
import { PostHogPageView } from "./posthog-page-view";
export const trpc = createTRPCReact<AppRouter>({
unstable_overrides: {
useMutation: {
@ -33,6 +35,7 @@ export function Providers(props: { children: React.ReactNode }) {
<I18nProvider>
<TooltipProvider>
<PostHogProvider>
<PostHogPageView />
<UserProvider>
<ConnectedDayjsProvider>
{props.children}

View file

@ -15,6 +15,10 @@
"posthog-js": "^1.178.0",
"posthog-node": "^4.2.1"
},
"devDependencies": {
"@rallly/eslint-config": "*",
"@rallly/tsconfig": "*"
},
"peerDependencies": {
"next": "^14.2.13",
"react": "^18.2.0"

View file

@ -1,5 +1,9 @@
{
"extends": "@rallly/tsconfig/next.json",
"include": ["**/*.ts", "**/*.tsx"],
"include": [
"**/*.ts",
"**/*.tsx",
"../../apps/web/src/posthog-page-view.tsx"
],
"exclude": ["node_modules"]
}

View file

@ -10,6 +10,7 @@
"noUncheckedIndexedAccess": false,
"noImplicitReturns": false,
"verbatimModuleSyntax": true,
"skipLibCheck": true
"skipLibCheck": true,
"strictNullChecks": true
}
}