sourcebot/packages/web/src/app/posthogProvider.tsx

43 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-09-17 04:37:34 +00:00
'use client'
import { NEXT_PUBLIC_POSTHOG_PAPIK, NEXT_PUBLIC_POSTHOG_UI_HOST, NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED, NEXT_PUBLIC_PUBLIC_SEARCH_DEMO } from '@/lib/environment.client'
2024-09-17 04:37:34 +00:00
import posthog from 'posthog-js'
import { PostHogProvider } from 'posthog-js/react'
import { resolveServerPath } from './api/(client)/client'
2024-11-26 05:04:52 +00:00
import { isDefined } from '@/lib/utils'
2024-09-17 04:37:34 +00:00
if (typeof window !== 'undefined') {
2024-11-26 05:04:52 +00:00
if (!NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED && isDefined(NEXT_PUBLIC_POSTHOG_PAPIK)) {
// @see next.config.mjs for path rewrites to the "/ingest" route.
const posthogHostPath = resolveServerPath('/ingest');
2024-11-26 05:04:52 +00:00
posthog.init(NEXT_PUBLIC_POSTHOG_PAPIK, {
api_host: posthogHostPath,
2024-09-17 04:37:34 +00:00
ui_host: NEXT_PUBLIC_POSTHOG_UI_HOST,
person_profiles: 'identified_only',
capture_pageview: NEXT_PUBLIC_PUBLIC_SEARCH_DEMO, // @nocheckin Disable automatic pageview capture if we're not in public demo mode
autocapture: false, // Disable automatic event capture
2024-09-19 20:32:40 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sanitize_properties: !NEXT_PUBLIC_PUBLIC_SEARCH_DEMO ? (properties: Record<string, any>, _event: string) => {
// https://posthog.com/docs/libraries/js#config
if (properties['$current_url']) {
properties['$current_url'] = null;
}
if (properties['$ip']) {
properties['$ip'] = null;
}
return properties;
} : undefined
2024-09-17 04:37:34 +00:00
});
} else {
console.log("PostHog telemetry disabled");
}
}
export function PHProvider({
children,
}: {
children: React.ReactNode
}) {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>
}