OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix(ingest): ai events no longer get an anonymous device id (#635)
Carl-Gerhard Lindesvärdb5208c94b1cf
diff --git a/apps/start/src/components/ai/ai-breakdown-widget.tsx b/apps/start/src/components/ai/ai-breakdown-widget.tsxindex 6791b70f4..04c28fa25 100644--- a/apps/start/src/components/ai/ai-breakdown-widget.tsx+++ b/apps/start/src/components/ai/ai-breakdown-widget.tsx@@ -72,6 +72,10 @@ function RowLabel({row: IAiBreakdownRow;dimension: AiBreakdownDimension;}) {+ const isAnonymous = dimension === 'profile' && !row.key;+ if (isAnonymous) {+ return <span className="truncate text-muted-foreground">Anonymous</span>;+ }if (dimension === 'profile') {const profile = row.profile;return (diff --git a/bun.lock b/bun.lockindex 79c08a03f..6ee9a56d7 100644--- a/bun.lock+++ b/bun.lock@@ -587,6 +587,7 @@"devDependencies": {"@openpanel/tsconfig": "workspace:*","@types/node": "catalog:",+ "bun-types": "1.4.0","tsup": "7.3.0","typescript": "catalog:",},diff --git a/packages/core/src/modules/ai/ai.service.ts b/packages/core/src/modules/ai/ai.service.tsindex 2eeeab5a1..acd8b382e 100644--- a/packages/core/src/modules/ai/ai.service.ts+++ b/packages/core/src/modules/ai/ai.service.ts@@ -451,7 +451,7 @@ export function createAiService(deps: ServiceDeps, _services: () => Services) {if (input.dimension === 'profile') {const profiles = await getProfilesCached(deps,- items.map((item) => item.key),+ items.map((item) => item.key).filter(Boolean),input.projectId);const byId = new Map(profiles.map((profile) => [profile.id, profile]));diff --git a/packages/core/src/modules/ai/src/sql.ts b/packages/core/src/modules/ai/src/sql.tsindex 59339572e..9e8729c2f 100644--- a/packages/core/src/modules/ai/src/sql.ts+++ b/packages/core/src/modules/ai/src/sql.ts@@ -139,7 +139,7 @@ const METRIC_COLUMNS = sql`countIf(type = 'generation') AS generations,count() AS events,uniq(trace_id) AS traces,- uniq(profile_id) AS users,+ uniqIf(profile_id, profile_id != '') AS users,uniqIf(conversation_id, conversation_id != '') AS conversations,sum(total_cost) AS cost,sum(input_tokens) AS input_tokens,@@ -191,13 +191,16 @@ export function breakdownQuery(const dimension = DIMENSION_COLUMN[input.dimension];const onlyErrors =input.dimension === 'errorCode' ? sql`AND is_error = true` : sql.empty;+ // Anonymous AI usage is a real share of the spend, so it stays as its own row.+ const withoutEmptyKey =+ input.dimension === 'profile' ? sql.empty : sql`HAVING key != ''`;return sql`SELECT${dimension} AS key,countIf(type = 'generation') AS generations,count() AS events,uniq(trace_id) AS traces,- uniq(profile_id) AS users,+ uniqIf(profile_id, profile_id != '') AS users,sum(total_cost) AS cost,sum(input_tokens) + sum(output_tokens) AS tokens,avgIf(latency_ms, type = 'generation') AS avg_latency_ms,@@ -208,7 +211,7 @@ export function breakdownQuery(WHERE ${scopeWhere(input)}${onlyErrors}GROUP BY key- HAVING key != ''+ ${withoutEmptyKey}ORDER BY cost DESC, events DESCLIMIT ${sql.uint64(input.limit)}`;diff --git a/packages/core/src/modules/ingest/ai-track.test.ts b/packages/core/src/modules/ingest/ai-track.test.tsnew file mode 100644index 000000000..26fb48f23--- /dev/null+++ b/packages/core/src/modules/ingest/ai-track.test.ts@@ -0,0 +1,128 @@+import {+ afterAll,+ beforeAll,+ beforeEach,+ describe,+ expect,+ it,+ mock,+} from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';+import type { Buffers } from '../../buffers/create-buffers';+import type { GeoLocation } from '../../clients/geo';+import type { ServiceDeps } from '../../services';+import type { ITrackHandlerPayload } from './ingest.constants';+import type { IncomingEventPayload } from './src/incoming-event';++const PROJECT_ID = 'ai-track-project';+const BROWSER_USER_AGENT =+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/128.0 Safari/537.36';++const getGeoLocation = mock(+ async (): Promise<GeoLocation> => ({+ country: 'SE',+ city: 'Stockholm',+ region: 'AB',+ longitude: 18,+ latitude: 59,+ })+);+const getAsnInfo = mock(async () => ({+ asn: 1234,+ org: 'Example ISP',+ isHosting: false,+}));+const getSalts = mock(async (_deps: unknown) => ({+ current: 'salt-current',+ previous: 'salt-previous',+}));++mock.module('../../clients/geo', () => ({ getGeoLocation, getAsnInfo }));++let ingestTrack: typeof import('./ingest.service').ingestTrack;++let realSaltService: typeof import('../salt/salt.service');+beforeAll(async () => {+ realSaltService = { ...(await import('../salt/salt.service')) };+ mock.module('../salt/salt.service', () => ({+ ...realSaltService,+ getSalts,+ }));+ ({ ingestTrack } = await import('./ingest.service'));+});++afterAll(() => {+ mock.module('../salt/salt.service', () => realSaltService);+});++const produced: { payload: IncomingEventPayload; partitionKey: string }[] = [];+const produceIncomingEvent = async (+ payload: IncomingEventPayload,+ partitionKey: string+) => {+ produced.push({ payload, partitionKey });+};++const buffers = {+ session: {+ getExistingSessions: async (_projectId: string, deviceIds: string[]) =>+ deviceIds.map(() => null),+ },+ replay: {},+ group: {},+} as unknown as Buffers;++const transport = {+ buffers,+ produceIncomingEvent,+ deps: { buffers, config: testCoreConfig() } as unknown as ServiceDeps,+};++const aiRequest = (payload: Record<string, unknown> = {}) => ({+ projectId: PROJECT_ID,+ clientIp: '203.0.113.10',+ headers: { 'user-agent': BROWSER_USER_AGENT },+ clientSecretAuth: true,+ timestamp: Date.now(),+ body: {+ type: 'ai',+ payload: {+ kind: 'generation',+ traceId: 'trace-1',+ model: 'gpt-5-mini',+ provider: 'openai',+ ...payload,+ },+ } as ITrackHandlerPayload,+});++beforeEach(() => {+ produced.length = 0;+});++describe('ingestTrack with an ai body', () => {+ it('mints no device or session id, even with a browser user-agent', async () => {+ const outcome = await ingestTrack(aiRequest(), transport);++ expect(outcome).toEqual({ status: 'ok', deviceId: '', sessionId: '' });+ expect(getGeoLocation).toHaveBeenCalled();+ expect(produced).toHaveLength(1);+ const { payload, partitionKey } = produced[0]!;+ expect(payload.deviceId).toBe('');+ expect(payload.sessionId).toBe('');+ expect(partitionKey).toBeTruthy();+ });++ it('keeps a device id the caller sends', async () => {+ const outcome = await ingestTrack(+ aiRequest({ deviceId: 'device-from-caller' }),+ transport+ );++ expect(outcome).toMatchObject({+ status: 'ok',+ deviceId: 'device-from-caller',+ });+ expect(produced[0]!.payload.deviceId).toBe('device-from-caller');+ });+});diff --git a/packages/core/src/modules/ingest/ingest.service.ts b/packages/core/src/modules/ingest/ingest.service.tsindex d98ac9f72..06f15b1d0 100644--- a/packages/core/src/modules/ingest/ingest.service.ts+++ b/packages/core/src/modules/ingest/ingest.service.ts@@ -106,6 +106,7 @@ const FIFTEEN_MINUTES_MS = 15 * ONE_MINUTE_MS;*/const EARLIEST_ACCEPTED_TIMESTAMP_MS = Date.UTC(2000, 0, 1);const FALLBACK_USER_AGENT = 'unknown/1.0';+const NO_DEVICE = { deviceId: '', sessionId: '' } as const;/** The whitelisted subset that ships with the queue payload. */export function getStringHeaders(@@ -295,17 +296,22 @@ async function buildContext(getSalts(deps),]);- const deviceIdResult = await getDeviceId({- projectId,- ip,- ua,- salts,- overrideDeviceId,- eventTimeMs: timestamp.timestamp,- sessionBuffer: buffers.session,- sessionTimeoutMs,- logger: deps.logger,- });+ // The ip + user-agent hash identifies a browser; for an AI call it would+ // only name the backend that made it, and show up as an anonymous user.+ const isDeviceless = request.body.type === 'ai' && !overrideDeviceId;+ const deviceIdResult = isDeviceless+ ? NO_DEVICE+ : await getDeviceId({+ projectId,+ ip,+ ua,+ salts,+ overrideDeviceId,+ eventTimeMs: timestamp.timestamp,+ sessionBuffer: buffers.session,+ sessionTimeoutMs,+ logger: deps.logger,+ });return {projectId,@@ -412,7 +418,9 @@ async function handleAi(ai: normalized.draft,};- const partitionKey = profileId ? `${projectId}:${profileId}` : deviceId;+ const partitionKey = profileId+ ? `${projectId}:${profileId}`+ : deviceId || generateId();await transport.produceIncomingEvent(queueData, partitionKey);}diff --git a/packages/core/src/modules/ingest/src/incoming-event-handler.test.ts b/packages/core/src/modules/ingest/src/incoming-event-handler.test.tsindex 8b308c5ca..35d022571 100644--- a/packages/core/src/modules/ingest/src/incoming-event-handler.test.ts+++ b/packages/core/src/modules/ingest/src/incoming-event-handler.test.ts@@ -687,6 +687,49 @@ describe('incomingEvent with an ai row', () => {});});+ test('an anonymous ai event opens no session and keeps no device id, even with a browser user-agent', async () => {+ const { deps, createEvent, addAiEvent, ingest, getExistingSession } = ctx;+ const anonymousDocument = {+ ...storedDocument,+ profile_id: '',+ device_id: '',+ session_id: '',+ };+ createEvent.mockResolvedValueOnce({ document: anonymousDocument });++ await incomingEvent(+ buildJobData({+ uaInfo,+ deviceId: '',+ sessionId: '',+ event: {+ name: 'ai_generation',+ properties: {},+ timestamp: Date.now(),+ isTimestampFromThePast: false,+ },+ ai: aiDraft,+ }),+ deps+ );++ expect(ingest).not.toHaveBeenCalled();+ expect(getExistingSession).not.toHaveBeenCalled();+ expect(createdEvents(createEvent)).toEqual([+ expect.objectContaining({+ name: 'ai_generation',+ deviceId: '',+ sessionId: '',+ profileId: '',+ }),+ ]);+ expect(addAiEvent.mock.calls[0]![0]).toMatchObject({+ profile_id: '',+ device_id: '',+ session_id: '',+ });+ });+test('an event the project filter excludes leaves no ai row', async () => {const { deps, createEvent, addAiEvent } = ctx;(deps.projects.getCached as ReturnType<typeof mock>).mockResolvedValue({diff --git a/packages/core/src/modules/ingest/src/incoming-event-handler.ts b/packages/core/src/modules/ingest/src/incoming-event-handler.tsindex 126614e7a..1e6014875 100644--- a/packages/core/src/modules/ingest/src/incoming-event-handler.ts+++ b/packages/core/src/modules/ingest/src/incoming-event-handler.ts@@ -327,6 +327,7 @@ async function ingestIncomingEvent(deviceId,sessionId,uaInfo,+ ai,// Producer-minted; a redelivered message re-creates the same row id.// Absent on messages produced before the field existed — createEvent then// falls back to a fresh uuid.@@ -401,10 +402,11 @@ async function ingestIncomingEvent(: undefined,};- // Server-side and "timestamp from the past" events ride alongside an+ // Server-side, AI and "timestamp from the past" events ride alongside an// existing client session if there is one — they don't open / close- // sessions of their own.- if (uaInfo.isServer || isTimestampFromThePast) {+ // sessions of their own. AI counts as server-side even when the caller+ // forwards a browser's user-agent.+ if (uaInfo.isServer || isTimestampFromThePast || ai) {const session =profileId && !isTimestampFromThePast? await deps.sessions.getExistingSession({ profileId, projectId })diff --git a/packages/sdks/sdk/package.json b/packages/sdks/sdk/package.jsonindex 51e70f485..eb5456cbe 100644--- a/packages/sdks/sdk/package.json+++ b/packages/sdks/sdk/package.json@@ -13,12 +13,14 @@},"scripts": {"build": "rm -rf dist && tsup",- "typecheck": "tsc --noEmit"+ "typecheck": "tsc --noEmit",+ "test": "bun test"},"dependencies": {},"devDependencies": {"@openpanel/tsconfig": "workspace:*","@types/node": "catalog:",+ "bun-types": "1.4.0","tsup": "7.3.0","typescript": "catalog:"}diff --git a/packages/sdks/sdk/src/index.test.ts b/packages/sdks/sdk/src/index.test.tsnew file mode 100644index 000000000..ed996ff33--- /dev/null+++ b/packages/sdks/sdk/src/index.test.ts@@ -0,0 +1,46 @@+import { describe, expect, mock, test } from 'bun:test';+import { OpenPanel } from './index';++const TRACKED_IDS = { deviceId: 'device-1', sessionId: 'session-1' };+const DEVICELESS_IDS = { deviceId: '', sessionId: '' };++function createSdk() {+ const sdk = new OpenPanel({ clientId: 'test-client' });+ const fetch = mock(async () => TRACKED_IDS);+ sdk.api.fetch = fetch as unknown as typeof sdk.api.fetch;+ return { sdk, fetch };+}++describe('ids from /track', () => {+ test('a track event stores the device and session it resolved to', async () => {+ const { sdk, fetch } = createSdk();++ await sdk.track('signup');++ expect(fetch).toHaveBeenCalledTimes(1);+ expect(sdk.getDeviceId()).toBe('device-1');+ expect(sdk.getSessionId()).toBe('session-1');+ });++ test('an ai event keeps the ids the other events resolved', async () => {+ const { sdk, fetch } = createSdk();+ await sdk.track('signup');+ fetch.mockResolvedValueOnce(DEVICELESS_IDS);++ await sdk.ai({ kind: 'span', traceId: 'trace-1' });++ expect(fetch).toHaveBeenCalledTimes(2);+ expect(sdk.getDeviceId()).toBe('device-1');+ expect(sdk.getSessionId()).toBe('session-1');+ });++ test('an ai event on a fresh sdk adopts no ids', async () => {+ const { sdk, fetch } = createSdk();++ await sdk.ai({ kind: 'span', traceId: 'trace-1' });++ expect(fetch).toHaveBeenCalledTimes(1);+ expect(sdk.getDeviceId()).toBe('');+ expect(sdk.getSessionId()).toBe('');+ });+});diff --git a/packages/sdks/sdk/src/index.ts b/packages/sdks/sdk/src/index.tsindex 31bae5888..6d82b60e6 100644--- a/packages/sdks/sdk/src/index.ts+++ b/packages/sdks/sdk/src/index.ts@@ -255,6 +255,11 @@ export class OpenPanel {TrackHandlerPayload,{ deviceId: string; sessionId: string }>('/track', payload, { keepalive: payload.type !== 'replay' });+ // An ai event is stored without a device or session, so its empty ids+ // must not replace the ones the other events resolved.+ if (payload.type === 'ai') {+ return result;+ }this.deviceId = result?.deviceId;const hadSession = !!this.sessionId;this.sessionId = result?.sessionId;diff --git a/packages/sdks/sdk/tsconfig.json b/packages/sdks/sdk/tsconfig.jsonindex 95a0d8537..20da643c7 100644--- a/packages/sdks/sdk/tsconfig.json+++ b/packages/sdks/sdk/tsconfig.json@@ -3,7 +3,8 @@"compilerOptions": {"incremental": false,"outDir": "dist",- "strictNullChecks": true+ "strictNullChecks": true,+ "types": ["bun-types", "node"]},"exclude": ["dist"]}