2024-09-17 04:37:34 +00:00
|
|
|
'use client'
|
2024-11-26 05:04:52 +00:00
|
|
|
import { NEXT_PUBLIC_POSTHOG_PAPIK, NEXT_PUBLIC_POSTHOG_UI_HOST, NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED } from '@/lib/environment.client'
|
2024-09-17 04:37:34 +00:00
|
|
|
import posthog from 'posthog-js'
|
2025-02-25 01:06:29 +00:00
|
|
|
import { usePostHog } from 'posthog-js/react'
|
|
|
|
|
import { PostHogProvider as PHProvider } from 'posthog-js/react'
|
2024-11-18 20:09:26 +00:00
|
|
|
import { resolveServerPath } from './api/(client)/client'
|
2024-11-26 05:04:52 +00:00
|
|
|
import { isDefined } from '@/lib/utils'
|
2025-02-25 01:06:29 +00:00
|
|
|
import { usePathname, useSearchParams } from "next/navigation"
|
2025-02-25 01:51:06 +00:00
|
|
|
import { useEffect, Suspense } from "react"
|
2024-09-17 04:37:34 +00:00
|
|
|
|
2025-02-25 01:06:29 +00:00
|
|
|
const POSTHOG_ENABLED = isDefined(NEXT_PUBLIC_POSTHOG_PAPIK) && !NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED;
|
2024-11-18 20:09:26 +00:00
|
|
|
|
2025-02-25 01:06:29 +00:00
|
|
|
function PostHogPageView() {
|
|
|
|
|
const pathname = usePathname()
|
|
|
|
|
const searchParams = useSearchParams()
|
|
|
|
|
const posthog = usePostHog()
|
|
|
|
|
|
|
|
|
|
// Track pageviews
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (pathname && posthog) {
|
|
|
|
|
let url = window.origin + pathname
|
|
|
|
|
if (searchParams.toString()) {
|
|
|
|
|
url = url + `?${searchParams.toString()}`
|
2024-09-19 20:22:13 +00:00
|
|
|
}
|
2025-02-25 01:06:29 +00:00
|
|
|
|
|
|
|
|
posthog.capture('$pageview', { '$current_url': url })
|
|
|
|
|
}
|
|
|
|
|
}, [pathname, searchParams, posthog])
|
|
|
|
|
|
|
|
|
|
return null
|
2024-09-17 04:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-25 01:51:06 +00:00
|
|
|
export default function SuspendedPostHogPageView() {
|
|
|
|
|
return <Suspense fallback={null}>
|
|
|
|
|
<PostHogPageView />
|
|
|
|
|
</Suspense>
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 01:06:29 +00:00
|
|
|
export function PostHogProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (POSTHOG_ENABLED) {
|
|
|
|
|
// @see next.config.mjs for path rewrites to the "/ingest" route.
|
|
|
|
|
const posthogHostPath = resolveServerPath('/ingest');
|
|
|
|
|
|
|
|
|
|
posthog.init(NEXT_PUBLIC_POSTHOG_PAPIK!, {
|
|
|
|
|
api_host: posthogHostPath,
|
|
|
|
|
ui_host: NEXT_PUBLIC_POSTHOG_UI_HOST,
|
|
|
|
|
capture_pageview: false, // Disable automatic pageview capture
|
|
|
|
|
autocapture: false, // Disable automatic event capture
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
/* @nocheckin HANDLE SELF HOSTED CASE
|
|
|
|
|
person_profiles: 'identified_only',
|
|
|
|
|
sanitize_properties: (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;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.log("PostHog telemetry disabled");
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<PHProvider client={posthog}>
|
2025-02-25 01:51:06 +00:00
|
|
|
<SuspendedPostHogPageView />
|
2025-02-25 01:06:29 +00:00
|
|
|
{children}
|
|
|
|
|
</PHProvider>
|
|
|
|
|
)
|
2024-09-17 04:37:34 +00:00
|
|
|
}
|