Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging #125

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/PostHogPageView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// app/PostHogPageView.tsx
'use client'

import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import { usePostHog } from 'posthog-js/react';

export default function PostHogPageView() : null {
const pathname = usePathname();
const searchParams = useSearchParams();
const posthog = usePostHog();
// Track pageviews
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
}
17 changes: 13 additions & 4 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import './globals.css'
import { SessionProvider } from "next-auth/react";
import { AuthProvider } from './contexts/AuthContext';
import { PHProvider } from './providers';
import dynamic from 'next/dynamic'

const PostHogPageView = dynamic(() => import('./PostHogPageView'), {
ssr: false,
})

export default function RootLayout({
children,
Expand All @@ -11,11 +16,15 @@ export default function RootLayout({
}) {
return (
<html lang="en">
<body className="min-w-screen">
<SessionProvider>
<PHProvider>
<body className="min-w-screen">
<SessionProvider>
<PostHogPageView />
{children}
</SessionProvider>
</body>
</SessionProvider>
</body>
</PHProvider>

</html >

)
Expand Down
19 changes: 19 additions & 0 deletions app/providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
import { useSession, signIn } from "next-auth/react"
import Loader from '@/components/Loader';

import posthog from 'posthog-js'
import { PostHogProvider } from 'posthog-js/react'

if (typeof window !== 'undefined') {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
capture_pageview: false // Disable automatic pageview capture, as we capture manually
})
}

export default function AdminSessionProvider({
children
}: {
Expand All @@ -24,3 +34,12 @@ export default function AdminSessionProvider({
</>
)
}


export function PHProvider({
children,
}: {
children: React.ReactNode
}) {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>
}
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"next": "13.4.9",
"next-auth": "4.23.1",
"postcss": "^8.4.29",
"posthog-js": "^1.130.0",
"qrcode.react": "^3.1.0",
"react": "18.2.0",
"react-datepicker": "^4.17.0",
Expand Down
Loading