posthog.capture() queues events automatically before init completes, so checking __loaded blocked the first pageview from being sent. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
25 lines
681 B
TypeScript
25 lines
681 B
TypeScript
'use client'
|
|
|
|
import { useEffect } from 'react'
|
|
import { usePathname } from 'next/navigation'
|
|
import posthog from 'posthog-js'
|
|
|
|
export default function PostHogProvider({ children }: { children: React.ReactNode }) {
|
|
useEffect(() => {
|
|
posthog.init('phc_BHgg9S7CQqVShe7EMCdi86PxA49qcNaTsR9Nn5EGxRCT', {
|
|
api_host: 'https://analytics.hyggecraftery.com',
|
|
capture_pageview: false,
|
|
persistence: 'localStorage',
|
|
})
|
|
}, [])
|
|
|
|
const pathname = usePathname()
|
|
|
|
useEffect(() => {
|
|
// PostHog queues events automatically before init completes
|
|
posthog.capture('$pageview', { $current_url: window.location.href })
|
|
}, [pathname])
|
|
|
|
return <>{children}</>
|
|
}
|