-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
02a12b1
commit 78d474a
Showing
4 changed files
with
72 additions
and
0 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,43 @@ | ||
/* eslint-disable turbo/no-undeclared-env-vars */ | ||
/* eslint-disable no-undef */ | ||
import { useRouter } from 'next/router'; | ||
import posthog from 'posthog-js'; | ||
import { PostHogProvider } from 'posthog-js/react'; | ||
import { useEffect } from 'react'; | ||
|
||
// Check that PostHog is client-side (used to handle Next.js SSR) | ||
if (typeof window !== 'undefined') { | ||
/** | ||
* The env vars are not available in the browser, so we need to hardcode the key here. They are NEXT_PUBLIC_ prefixed anyway so it's not a big deal. | ||
* | ||
* Alternatively could use https://github.com/t3-oss/t3-env as it supports client-side env vars. | ||
*/ | ||
// posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY as string, { | ||
posthog.init('phc_dzrTzS4S8mQgrBlxZ6a8YDaz4X9xp04svIoXPSJNznG', { | ||
api_host: 'https://eu.posthog.com', | ||
persistence: 'memory', | ||
loaded: (posthog) => { | ||
if (process.env.NODE_ENV === 'development') posthog.debug(); | ||
}, | ||
}); | ||
} | ||
|
||
function useCapturePageView() { | ||
const router = useRouter(); | ||
useEffect(() => { | ||
const handleRouteChange = () => posthog?.capture('$pageview'); | ||
router.events.on('routeChangeComplete', handleRouteChange); | ||
return () => { | ||
router.events.off('routeChangeComplete', handleRouteChange); | ||
}; | ||
}, []); | ||
} | ||
|
||
export function CustomPosthogProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
useCapturePageView(); | ||
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
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,10 @@ | ||
import { AppPropsType } from 'next/dist/shared/lib/utils'; | ||
import { CustomPosthogProvider } from '../components/custom-posthog-provider'; | ||
|
||
export default function App({ Component, pageProps }: AppPropsType) { | ||
return ( | ||
<CustomPosthogProvider> | ||
<Component {...pageProps} /> | ||
</CustomPosthogProvider> | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.