Skip to content

Commit 316ede6

Browse files
author
waleed
committed
added envvars to env.ts
1 parent 17375bd commit 316ede6

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

apps/sim/instrumentation-client.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
*
77
*/
88
import posthog from 'posthog-js'
9-
import { env } from './lib/env'
9+
import { env, getEnv, isTruthy } from './lib/env'
1010

1111
// Initialize PostHog only if explicitly enabled
12-
if (
13-
process.env.NEXT_PUBLIC_POSTHOG_ENABLED === 'true' &&
14-
process.env.NEXT_PUBLIC_POSTHOG_KEY &&
15-
process.env.NEXT_PUBLIC_POSTHOG_HOST
16-
) {
17-
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
12+
if (isTruthy(getEnv('NEXT_PUBLIC_POSTHOG_ENABLED')) && getEnv('NEXT_PUBLIC_POSTHOG_KEY')) {
13+
posthog.init(getEnv('NEXT_PUBLIC_POSTHOG_KEY')!, {
1814
api_host: '/ingest',
1915
ui_host: 'https://us.posthog.com',
2016
person_profiles: 'identified_only',

apps/sim/lib/env.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export const env = createEnv({
9090
TELEMETRY_ENDPOINT: z.string().url().optional(), // Custom telemetry/analytics endpoint
9191
COST_MULTIPLIER: z.number().optional(), // Multiplier for cost calculations
9292
LOG_LEVEL: z.enum(['DEBUG', 'INFO', 'WARN', 'ERROR']).optional(), // Minimum log level to display (defaults to ERROR in production, DEBUG in development)
93+
POSTHOG_ENABLED: z.boolean().optional(), // Enable PostHog analytics and session recording
9394

9495
// External Services
9596
BROWSERBASE_API_KEY: z.string().min(1).optional(), // Browserbase API key for browser automation
@@ -258,6 +259,8 @@ export const env = createEnv({
258259
// Analytics & Tracking
259260
NEXT_PUBLIC_GOOGLE_API_KEY: z.string().optional(), // Google API key for client-side API calls
260261
NEXT_PUBLIC_GOOGLE_PROJECT_NUMBER: z.string().optional(), // Google project number for Drive picker
262+
NEXT_PUBLIC_POSTHOG_ENABLED: z.boolean().optional(), // Enable PostHog analytics (client-side)
263+
NEXT_PUBLIC_POSTHOG_KEY: z.string().optional(), // PostHog project API key
261264

262265
// UI Branding & Whitelabeling
263266
NEXT_PUBLIC_BRAND_NAME: z.string().optional(), // Custom brand name (defaults to "Sim")
@@ -317,6 +320,8 @@ export const env = createEnv({
317320
NEXT_PUBLIC_EMAIL_PASSWORD_SIGNUP_ENABLED: process.env.NEXT_PUBLIC_EMAIL_PASSWORD_SIGNUP_ENABLED,
318321
NEXT_PUBLIC_E2B_ENABLED: process.env.NEXT_PUBLIC_E2B_ENABLED,
319322
NEXT_PUBLIC_COPILOT_TRAINING_ENABLED: process.env.NEXT_PUBLIC_COPILOT_TRAINING_ENABLED,
323+
NEXT_PUBLIC_POSTHOG_ENABLED: process.env.NEXT_PUBLIC_POSTHOG_ENABLED,
324+
NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,
320325
NODE_ENV: process.env.NODE_ENV,
321326
NEXT_TELEMETRY_DISABLED: process.env.NEXT_TELEMETRY_DISABLED,
322327
},

apps/sim/lib/session/session-context.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,22 @@ export function SessionProvider({ children }: { children: React.ReactNode }) {
5454
}, [loadSession])
5555

5656
useEffect(() => {
57-
if (isPending || !posthog.__loaded) {
57+
if (isPending || typeof posthog.identify !== 'function') {
5858
return
5959
}
6060

61-
if (data?.user) {
62-
posthog.identify(data.user.id, {
63-
email: data.user.email,
64-
name: data.user.name,
65-
email_verified: data.user.emailVerified,
66-
created_at: data.user.createdAt,
67-
})
68-
} else {
69-
posthog.reset()
70-
}
61+
try {
62+
if (data?.user) {
63+
posthog.identify(data.user.id, {
64+
email: data.user.email,
65+
name: data.user.name,
66+
email_verified: data.user.emailVerified,
67+
created_at: data.user.createdAt,
68+
})
69+
} else {
70+
posthog.reset()
71+
}
72+
} catch {}
7173
}, [data, isPending])
7274

7375
const value = useMemo<SessionHookResult>(

apps/sim/next.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ const nextConfig: NextConfig = {
239239
return redirects
240240
},
241241
async rewrites() {
242-
// Only enable PostHog reverse proxy if PostHog is enabled (defaults to disabled)
243242
if (!isTruthy(env.POSTHOG_ENABLED)) {
244243
return []
245244
}

0 commit comments

Comments
 (0)