Skip to content

Commit 6b3b973

Browse files
authored
Merge pull request #318 from authzed/scripts-update
Scripts update
2 parents cdbc7ec + ada63c0 commit 6b3b973

File tree

6 files changed

+149
-7
lines changed

6 files changed

+149
-7
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
'use client';
22

33
import inEU from '@segment/in-eu';
4+
import { usePathname, useSearchParams } from 'next/navigation';
5+
import { Router } from 'next/router';
46
import Script from 'next/script';
7+
import posthog from 'posthog-js';
58
import { useEffect, useState } from 'react';
69

7-
export default function Scripts() {
10+
const isProd = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';
11+
12+
function Reo() {
13+
const reoClientId = process.env.NEXT_PUBLIC_REO_CLIENT_ID;
814
const [loadReo, setLoadReo] = useState(false);
915
const [afterLoad, setAfterLoad] = useState(false);
10-
const isProd = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';
1116

1217
useEffect(() => {
1318
if (window) {
@@ -19,18 +24,97 @@ export default function Scripts() {
1924
// @ts-ignore
2025
if (afterLoad && window.Reo) {
2126
// @ts-ignore
22-
window.Reo.init({ clientID: 'bf9727b30a874e3' });
27+
window.Reo.init({ clientID: reoClientId });
2328
}
2429
}, [afterLoad]);
2530

2631
return (
2732
<div>
28-
{isProd && loadReo && (
33+
{reoClientId && isProd && loadReo && (
34+
<Script
35+
src={`https://static.reo.dev/${reoClientId}/reo.js`}
36+
onLoad={() => setAfterLoad(true)}
37+
defer
38+
></Script>
39+
)}
40+
</div>
41+
);
42+
}
43+
44+
function HubSpot() {
45+
const hsId = process.env.NEXT_PUBLIC_HUBSPOT_ID;
46+
const [loadHs, setLoadHs] = useState(false);
47+
const [afterLoad, setAfterLoad] = useState(false);
48+
const pathname = usePathname();
49+
const searchParams = useSearchParams();
50+
51+
useEffect(() => {
52+
if (window) {
53+
setLoadHs(!inEU());
54+
}
55+
}, [loadHs]);
56+
57+
useEffect(() => {
58+
// @ts-ignore
59+
const hs = window?._hsq;
60+
if (afterLoad && pathname && hs) {
61+
let path = pathname;
62+
if (searchParams && searchParams.toString()) {
63+
path = path + `?${searchParams.toString()}`;
64+
}
65+
hs.push(['setPath', path]);
66+
hs.push(['trackPageView']);
67+
}
68+
}, [afterLoad, pathname, searchParams]);
69+
70+
return (
71+
<div>
72+
{hsId && isProd && loadHs && (
2973
<Script
30-
src="https://static.reo.dev/bf9727b30a874e3/reo.js"
74+
id="hs-script-loader"
75+
async
76+
defer
77+
src={`//js.hs-scripts.com/${hsId}.js`}
3178
onLoad={() => setAfterLoad(true)}
3279
></Script>
3380
)}
3481
</div>
3582
);
3683
}
84+
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+
112+
export default function Scripts() {
113+
return (
114+
<div>
115+
<Reo />
116+
<HubSpot />
117+
<Posthog />
118+
</div>
119+
);
120+
}

package.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)