gitea +
This commit is contained in:
34
frontend/components/analytics/PostHogPageView.tsx
Normal file
34
frontend/components/analytics/PostHogPageView.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
'use client'
|
||||
|
||||
import { usePathname, useSearchParams } from "next/navigation"
|
||||
import { useEffect, Suspense } from "react"
|
||||
import { usePostHog } from 'posthog-js/react'
|
||||
|
||||
function PostHogPageViewContent() {
|
||||
const pathname = usePathname()
|
||||
const searchParams = useSearchParams()
|
||||
const posthog = usePostHog()
|
||||
|
||||
useEffect(() => {
|
||||
// Track pageview
|
||||
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 PostHogPageView() {
|
||||
return (
|
||||
<Suspense fallback={null}>
|
||||
<PostHogPageViewContent />
|
||||
</Suspense>
|
||||
)
|
||||
}
|
||||
25
frontend/components/analytics/PostHogProvider.tsx
Normal file
25
frontend/components/analytics/PostHogProvider.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
'use client'
|
||||
import posthog from 'posthog-js'
|
||||
import { PostHogProvider as PHProvider } from 'posthog-js/react'
|
||||
import { useEffect } from 'react'
|
||||
import PostHogPageView from './PostHogPageView'
|
||||
|
||||
export function PostHogProvider({ children }: { children: React.ReactNode }) {
|
||||
useEffect(() => {
|
||||
if (typeof window !== 'undefined' && !posthog.__loaded) {
|
||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY || 'phc_placeholder_key', {
|
||||
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
|
||||
capture_pageview: false, // Disable automatic pageview capture, as we handle it manually
|
||||
capture_pageleave: true,
|
||||
persistence: 'localStorage+cookie',
|
||||
opt_out_capturing_by_default: true,
|
||||
debug: true,
|
||||
})
|
||||
}
|
||||
}, [])
|
||||
|
||||
return <PHProvider client={posthog}>
|
||||
<PostHogPageView />
|
||||
{children}
|
||||
</PHProvider>
|
||||
}
|
||||
Reference in New Issue
Block a user