MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / PostHogProvider

Function PostHogProvider

freebuff/web/src/lib/PostHogProvider.tsx:9–50  ·  view source on GitHub ↗
({ children }: { children: ReactNode })

Source from the content-addressed store, hash-verified

7import { useEffect, useRef, type ReactNode } from 'react'
8
9export function PostHogProvider({ children }: { children: ReactNode }) {
10 const { data: session } = useSession()
11 const prevSessionRef = useRef(session)
12
13 useEffect(() => {
14 if (!env.NEXT_PUBLIC_POSTHOG_API_KEY || typeof window === 'undefined') {
15 return
16 }
17
18 posthog.init(env.NEXT_PUBLIC_POSTHOG_API_KEY, {
19 api_host: '/ingest',
20 ui_host: env.NEXT_PUBLIC_POSTHOG_HOST_URL,
21 person_profiles: 'always',
22 })
23 }, [])
24
25 useEffect(() => {
26 if (!env.NEXT_PUBLIC_POSTHOG_API_KEY) {
27 return
28 }
29
30 const hadSession = !!prevSessionRef.current?.user?.email
31 const hasSession = !!session?.user?.email
32 prevSessionRef.current = session
33
34 if (hasSession && session.user) {
35 posthog.identify(session.user.email!, {
36 email: session.user.email,
37 user_id: session.user.id,
38 name: session.user.name,
39 })
40 } else if (hadSession && !hasSession) {
41 posthog.reset()
42 }
43 }, [session])
44
45 return (
46 <PostHogProviderWrapper client={posthog}>
47 {children}
48 </PostHogProviderWrapper>
49 )
50}

Callers

nothing calls this directly

Calls 1

resetMethod · 0.45

Tested by

no test coverage detected