Skip to content

Commit a11350a

Browse files
committed
Landing page posthog analytics
1 parent 8ab294f commit a11350a

File tree

6 files changed

+140
-11
lines changed

6 files changed

+140
-11
lines changed

packages/landing/app/layout.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { Metadata } from "next";
22
import { Inter } from "next/font/google";
33
import { ThemeProvider } from "next-themes";
4+
import { PHProvider } from "@/components/posthog-provider";
5+
import PostHogPageView from "@/components/posthog-pageview";
6+
import { Suspense } from "react";
47
import "./globals.css";
58

69
const inter = Inter({
@@ -169,15 +172,20 @@ export default function RootLayout({
169172
<link rel="dns-prefetch" href="//fonts.gstatic.com" />
170173
</head>
171174
<body className="antialiased">
172-
<ThemeProvider
173-
attribute="class"
174-
defaultTheme="light"
175-
enableSystem={false}
176-
forcedTheme="light"
177-
disableTransitionOnChange={false}
178-
>
179-
{children}
180-
</ThemeProvider>
175+
<PHProvider>
176+
<Suspense>
177+
<PostHogPageView />
178+
</Suspense>
179+
<ThemeProvider
180+
attribute="class"
181+
defaultTheme="light"
182+
enableSystem={false}
183+
forcedTheme="light"
184+
disableTransitionOnChange={false}
185+
>
186+
{children}
187+
</ThemeProvider>
188+
</PHProvider>
181189
</body>
182190
</html>
183191
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use client'
2+
3+
import { usePathname, useSearchParams } from 'next/navigation'
4+
import { usePostHog } from 'posthog-js/react'
5+
import { useEffect } from 'react'
6+
7+
export default function PostHogPageView(): null {
8+
const pathname = usePathname()
9+
const searchParams = useSearchParams()
10+
const posthog = usePostHog()
11+
12+
useEffect(() => {
13+
if (pathname && posthog) {
14+
let url = window.origin + pathname
15+
if (searchParams && searchParams.toString()) {
16+
url = url + `?${searchParams.toString()}`
17+
}
18+
posthog.capture('$pageview', {
19+
$current_url: url,
20+
})
21+
}
22+
}, [pathname, searchParams, posthog])
23+
24+
return null
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use client'
2+
3+
import posthog from 'posthog-js'
4+
import { PostHogProvider } from 'posthog-js/react'
5+
import { useEffect } from 'react'
6+
7+
export function PHProvider({
8+
children,
9+
}: {
10+
children: React.ReactNode
11+
}) {
12+
useEffect(() => {
13+
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
14+
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
15+
person_profiles: 'identified_only',
16+
capture_pageview: false,
17+
capture_pageleave: true,
18+
})
19+
}, [])
20+
21+
return <PostHogProvider client={posthog}>{children}</PostHogProvider>
22+
}

packages/landing/hooks/use-posthog.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use client'
2+
3+
import { usePostHog as usePostHogReact } from 'posthog-js/react'
4+
5+
export function usePostHog() {
6+
const posthog = usePostHogReact()
7+
8+
const trackEvent = (eventName: string, properties?: Record<string, any>) => {
9+
if (posthog) {
10+
posthog.capture(eventName, properties)
11+
}
12+
}
13+
14+
const identify = (userId: string, properties?: Record<string, any>) => {
15+
if (posthog) {
16+
posthog.identify(userId, properties)
17+
}
18+
}
19+
20+
const reset = () => {
21+
if (posthog) {
22+
posthog.reset()
23+
}
24+
}
25+
26+
return {
27+
trackEvent,
28+
identify,
29+
reset,
30+
posthog,
31+
}
32+
}

packages/landing/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"lucide-react": "^0.536.0",
4747
"next": "15.4.6",
4848
"next-themes": "^0.4.6",
49+
"posthog-js": "^1.260.1",
4950
"react": "19.1.1",
5051
"react-day-picker": "^9.8.1",
5152
"react-dom": "19.1.1",

pnpm-lock.yaml

Lines changed: 43 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)