refactor(posthog): unify project key for all AIRI surfaces and streamline configuration
Some checks failed
Cloudflare Workers (server-dev) / Deploy - stage-web (server-dev) (push) Has been cancelled
Cloudflare Workers (server-dev) / Deploy - ui-server-auth (server-dev) (push) Has been cancelled

This commit is contained in:
RainbowBird 2026-07-08 00:04:51 +08:00
parent fb9461e00c
commit b970cb04ce
No known key found for this signature in database
4 changed files with 24 additions and 30 deletions

View file

@ -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' })
}

View file

@ -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
}

View file

@ -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 = {

5
vite-env.d.ts vendored
View file

@ -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 {