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

Updated posthog for next #176

Merged
merged 3 commits into from
Nov 7, 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
28 changes: 28 additions & 0 deletions apps/frontend/src/app/PostHogPageView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'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();
useEffect(() => {
// Track pageviews
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
}
20 changes: 20 additions & 0 deletions apps/frontend/src/app/providers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'
import posthog from 'posthog-js'
import { PostHogProvider } from 'posthog-js/react'
import { useEffect } from 'react'

export function PHProvider({
children,
}: {
children: React.ReactNode
}) {
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY || "", {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
person_profiles: 'identified_only',
capture_pageview: false // Disable automatic pageview capture, as we capture manually
})
}, []);

return <PostHogProvider client={posthog}>{children}</PostHogProvider>
}
15 changes: 5 additions & 10 deletions apps/frontend/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ import { PersistGate } from "redux-persist/integration/react";
import Head from "next/head";

import { ClerkProvider } from "@clerk/nextjs";
import { PostHogProvider } from 'posthog-js/react'

const options = {
api_host: process.env.REACT_APP_PUBLIC_POSTHOG_HOST,
}
import { PHProvider } from "~/app/providers";
import PostHogPageView from "~/app/PostHogPageView";

export default function MyApp({ Component, pageProps }: AppProps) {
return (
<PostHogProvider
apiKey={process.env.NEXT_PUBLIC_POSTHOG_KEY}
options={options}
>
<PHProvider>
<ClerkProvider>
<Head>
<title>CMU Courses</title>
Expand All @@ -35,10 +29,11 @@ export default function MyApp({ Component, pageProps }: AppProps) {
</Head>
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<PostHogPageView />
<Component {...pageProps} />
</PersistGate>
</Provider>
</ClerkProvider>
</PostHogProvider>
</PHProvider>
);
}
Loading