Skip to content

Commit ada63c0

Browse files
committed
Add analytics
1 parent abded8b commit ada63c0

File tree

6 files changed

+90
-2
lines changed

6 files changed

+90
-2
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
.docusaurus/*
21
build/*
32
node_modules/*
43
.vercel
54
.next
5+
.env*
66

77
# Generated
88
public/robots.txt

components/nextra/Search.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { useRouter } from 'next/router';
2727
import { Link } from 'nextra-theme-docs';
2828
import { useMounted } from 'nextra/hooks';
2929
import { InformationCircleIcon, SpinnerIcon } from 'nextra/icons';
30+
import { usePostHog } from 'posthog-js/react';
3031
import type { CompositionEvent, KeyboardEvent, ReactElement } from 'react';
3132
import { Fragment, useCallback, useEffect, useRef, useState } from 'react';
3233
import { Input } from './Input';
@@ -64,6 +65,7 @@ export function Search({
6465
const [focused, setFocused] = useState(false);
6566
// Trigger the search after the Input is complete for languages like Chinese
6667
const [composition, setComposition] = useState(true);
68+
const posthog = usePostHog();
6769

6870
useEffect(() => {
6971
setActive(0);
@@ -101,6 +103,7 @@ export function Search({
101103
}, []);
102104

103105
const finishSearch = useCallback(() => {
106+
posthog?.capture('search', { query: input.current.value });
104107
input.current?.blur();
105108
onChange('');
106109
setShow(false);

components/scripts.tsx

+30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import inEU from '@segment/in-eu';
44
import { usePathname, useSearchParams } from 'next/navigation';
5+
import { Router } from 'next/router';
56
import Script from 'next/script';
7+
import posthog from 'posthog-js';
68
import { useEffect, useState } from 'react';
79

810
const isProd = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';
@@ -80,11 +82,39 @@ function HubSpot() {
8082
);
8183
}
8284

85+
function Posthog() {
86+
useEffect(() => {
87+
if (inEU() || !process.env.NEXT_PUBLIC_POSTHOG_KEY) {
88+
return;
89+
}
90+
91+
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY ?? '', {
92+
api_host: isProd ? '/ingest' : process.env.NEXT_PUBLIC_POSTHOG_HOST, // See Posthog rewrites in next config
93+
ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
94+
person_profiles: 'always',
95+
loaded: (posthog) => {
96+
if (process.env.NODE_ENV === 'development') posthog.debug();
97+
},
98+
});
99+
100+
const handleRouteChange = () => posthog?.capture('$pageview');
101+
102+
Router.events.on('routeChangeComplete', handleRouteChange);
103+
104+
return () => {
105+
Router.events.off('routeChangeComplete', handleRouteChange);
106+
};
107+
}, []);
108+
109+
return <></>;
110+
}
111+
83112
export default function Scripts() {
84113
return (
85114
<div>
86115
<Reo />
87116
<HubSpot />
117+
<Posthog />
88118
</div>
89119
);
90120
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"next-sitemap": "^4.2.3",
3333
"nextra": "^2.13.4",
3434
"nextra-theme-docs": "^2.13.4",
35+
"posthog-js": "^1.223.5",
3536
"react": "^18.3.1",
3637
"react-dom": "^18.3.1",
3738
"react-youtube": "^10.1.0",

pages/_app.mdx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import posthog from 'posthog-js'
2+
import { PostHogProvider } from 'posthog-js/react'
13
import { SpeedInsights } from "@vercel/speed-insights/next"
24

35
import Layout from '@/components/layout';
@@ -7,7 +9,9 @@ export default function MyApp({ Component, pageProps }) {
79
return (
810
<Layout>
911
<SpeedInsights/>
10-
<Component {...pageProps} />
12+
<PostHogProvider client={posthog}>
13+
<Component {...pageProps} />
14+
</PostHogProvider>
1115
</Layout>
1216
);
1317
}

pnpm-lock.yaml

+50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)