diff --git a/website/.env-example b/website/.env-example new file mode 100644 index 00000000..244f8a73 --- /dev/null +++ b/website/.env-example @@ -0,0 +1 @@ +VITE_POSTHOG_TOKEN=your_posthog_token \ No newline at end of file diff --git a/website/docs/.vitepress/config.ts b/website/docs/.vitepress/config.ts index 3c0b267f..52b4b660 100644 --- a/website/docs/.vitepress/config.ts +++ b/website/docs/.vitepress/config.ts @@ -107,6 +107,9 @@ const config: UserConfig = { ], ], vite: { + // the root path is website/docs/, so this will make + // vite look for the env file in website/ + envDir: "../", define: { "process.env": {}, }, diff --git a/website/docs/.vitepress/theme/index.ts b/website/docs/.vitepress/theme/index.ts index f6247918..78f58558 100644 --- a/website/docs/.vitepress/theme/index.ts +++ b/website/docs/.vitepress/theme/index.ts @@ -2,4 +2,25 @@ import DefaultTheme from "vitepress/theme"; import "./vars.css"; -export default DefaultTheme; +import type { Theme } from "vitepress"; +import posthog from "posthog-js"; + +const theme: Theme = { + extends: DefaultTheme, + enhanceApp({ router }) { + if (import.meta.env.SSR) return; + const token = import.meta.env.VITE_POSTHOG_TOKEN; + if (!token) return; + + posthog.init(token, { + api_host: "https://app.posthog.com", + autocapture: false, + }); + + router.onAfterRouteChanged = () => { + posthog.capture("$pageview"); + }; + }, +}; + +export default theme; diff --git a/website/vite-env.d.ts b/website/vite-env.d.ts index 12ac2266..c7001372 100644 --- a/website/vite-env.d.ts +++ b/website/vite-env.d.ts @@ -10,3 +10,7 @@ declare module "*!raw" { const lib: [string, string][]; export default lib; } + +interface ImportMetaEnv { + VITE_POSTHOG_TOKEN?: string; +}