Skip to content

Commit 07fd9c3

Browse files
authored
feat(posthog): added posthog provider instead of using nextjs instrumentation (#1555)
* fix(posthog): make instrumentation client not use next-runtime-env * feat(posthog): added posthog provider
1 parent 223ecda commit 07fd9c3

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

apps/sim/app/layout.tsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { Analytics } from '@vercel/analytics/next'
21
import type { Metadata, Viewport } from 'next'
32
import { PublicEnvScript } from 'next-runtime-env'
43
import { BrandedLayout } from '@/components/branded-layout'
54
import { generateThemeCSS } from '@/lib/branding/inject-theme'
65
import { generateBrandedMetadata, generateStructuredData } from '@/lib/branding/metadata'
7-
import { isHosted } from '@/lib/environment'
86
import { createLogger } from '@/lib/logs/console/logger'
7+
import { PostHogProvider } from '@/lib/posthog/provider'
98
import '@/app/globals.css'
109

1110
import { SessionProvider } from '@/lib/session/session-context'
@@ -55,7 +54,6 @@ export const viewport: Viewport = {
5554
],
5655
}
5756

58-
// Generate dynamic metadata based on brand configuration
5957
export const metadata: Metadata = generateBrandedMetadata()
6058

6159
export default function RootLayout({ children }: { children: React.ReactNode }) {
@@ -91,19 +89,16 @@ export default function RootLayout({ children }: { children: React.ReactNode })
9189
<PublicEnvScript />
9290
</head>
9391
<body suppressHydrationWarning>
94-
<ThemeProvider>
95-
<SessionProvider>
96-
<BrandedLayout>
97-
<ZoomPrevention />
98-
{children}
99-
{isHosted && (
100-
<>
101-
<Analytics />
102-
</>
103-
)}
104-
</BrandedLayout>
105-
</SessionProvider>
106-
</ThemeProvider>
92+
<PostHogProvider>
93+
<ThemeProvider>
94+
<SessionProvider>
95+
<BrandedLayout>
96+
<ZoomPrevention />
97+
{children}
98+
</BrandedLayout>
99+
</SessionProvider>
100+
</ThemeProvider>
101+
</PostHogProvider>
107102
</body>
108103
</html>
109104
)

apps/sim/instrumentation-client.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,7 @@
55
* It respects the user's telemetry preferences stored in localStorage.
66
*
77
*/
8-
import posthog from 'posthog-js'
9-
import { env, getEnv, isTruthy } from './lib/env'
10-
11-
// Initialize PostHog only if explicitly enabled
12-
if (isTruthy(getEnv('NEXT_PUBLIC_POSTHOG_ENABLED')) && getEnv('NEXT_PUBLIC_POSTHOG_KEY')) {
13-
posthog.init(getEnv('NEXT_PUBLIC_POSTHOG_KEY')!, {
14-
api_host: '/ingest',
15-
ui_host: 'https://us.posthog.com',
16-
person_profiles: 'identified_only',
17-
capture_pageview: true,
18-
capture_pageleave: true,
19-
capture_performance: true,
20-
session_recording: {
21-
maskAllInputs: false,
22-
maskInputOptions: {
23-
password: true,
24-
email: false,
25-
},
26-
recordCrossOriginIframes: false,
27-
recordHeaders: true,
28-
recordBody: true,
29-
},
30-
autocapture: true,
31-
capture_dead_clicks: true,
32-
persistence: 'localStorage+cookie',
33-
enable_heatmaps: true,
34-
})
35-
}
8+
import { env } from './lib/env'
369

3710
if (typeof window !== 'undefined') {
3811
const TELEMETRY_STATUS_KEY = 'simstudio-telemetry-status'

apps/sim/lib/posthog/provider.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use client'
2+
3+
import { useEffect } from 'react'
4+
import posthog from 'posthog-js'
5+
import { PostHogProvider as PHProvider } from 'posthog-js/react'
6+
import { getEnv, isTruthy } from '../env'
7+
8+
export function PostHogProvider({ children }: { children: React.ReactNode }) {
9+
useEffect(() => {
10+
const posthogEnabled = getEnv('NEXT_PUBLIC_POSTHOG_ENABLED')
11+
const posthogKey = getEnv('NEXT_PUBLIC_POSTHOG_KEY')
12+
13+
if (isTruthy(posthogEnabled) && posthogKey && !posthog.__loaded) {
14+
posthog.init(posthogKey, {
15+
api_host: '/ingest',
16+
ui_host: 'https://us.posthog.com',
17+
defaults: '2025-05-24',
18+
person_profiles: 'identified_only',
19+
capture_pageview: true,
20+
capture_pageleave: true,
21+
capture_performance: true,
22+
session_recording: {
23+
maskAllInputs: false,
24+
maskInputOptions: {
25+
password: true,
26+
email: false,
27+
},
28+
recordCrossOriginIframes: false,
29+
recordHeaders: true,
30+
recordBody: true,
31+
},
32+
autocapture: true,
33+
capture_dead_clicks: true,
34+
persistence: 'localStorage+cookie',
35+
enable_heatmaps: true,
36+
})
37+
}
38+
}, [])
39+
40+
return <PHProvider client={posthog}>{children}</PHProvider>
41+
}

0 commit comments

Comments
 (0)