diff --git a/docs/.vitepress/modules/posthog.ts b/docs/.vitepress/modules/posthog.ts index 890821d65..a77d72bd6 100644 --- a/docs/.vitepress/modules/posthog.ts +++ b/docs/.vitepress/modules/posthog.ts @@ -1,9 +1,12 @@ import posthog from 'posthog-js' -import { DEFAULT_POSTHOG_CONFIG, POSTHOG_PROJECT_KEY_DOCS } from '../../../posthog.config' +import { DEFAULT_POSTHOG_CONFIG, POSTHOG_PROJECT_KEY } from '../../../posthog.config' if (!import.meta.env.DEV) { - posthog.init(POSTHOG_PROJECT_KEY_DOCS, { + posthog.init(POSTHOG_PROJECT_KEY, { ...DEFAULT_POSTHOG_CONFIG, }) + // Tag docs-site traffic so it can be told apart from the app surfaces + // inside the shared project. + posthog.register({ surface: 'docs' }) } diff --git a/packages/stage-ui/src/stores/analytics/posthog.ts b/packages/stage-ui/src/stores/analytics/posthog.ts index 140cb0a9f..2f5f64104 100644 --- a/packages/stage-ui/src/stores/analytics/posthog.ts +++ b/packages/stage-ui/src/stores/analytics/posthog.ts @@ -7,21 +7,23 @@ import { isStageCapacitor, isStageTamagotchi } from '@proj-airi/stage-shared' import { DEFAULT_POSTHOG_CONFIG, POSTHOG_ENABLED, - POSTHOG_PROJECT_KEY_DESKTOP, - POSTHOG_PROJECT_KEY_POCKET, - POSTHOG_PROJECT_KEY_WEB, + POSTHOG_PROJECT_KEY, } from '../../../../../posthog.config' let posthogInitialized = false -function getPosthogProjectKey(): string { +// All AIRI surfaces (web, desktop, mobile) capture into a single PostHog +// project. The platform is carried on every event via the `surface` super +// property (registered at init), so cross-platform funnels live in one +// project instead of being split across per-platform projects. +function currentSurface(): 'web' | 'mobile' | 'electron' { if (isStageTamagotchi()) - return POSTHOG_PROJECT_KEY_DESKTOP + return 'electron' if (isStageCapacitor()) - return POSTHOG_PROJECT_KEY_POCKET + return 'mobile' - return POSTHOG_PROJECT_KEY_WEB + return 'web' } export function isPosthogAvailableInBuild(): boolean { @@ -35,10 +37,13 @@ export function ensurePosthogInitialized(enabled: boolean): boolean { if (posthogInitialized) return true - posthog.init(getPosthogProjectKey(), { + posthog.init(POSTHOG_PROJECT_KEY, { ...DEFAULT_POSTHOG_CONFIG, opt_out_capturing_by_default: !enabled, }) + // Tag every event (including autocapture / pageview) with the platform so + // the single project can still be broken down by web / desktop / mobile. + posthog.register({ surface: currentSurface() }) posthogInitialized = true return true } diff --git a/posthog.config.ts b/posthog.config.ts index de9c1588f..573c21519 100644 --- a/posthog.config.ts +++ b/posthog.config.ts @@ -12,22 +12,11 @@ function isEnvFlagEnabled(value: string | undefined): boolean { // For Release workflows set `VITE_ENABLE_POSTHOG=true`. export const POSTHOG_ENABLED = isEnvFlagEnabled(import.meta.env.VITE_ENABLE_POSTHOG) -export const POSTHOG_PROJECT_KEY_WEB - = import.meta.env.VITE_POSTHOG_PROJECT_KEY_WEB - ?? 'phc_pzjziJjrVZpa9SqnQqq0QEKvkmuCPH7GDTA6TbRTEf9' // cspell:disable-line - -export const POSTHOG_PROJECT_KEY_DESKTOP - = import.meta.env.VITE_POSTHOG_PROJECT_KEY_DESKTOP - ?? 'phc_rljw376z5gt6vXJlc3sTr7hFbXodciY9THEQXIRnW53'// cspell:disable-line - -// FIXME: Using the same key for 'web' for now. -export const POSTHOG_PROJECT_KEY_POCKET - = import.meta.env.VITE_POSTHOG_PROJECT_KEY_POCKET - ?? 'phc_pzjziJjrVZpa9SqnQqq0QEKvkmuCPH7GDTA6TbRTEf9' // cspell:disable-line - -// FIXME: Using the same key for 'web' for now. -export const POSTHOG_PROJECT_KEY_DOCS - = import.meta.env.VITE_POSTHOG_PROJECT_KEY_DOCS +// Single PostHog project for every AIRI surface (web / desktop / mobile). +// Platforms are told apart by the `surface` super property set at init, not +// by routing to separate per-platform projects. +export const POSTHOG_PROJECT_KEY + = import.meta.env.VITE_POSTHOG_PROJECT_KEY ?? 'phc_pzjziJjrVZpa9SqnQqq0QEKvkmuCPH7GDTA6TbRTEf9' // cspell:disable-line export const DEFAULT_POSTHOG_CONFIG = { diff --git a/vite-env.d.ts b/vite-env.d.ts index 11dc7cd1d..27e806e4b 100644 --- a/vite-env.d.ts +++ b/vite-env.d.ts @@ -4,10 +4,7 @@ interface ImportMetaEnv { readonly VITE_DISABLE_FLUX_PURCHASE?: string readonly VITE_DISABLE_CUSTOM_PROVIDERS?: string readonly VITE_ENABLE_POSTHOG?: string - readonly VITE_POSTHOG_PROJECT_KEY_WEB?: string - readonly VITE_POSTHOG_PROJECT_KEY_DESKTOP?: string - readonly VITE_POSTHOG_PROJECT_KEY_POCKET?: string - readonly VITE_POSTHOG_PROJECT_KEY_DOCS?: string + readonly VITE_POSTHOG_PROJECT_KEY?: string } interface ImportMeta {