Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
feat: fix posthog (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdilouraoui authored Sep 19, 2023
1 parent 6567d41 commit cf79b16
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
33 changes: 33 additions & 0 deletions hooks/usePostHog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useRouter } from 'next/router';
import posthog from 'posthog-js';
import { useEffect } from 'react';

if (typeof window !== 'undefined') {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY ?? '', {
api_host:
process.env.NEXT_PUBLIC_POSTHOG_HOST ||
'https://docs.posthog.archifiltre.fr',
capture_pageview: false,
});
}

export const usePostHog = () => {
const router = useRouter();

useEffect(() => {
const handleRouteChange = () => {
posthog?.capture('$pageview');
console.log('capture');
};
router.events.on('routeChangeComplete', handleRouteChange);

return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, []);
};

export const PostHogTracker = () => {
usePostHog();
return <></>;
};
28 changes: 2 additions & 26 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,21 @@ import { PrismicPreview } from '@prismicio/next';
import { PrismicProvider } from '@prismicio/react';
import type { AppProps } from 'next/app';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { DefaultSeo } from 'next-seo';
import posthog from 'posthog-js';
import { PostHogProvider } from 'posthog-js/react';
import { useEffect } from 'react';
import { Footer } from '../components/common/Footer';
import { Header } from '../components/common/Header';
import { Layout } from '../components/common/Layout';
import { Main } from '../components/common/Main';
import { PostHogTracker } from '../hooks/usePostHog';
import { repositoryName } from '../prismicConfiguration';
import '../styles/index.scss';
import { linkResolver } from '../utils/prismic/helpers';

if (typeof window !== 'undefined') {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY ?? '', {
api_host:
process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://app.posthog.com',
// Enable debug mode in development
loaded: posthog => {
if (process.env.NODE_ENV === 'development') posthog.debug();
},
capture_pageview: false, // Disable automatic pageview capture, as we capture manually
});
}

const App = ({ Component, pageProps }: AppProps) => {
const router = useRouter();

useEffect(() => {
// Track page views
const handleRouteChange = () => posthog?.capture('$pageview');
router.events.on('routeChangeComplete', handleRouteChange);

return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, []);
return (
<PostHogProvider>
<PostHogTracker />
<PrismicProvider
linkResolver={linkResolver}
internalLinkComponent={({ href, children, ...props }) => (
Expand Down

0 comments on commit cf79b16

Please sign in to comment.