mirror of
https://github.com/learnhouse/docs.git
synced 2026-12-01 02:02:29 +01:00
feat: new documentation
This commit is contained in:
47
components/Analytics/PostHogProvider.jsx
Normal file
47
components/Analytics/PostHogProvider.jsx
Normal file
@@ -0,0 +1,47 @@
|
||||
'use client'
|
||||
|
||||
import posthog from 'posthog-js'
|
||||
import { PostHogProvider as PHProvider, usePostHog } from 'posthog-js/react'
|
||||
import { usePathname, useSearchParams } from 'next/navigation'
|
||||
import { useEffect, Suspense } from 'react'
|
||||
|
||||
if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_POSTHOG_KEY) {
|
||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
|
||||
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
|
||||
capture_pageview: false,
|
||||
capture_pageleave: true,
|
||||
})
|
||||
}
|
||||
|
||||
function PostHogPageView() {
|
||||
const pathname = usePathname()
|
||||
const searchParams = useSearchParams()
|
||||
const posthog = usePostHog()
|
||||
|
||||
useEffect(() => {
|
||||
if (pathname && posthog) {
|
||||
let url = window.origin + pathname
|
||||
if (searchParams.toString()) {
|
||||
url = url + '?' + searchParams.toString()
|
||||
}
|
||||
posthog.capture('$pageview', { $current_url: url })
|
||||
}
|
||||
}, [pathname, searchParams, posthog])
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export default function PostHogProvider({ children }) {
|
||||
if (!process.env.NEXT_PUBLIC_POSTHOG_KEY) {
|
||||
return children
|
||||
}
|
||||
|
||||
return (
|
||||
<PHProvider client={posthog}>
|
||||
<Suspense fallback={null}>
|
||||
<PostHogPageView />
|
||||
</Suspense>
|
||||
{children}
|
||||
</PHProvider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user