From c50c391ff128b027ee5eae22dac9882109b80958 Mon Sep 17 00:00:00 2001 From: AussieScorcher Date: Sat, 3 Oct 2026 07:47:51 +0800 Subject: [PATCH] fix: preserve local anonymous analytics changes --- src/index.ts | 40 ---------------------------------------- src/services/posthog.ts | 11 +++++------ 2 files changed, 5 insertions(+), 46 deletions(-) diff --git a/src/index.ts b/src/index.ts index db30ed6..eb00e28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -444,46 +444,6 @@ app.use('*', async (c, next) => { } }); -app.use('*', async (c, next) => { - const start = Date.now(); - await next(); - try { - const path = c.req.path; - if (c.req.method === 'OPTIONS') return; - if (path === '/favicon.ico') return; - if (path.includes('/health')) return; - // SAFETY: ANALYTICS_IGNORE is an optional text binding supplied by the Worker environment. - const ignoreRaw = (c.env as { ANALYTICS_IGNORE?: string }).ANALYTICS_IGNORE; - if (ignoreRaw) { - const ignores = ignoreRaw - .split(',') - .map((s) => s.trim()) - .filter(Boolean); - for (const pattern of ignores) { - if (pattern.endsWith('*')) { - const prefix = pattern.slice(0, -1); - if (path.startsWith(prefix)) return; - } else if (pattern === path) { - return; - } - } - } - const posthog = ServicePool.getPostHog(c.env); - posthog.track( - 'API Request', - { - path, - method: c.req.method, - status: c.res?.status ?? 0, - duration_ms: Date.now() - start, - }, - 'anonymous', - ); - } catch (err) { - console.warn('[Analytics] failed', err instanceof Error ? err.message : err); - } -}); - app.use( '*', cors({ diff --git a/src/services/posthog.ts b/src/services/posthog.ts index 1151e84..d53a8a7 100644 --- a/src/services/posthog.ts +++ b/src/services/posthog.ts @@ -18,7 +18,6 @@ interface PostHogCapturePayload { event: string; properties: AnalyticsProperties; timestamp?: string; // ISO 8601 - $process_person_profile?: boolean; } export interface TrackOptions { @@ -102,20 +101,22 @@ export class PostHogService { return { distinct_id: distinctId, ...(await this.sanitizeProperties(mergedProps)), + $process_person_profile: false, }; } private dispatch(doFetch: () => Promise, inline = false): void | Promise { - if (inline) return doFetch(); + const task = doFetch(); + if (inline) return task; try { - cfWaitUntil(doFetch()); + cfWaitUntil(task); return; } catch { /* ignore */ } try { const runtimeGlobal: BackgroundTaskRuntime = globalThis; - runtimeGlobal.waitUntil?.(doFetch()); + runtimeGlobal.waitUntil?.(task); } catch { /* ignore */ } @@ -133,7 +134,6 @@ export class PostHogService { api_key: this.apiKey!, event, properties: await this.prepareProperties(properties, distinctId, options), - $process_person_profile: false, }; if (options.timestamp) { payload.timestamp = options.timestamp instanceof Date ? options.timestamp.toISOString() : options.timestamp; @@ -174,7 +174,6 @@ export class PostHogService { const payload: Omit = { event: item.event, properties: await this.prepareProperties(item.properties ?? {}, item.distinctId ?? 'anonymous', options), - $process_person_profile: false, }; if (item.timestamp) { payload.timestamp = item.timestamp instanceof Date ? item.timestamp.toISOString() : item.timestamp;