From 5a431ae5f229e04bc79a79c936af85fe220baa91 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 11 Mar 2026 18:34:48 -0500 Subject: [PATCH] Add Plausible Analytics integration with AppConfig support --- .agents/skills/plausible-analytics/SKILL.md | 121 ++++++++++++++++++++ package-lock.json | 7 ++ package.json | 1 + src/App.tsx | 9 +- src/components/AppProvider.tsx | 2 + src/components/PlausibleProvider.tsx | 29 +++++ src/components/SentryProvider.test.tsx | 8 ++ src/contexts/AppContext.ts | 4 + src/hooks/useGenerateProjectInfo.test.ts | 4 + src/hooks/useGit.test.ts | 2 + src/lib/SessionManager.test.ts | 2 + src/lib/system.test.ts | 2 + src/test/TestApp.tsx | 2 + 13 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 .agents/skills/plausible-analytics/SKILL.md create mode 100644 src/components/PlausibleProvider.tsx diff --git a/.agents/skills/plausible-analytics/SKILL.md b/.agents/skills/plausible-analytics/SKILL.md new file mode 100644 index 00000000..292faf71 --- /dev/null +++ b/.agents/skills/plausible-analytics/SKILL.md @@ -0,0 +1,121 @@ +--- +name: plausible-analytics +description: Add Plausible Analytics tracking to the application, configured through AppConfig and environment variables. +--- + +# Plausible Analytics + +Add privacy-friendly analytics with [Plausible](https://plausible.io/) using the `@plausible-analytics/tracker` npm package. Configuration lives in `AppConfig` so it can be set via `VITE_` environment variables. + +## 1. Install the package + +```bash +npm install @plausible-analytics/tracker +``` + +## 2. Add fields to `AppConfig` + +In `src/contexts/AppContext.ts`, add two fields to the `AppConfig` interface: + +```typescript +export interface AppConfig { + // ...existing fields... + /** Plausible Analytics domain (empty string = disabled). */ + plausibleDomain: string; + /** Plausible Analytics API endpoint (empty string = use default). */ + plausibleEndpoint: string; +} +``` + +## 3. Update the Zod schema in `AppProvider.tsx` + +Add the new fields to the `AppConfigSchema`: + +```typescript +const AppConfigSchema = z.object({ + // ...existing fields... + plausibleDomain: z.string(), + plausibleEndpoint: z.string(), +}) satisfies z.ZodType; +``` + +## 4. Create `PlausibleProvider` + +Create `src/components/PlausibleProvider.tsx`: + +```tsx +import { ReactNode, useEffect, useRef } from 'react'; +import { useAppContext } from '@/hooks/useAppContext'; + +interface PlausibleProviderProps { + children: ReactNode; +} + +/** + * Reactively initializes Plausible Analytics from AppConfig. + * Plausible's `init()` can only be called once, so we guard with a ref. + */ +export function PlausibleProvider({ children }: PlausibleProviderProps) { + const { config } = useAppContext(); + const initializedRef = useRef(false); + + useEffect(() => { + if (initializedRef.current || !config.plausibleDomain) return; + initializedRef.current = true; + + import('@plausible-analytics/tracker').then(({ init }) => { + init({ + domain: config.plausibleDomain, + ...(config.plausibleEndpoint && { endpoint: config.plausibleEndpoint }), + }); + }).catch(console.error); + }, [config.plausibleDomain, config.plausibleEndpoint]); + + return <>{children}; +} +``` + +## 5. Wire into `App.tsx` + +Import `PlausibleProvider` and add it inside `AppProvider` (it needs access to `useAppContext`): + +```tsx +import { PlausibleProvider } from '@/components/PlausibleProvider'; + +// In the defaultConfig, add: +const defaultConfig: AppConfig = { + // ...existing fields... + plausibleDomain: import.meta.env.VITE_PLAUSIBLE_DOMAIN || '', + plausibleEndpoint: import.meta.env.VITE_PLAUSIBLE_ENDPOINT || '', +}; + +// In the JSX, wrap children of AppProvider: + + + {/* ...rest of providers... */} + + +``` + +## 6. Update `TestApp.tsx` + +Add the new fields to the test default config with empty strings (disabled): + +```typescript +const defaultConfig: AppConfig = { + // ...existing fields... + plausibleDomain: '', + plausibleEndpoint: '', +}; +``` + +## 7. Configure via environment variables + +Create or update `.env`: + +``` +VITE_PLAUSIBLE_DOMAIN="example.com" +VITE_PLAUSIBLE_ENDPOINT="https://plausible.example.com/api/event" +``` + +`VITE_PLAUSIBLE_ENDPOINT` is optional -- it defaults to Plausible Cloud's endpoint if omitted. Set it when using a self-hosted Plausible instance. diff --git a/package-lock.json b/package-lock.json index d6122d9e..4044cef4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "@nostrify/nostrify": "^0.49.2", "@nostrify/react": "^0.2.24", "@opensecret/react": "^1.5.4", + "@plausible-analytics/tracker": "^0.4.4", "@radix-ui/react-accordion": "^1.2.0", "@radix-ui/react-alert-dialog": "^1.1.1", "@radix-ui/react-aspect-ratio": "^1.1.0", @@ -3212,6 +3213,12 @@ "node": ">=14" } }, + "node_modules/@plausible-analytics/tracker": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/@plausible-analytics/tracker/-/tracker-0.4.4.tgz", + "integrity": "sha512-fz0NOYUEYXtg1TBaPEEvtcBq3FfmLFuTe1VZw4M8sTWX129br5dguu3M15+plOQnc181ShYe67RfwhKgK89VnA==", + "license": "MIT" + }, "node_modules/@radix-ui/number": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@radix-ui/number/-/number-1.1.1.tgz", diff --git a/package.json b/package.json index ae48af24..19ca11c8 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "@nostrify/nostrify": "^0.49.2", "@nostrify/react": "^0.2.24", "@opensecret/react": "^1.5.4", + "@plausible-analytics/tracker": "^0.4.4", "@radix-ui/react-accordion": "^1.2.0", "@radix-ui/react-alert-dialog": "^1.1.1", "@radix-ui/react-aspect-ratio": "^1.1.0", diff --git a/src/App.tsx b/src/App.tsx index 90287a77..d5a9f4a9 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -15,6 +15,7 @@ import { AppProvider } from '@/components/AppProvider'; import { AppConfig } from '@/contexts/AppContext'; import { useAppContext } from '@/hooks/useAppContext'; import { SentryProvider } from '@/components/SentryProvider'; +import { PlausibleProvider } from '@/components/PlausibleProvider'; import { AISettingsProvider } from '@/components/AISettingsProvider'; import { GitSettingsProvider } from '@/components/GitSettingsProvider'; import { DeploySettingsProvider } from '@/components/DeploySettingsProvider'; @@ -87,6 +88,8 @@ const defaultConfig: AppConfig = { fsPathTemplates: "/templates", sentryDsn: import.meta.env.VITE_SENTRY_DSN || "", sentryEnabled: true, + plausibleDomain: import.meta.env.VITE_PLAUSIBLE_DOMAIN || "", + plausibleEndpoint: import.meta.env.VITE_PLAUSIBLE_ENDPOINT || "", }; // Initialize filesystem adapter @@ -131,7 +134,8 @@ export function App() { - + + @@ -162,7 +166,8 @@ export function App() { - + + diff --git a/src/components/AppProvider.tsx b/src/components/AppProvider.tsx index 5bd29655..d30d8404 100644 --- a/src/components/AppProvider.tsx +++ b/src/components/AppProvider.tsx @@ -60,6 +60,8 @@ const AppConfigSchema = z.object({ sentryDsn: z.string(), sentryEnabled: z.boolean(), systemPrompt: z.string(), + plausibleDomain: z.string(), + plausibleEndpoint: z.string(), }) satisfies z.ZodType; export function AppProvider(props: AppProviderProps) { diff --git a/src/components/PlausibleProvider.tsx b/src/components/PlausibleProvider.tsx new file mode 100644 index 00000000..829e8790 --- /dev/null +++ b/src/components/PlausibleProvider.tsx @@ -0,0 +1,29 @@ +import { ReactNode, useEffect, useRef } from 'react'; +import { useAppContext } from '@/hooks/useAppContext'; + +interface PlausibleProviderProps { + children: ReactNode; +} + +/** + * Reactively initializes Plausible Analytics from AppConfig. + * Plausible's `init()` can only be called once, so we guard with a ref. + */ +export function PlausibleProvider({ children }: PlausibleProviderProps) { + const { config } = useAppContext(); + const initializedRef = useRef(false); + + useEffect(() => { + if (initializedRef.current || !config.plausibleDomain) return; + initializedRef.current = true; + + import('@plausible-analytics/tracker').then(({ init }) => { + init({ + domain: config.plausibleDomain, + ...(config.plausibleEndpoint && { endpoint: config.plausibleEndpoint }), + }); + }).catch(console.error); + }, [config.plausibleDomain, config.plausibleEndpoint]); + + return <>{children}; +} diff --git a/src/components/SentryProvider.test.tsx b/src/components/SentryProvider.test.tsx index 4b600986..15f13a6f 100644 --- a/src/components/SentryProvider.test.tsx +++ b/src/components/SentryProvider.test.tsx @@ -45,6 +45,8 @@ describe('SentryProvider', () => { fsPathTmp: '/tmp', fsPathPlugins: '/plugins', fsPathTemplates: '/templates', + plausibleDomain: '', + plausibleEndpoint: '', }; render( @@ -87,6 +89,8 @@ describe('SentryProvider', () => { fsPathTmp: '/tmp', fsPathPlugins: '/plugins', fsPathTemplates: '/templates', + plausibleDomain: '', + plausibleEndpoint: '', }; render( @@ -130,6 +134,8 @@ describe('SentryProvider', () => { fsPathTmp: '/tmp', fsPathPlugins: '/plugins', fsPathTemplates: '/templates', + plausibleDomain: '', + plausibleEndpoint: '', }; render( @@ -176,6 +182,8 @@ describe('SentryProvider', () => { fsPathTmp: '/tmp', fsPathPlugins: '/plugins', fsPathTemplates: '/templates', + plausibleDomain: '', + plausibleEndpoint: '', }; render( diff --git a/src/contexts/AppContext.ts b/src/contexts/AppContext.ts index ac8b40c1..4ea8db35 100644 --- a/src/contexts/AppContext.ts +++ b/src/contexts/AppContext.ts @@ -68,6 +68,10 @@ export interface AppConfig { sentryEnabled: boolean; /** System prompt EJS template */ systemPrompt?: string; + /** Plausible Analytics domain (empty string = disabled). */ + plausibleDomain: string; + /** Plausible Analytics API endpoint (empty string = use default). */ + plausibleEndpoint: string; } export interface AppContextType { diff --git a/src/hooks/useGenerateProjectInfo.test.ts b/src/hooks/useGenerateProjectInfo.test.ts index 098c94ae..d52331ff 100644 --- a/src/hooks/useGenerateProjectInfo.test.ts +++ b/src/hooks/useGenerateProjectInfo.test.ts @@ -80,6 +80,8 @@ describe('useGenerateProjectInfo', () => { fsPathTemplates: '/templates', sentryDsn: '', sentryEnabled: false, + plausibleDomain: '', + plausibleEndpoint: '', }; mockUseAppContext.mockReturnValue({ config, @@ -242,6 +244,8 @@ describe('useGenerateProjectInfo', () => { fsPathTemplates: '/templates', sentryDsn: '', sentryEnabled: false, + plausibleDomain: '', + plausibleEndpoint: '', }; mockUseAppContext.mockReturnValue({ config, diff --git a/src/hooks/useGit.test.ts b/src/hooks/useGit.test.ts index e049ac7d..296dea26 100644 --- a/src/hooks/useGit.test.ts +++ b/src/hooks/useGit.test.ts @@ -54,6 +54,8 @@ function createWrapper(corsProxy: string) { fsPathTemplates: '/templates', sentryDsn: '', sentryEnabled: false, + plausibleDomain: '', + plausibleEndpoint: '', }; return createElement( diff --git a/src/lib/SessionManager.test.ts b/src/lib/SessionManager.test.ts index 32c5a961..5264a5d1 100644 --- a/src/lib/SessionManager.test.ts +++ b/src/lib/SessionManager.test.ts @@ -58,6 +58,8 @@ const testConfig: AppConfig = { fsPathTemplates: '/templates', sentryDsn: '', sentryEnabled: false, + plausibleDomain: '', + plausibleEndpoint: '', }; describe('SessionManager', () => { diff --git a/src/lib/system.test.ts b/src/lib/system.test.ts index 993e9955..1cb81017 100644 --- a/src/lib/system.test.ts +++ b/src/lib/system.test.ts @@ -45,6 +45,8 @@ const testConfig: AppConfig = { fsPathTemplates: '/templates', sentryDsn: '', sentryEnabled: false, + plausibleDomain: '', + plausibleEndpoint: '', }; describe('makeSystemPrompt', () => { diff --git a/src/test/TestApp.tsx b/src/test/TestApp.tsx index 3568fe7a..2d044571 100644 --- a/src/test/TestApp.tsx +++ b/src/test/TestApp.tsx @@ -65,6 +65,8 @@ export function TestApp({ children }: TestAppProps) { fsPathTemplates: '/templates', sentryDsn: '', sentryEnabled: false, + plausibleDomain: '', + plausibleEndpoint: '', }; // Initialize LightningFS for testing