[backport cloud/1.41] feat: add server-side PostHog config overrides (#9758) (#9855)

This commit is contained in:
Robin Huang
2026-03-12 23:29:53 -07:00
committed by GitHub
parent 2c8ad1380f
commit c0ea8f1e31
4 changed files with 26 additions and 14 deletions

2
global.d.ts vendored
View File

@@ -36,7 +36,7 @@ interface Window {
mixpanel_token?: string
posthog_project_token?: string
posthog_api_host?: string
posthog_debug?: boolean
posthog_config?: Record<string, unknown>
require_whitelist?: boolean
subscription_required?: boolean
max_upload_size?: number

View File

@@ -1,3 +1,5 @@
import type { PostHogConfig } from 'posthog-js'
import type { TelemetryEventName } from '@/platform/telemetry/types'
/**
@@ -31,7 +33,7 @@ export type RemoteConfig = {
mixpanel_token?: string
posthog_project_token?: string
posthog_api_host?: string
posthog_debug?: boolean
posthog_config?: Partial<PostHogConfig>
subscription_required?: boolean
server_health_alert?: ServerHealthAlert
max_upload_size?: number

View File

@@ -40,8 +40,12 @@ vi.mock('@/composables/auth/useCurrentUser', () => ({
})
}))
const mockRemoteConfig = vi.hoisted(
() => ({ value: null }) as { value: Record<string, unknown> | null }
)
vi.mock('@/platform/remoteConfig/remoteConfig', () => ({
remoteConfig: { value: null }
remoteConfig: mockRemoteConfig
}))
vi.mock('posthog-js', () => hoisted.mockPosthog)
@@ -67,6 +71,7 @@ function createProvider(
describe('PostHogTelemetryProvider', () => {
beforeEach(() => {
vi.clearAllMocks()
mockRemoteConfig.value = null
window.__CONFIG__ = {
posthog_project_token: 'phc_test_token'
} as typeof window.__CONFIG__
@@ -82,7 +87,7 @@ describe('PostHogTelemetryProvider', () => {
expect(hoisted.mockCapture).not.toHaveBeenCalled()
})
it('calls posthog.init with the token and default api_host', async () => {
it('calls posthog.init with the token and default config', async () => {
createProvider()
await vi.dynamicImportSettled()
@@ -99,17 +104,22 @@ describe('PostHogTelemetryProvider', () => {
)
})
it('enables debug mode when posthog_debug is true in config', async () => {
window.__CONFIG__ = {
posthog_project_token: 'phc_test_token',
posthog_debug: true
} as typeof window.__CONFIG__
new PostHogTelemetryProvider()
it('applies posthog_config overrides from remote config', async () => {
mockRemoteConfig.value = {
posthog_config: {
debug: true,
api_host: 'https://custom.host.com'
}
}
createProvider()
await vi.dynamicImportSettled()
expect(hoisted.mockInit).toHaveBeenCalledWith(
'phc_test_token',
expect.objectContaining({ debug: true })
expect.objectContaining({
debug: true,
api_host: 'https://custom.host.com'
})
)
})

View File

@@ -102,6 +102,7 @@ export class PostHogTelemetryProvider implements TelemetryProvider {
void import('posthog-js')
.then((posthogModule) => {
this.posthog = posthogModule.default
const serverConfig = remoteConfig.value?.posthog_config ?? {}
this.posthog!.init(apiKey, {
api_host:
window.__CONFIG__?.posthog_api_host || 'https://t.comfy.org',
@@ -110,9 +111,8 @@ export class PostHogTelemetryProvider implements TelemetryProvider {
capture_pageview: false,
capture_pageleave: false,
persistence: 'localStorage+cookie',
debug:
window.__CONFIG__?.posthog_debug ??
import.meta.env.VITE_POSTHOG_DEBUG === 'true'
debug: import.meta.env.VITE_POSTHOG_DEBUG === 'true',
...serverConfig
})
this.isInitialized = true
this.flushEventQueue()