Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
11 changes: 5 additions & 6 deletions src/services/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ interface PostHogCapturePayload {
event: string;
properties: AnalyticsProperties;
timestamp?: string; // ISO 8601
$process_person_profile?: boolean;
}

export interface TrackOptions {
Expand Down Expand Up @@ -102,20 +101,22 @@ export class PostHogService {
return {
distinct_id: distinctId,
...(await this.sanitizeProperties(mergedProps)),
$process_person_profile: false,
Comment thread
AussieScorcher marked this conversation as resolved.
Resolved
};
}

private dispatch(doFetch: () => Promise<void>, inline = false): void | Promise<void> {
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 */
}
Expand All @@ -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;
Expand Down Expand Up @@ -174,7 +174,6 @@ export class PostHogService {
const payload: Omit<PostHogCapturePayload, 'api_key'> = {
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;
Expand Down
Loading