-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
38 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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(); | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
// app/providers.tsx | ||
"use client"; | ||
import posthog from "posthog-js"; | ||
import { PostHogProvider } from "posthog-js/react"; | ||
|
||
if (typeof window !== "undefined") { | ||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, { | ||
console.log(process.env.NEXT_PUBLIC_POSTHOG_KEY); | ||
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 | ||
}); | ||
} | ||
|
||
export const CSPostHogProvider = ({ children }) => { | ||
export function PHProvider({ children }: { children: React.ReactNode }) { | ||
return <PostHogProvider client={posthog}>{children}</PostHogProvider>; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters