Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripts update #318

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.docusaurus/*
build/*
node_modules/*
.vercel
.next
.env*

# Generated
public/robots.txt
Expand Down
3 changes: 3 additions & 0 deletions components/nextra/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { useRouter } from 'next/router';
import { Link } from 'nextra-theme-docs';
import { useMounted } from 'nextra/hooks';
import { InformationCircleIcon, SpinnerIcon } from 'nextra/icons';
import { usePostHog } from 'posthog-js/react';
import type { CompositionEvent, KeyboardEvent, ReactElement } from 'react';
import { Fragment, useCallback, useEffect, useRef, useState } from 'react';
import { Input } from './Input';
Expand Down Expand Up @@ -64,6 +65,7 @@ export function Search({
const [focused, setFocused] = useState(false);
// Trigger the search after the Input is complete for languages like Chinese
const [composition, setComposition] = useState(true);
const posthog = usePostHog();

useEffect(() => {
setActive(0);
Expand Down Expand Up @@ -101,6 +103,7 @@ export function Search({
}, []);

const finishSearch = useCallback(() => {
posthog?.capture('search', { query: input.current.value });
input.current?.blur();
onChange('');
setShow(false);
Expand Down
94 changes: 89 additions & 5 deletions components/scripts.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use client';

import inEU from '@segment/in-eu';
import { usePathname, useSearchParams } from 'next/navigation';
import { Router } from 'next/router';
import Script from 'next/script';
import posthog from 'posthog-js';
import { useEffect, useState } from 'react';

export default function Scripts() {
const isProd = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';

function Reo() {
const reoClientId = process.env.NEXT_PUBLIC_REO_CLIENT_ID;
const [loadReo, setLoadReo] = useState(false);
const [afterLoad, setAfterLoad] = useState(false);
const isProd = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';

useEffect(() => {
if (window) {
Expand All @@ -19,18 +24,97 @@ export default function Scripts() {
// @ts-ignore
if (afterLoad && window.Reo) {
// @ts-ignore
window.Reo.init({ clientID: 'bf9727b30a874e3' });
window.Reo.init({ clientID: reoClientId });
}
}, [afterLoad]);

return (
<div>
{isProd && loadReo && (
{reoClientId && isProd && loadReo && (
<Script
src={`https://static.reo.dev/${reoClientId}/reo.js`}
onLoad={() => setAfterLoad(true)}
defer
></Script>
)}
</div>
);
}

function HubSpot() {
const hsId = process.env.NEXT_PUBLIC_HUBSPOT_ID;
const [loadHs, setLoadHs] = useState(false);
const [afterLoad, setAfterLoad] = useState(false);
const pathname = usePathname();
const searchParams = useSearchParams();

useEffect(() => {
if (window) {
setLoadHs(!inEU());
}
}, [loadHs]);

useEffect(() => {
// @ts-ignore
const hs = window?._hsq;
if (afterLoad && pathname && hs) {
let path = pathname;
if (searchParams && searchParams.toString()) {
path = path + `?${searchParams.toString()}`;
}
hs.push(['setPath', path]);
hs.push(['trackPageView']);
}
}, [afterLoad, pathname, searchParams]);

return (
<div>
{hsId && isProd && loadHs && (
<Script
src="https://static.reo.dev/bf9727b30a874e3/reo.js"
id="hs-script-loader"
async
defer
src={`//js.hs-scripts.com/${hsId}.js`}
onLoad={() => setAfterLoad(true)}
></Script>
)}
</div>
);
}

function Posthog() {
useEffect(() => {
if (inEU() || !process.env.NEXT_PUBLIC_POSTHOG_KEY) {
return;
}

posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY ?? '', {
api_host: isProd ? '/ingest' : process.env.NEXT_PUBLIC_POSTHOG_HOST, // See Posthog rewrites in next config
ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
person_profiles: 'always',
loaded: (posthog) => {
if (process.env.NODE_ENV === 'development') posthog.debug();
},
});

const handleRouteChange = () => posthog?.capture('$pageview');

Router.events.on('routeChangeComplete', handleRouteChange);

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

return <></>;
}

export default function Scripts() {
return (
<div>
<Reo />
<HubSpot />
<Posthog />
</div>
);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"next-sitemap": "^4.2.3",
"nextra": "^2.13.4",
"nextra-theme-docs": "^2.13.4",
"posthog-js": "^1.223.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-youtube": "^10.1.0",
Expand Down
6 changes: 5 additions & 1 deletion pages/_app.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import posthog from 'posthog-js'
import { PostHogProvider } from 'posthog-js/react'
import { SpeedInsights } from "@vercel/speed-insights/next"

import Layout from '@/components/layout';
Expand All @@ -7,7 +9,9 @@ export default function MyApp({ Component, pageProps }) {
return (
<Layout>
<SpeedInsights/>
<Component {...pageProps} />
<PostHogProvider client={posthog}>
<Component {...pageProps} />
</PostHogProvider>
</Layout>
);
}
50 changes: 50 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.