feat: integrate PostHog authentication handler

- Add PostHogAuthHandler component to PostHogProvider
- Integrate usePostHogAuth hook for automatic user identification
- Add person_profiles: 'identified_only' configuration
- Enable automatic user identification on app load and auth changes
This commit is contained in:
Thomas Camlong
2025-10-02 16:20:32 +02:00
parent 08ff932257
commit fe9f5edc9a

View File

@@ -4,6 +4,7 @@ import { usePathname, useSearchParams } from "next/navigation"
import posthog from "posthog-js" import posthog from "posthog-js"
import { PostHogProvider as PHProvider, usePostHog } from "posthog-js/react" import { PostHogProvider as PHProvider, usePostHog } from "posthog-js/react"
import { Suspense, useEffect } from "react" import { Suspense, useEffect } from "react"
import { usePostHogAuth } from "@/hooks/use-posthog-auth"
export function PostHogProvider({ children }: { children: React.ReactNode }) { export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => { useEffect(() => {
@@ -14,6 +15,7 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || "https://eu.i.posthog.com", api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || "https://eu.i.posthog.com",
capture_pageview: false, // We capture pageviews manually capture_pageview: false, // We capture pageviews manually
capture_pageleave: true, // Enable pageleave capture capture_pageleave: true, // Enable pageleave capture
person_profiles: 'identified_only',
loaded(posthogInstance) { loaded(posthogInstance) {
// @ts-expect-error // @ts-expect-error
window.posthog = posthogInstance window.posthog = posthogInstance
@@ -23,12 +25,18 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
return ( return (
<PHProvider client={posthog}> <PHProvider client={posthog}>
<PostHogAuthHandler />
<SuspendedPostHogPageView /> <SuspendedPostHogPageView />
{children} {children}
</PHProvider> </PHProvider>
) )
} }
function PostHogAuthHandler() {
usePostHogAuth()
return null
}
function PostHogPageView() { function PostHogPageView() {
const pathname = usePathname() const pathname = usePathname()
const searchParams = useSearchParams() const searchParams = useSearchParams()