Rework PostHog setup — use Next.js ingest proxy + posthog-js/react

Mirrors the proven pattern from gendersloty: rewrites /ingest/* to
the PostHog host so requests go through the same origin (no CORS,
no ad-blocker issues). Uses PostHogProvider and usePostHog from
posthog-js/react official React integration.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tim Leikauf
2026-07-01 21:50:43 +02:00
parent eb228ba50b
commit 4274d680e1
5 changed files with 53 additions and 13 deletions

View File

@@ -1,24 +1,18 @@
'use client'
import { useEffect } from 'react'
import { usePathname } from 'next/navigation'
import posthog from 'posthog-js'
import { PostHogProvider as PHProvider } from 'posthog-js/react'
import { useEffect } from 'react'
export default function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
posthog.init('phc_BHgg9S7CQqVShe7EMCdi86PxA49qcNaTsR9Nn5EGxRCT', {
api_host: 'https://eu.posthog.com',
api_host: '/ingest',
ui_host: 'https://analytics.hyggecraftery.com',
capture_pageview: false,
persistence: 'memory',
})
}, [])
const pathname = usePathname()
useEffect(() => {
// PostHog queues events automatically before init completes
posthog.capture('$pageview', { $current_url: window.location.href })
}, [pathname])
return <>{children}</>
return <PHProvider client={posthog}>{children}</PHProvider>
}