OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix: set random groupId if events from a server without a profile id
Carl-Gerhard Lindesvärde4bc21fc2e16
diff --git a/apps/api/src/controllers/event.controller.ts b/apps/api/src/controllers/event.controller.tsindex dcecedbb1..7a01737f8 100644--- a/apps/api/src/controllers/event.controller.ts+++ b/apps/api/src/controllers/event.controller.ts@@ -1,13 +1,14 @@import { getClientIp } from '@/utils/get-client-ip';import type { FastifyReply, FastifyRequest } from 'fastify';-import { generateDeviceId } from '@openpanel/common/server';+import { generateDeviceId, parseUserAgent } from '@openpanel/common/server';import { getSalts } from '@openpanel/db';import { eventsGroupQueue, eventsQueue } from '@openpanel/queue';import { getLock, getRedisCache } from '@openpanel/redis';import type { PostEventPayload } from '@openpanel/sdk';import { checkDuplicatedEvent } from '@/utils/deduplicate';+import { generateId } from '@openpanel/common';import { getGeoLocation } from '@openpanel/geo';import { getStringHeaders, getTimestamp } from './track.controller';@@ -62,9 +63,12 @@ export async function postEvent(const isGroupQueue = await getRedisCache().exists('group_queue');if (isGroupQueue) {+ const uaInfo = parseUserAgent(ua, request.body?.properties);const groupId = request.body?.profileId? `${projectId}:${request.body?.profileId}`- : currentDeviceId;+ : uaInfo.isServer+ ? `${projectId}:${generateId()}`+ : currentDeviceId;await eventsGroupQueue.add({orderMs: new Date(timestamp).getTime(),data: {diff --git a/apps/api/src/controllers/track.controller.ts b/apps/api/src/controllers/track.controller.tsindex e9760f97e..6ce64949c 100644--- a/apps/api/src/controllers/track.controller.ts+++ b/apps/api/src/controllers/track.controller.ts@@ -3,6 +3,7 @@ import type { FastifyReply, FastifyRequest } from 'fastify';import { path, assocPath, pathOr, pick } from 'ramda';import { checkDuplicatedEvent } from '@/utils/deduplicate';+import { generateId } from '@openpanel/common';import { generateDeviceId, parseUserAgent } from '@openpanel/common/server';import { getProfileById, getSalts, upsertProfile } from '@openpanel/db';import { type GeoLocation, getGeoLocation } from '@openpanel/geo';@@ -282,9 +283,12 @@ async function track({}) {const isGroupQueue = await getRedisCache().exists('group_queue');if (isGroupQueue) {+ const uaInfo = parseUserAgent(headers['user-agent'], payload.properties);const groupId = payload.profileId? `${projectId}:${payload.profileId}`- : currentDeviceId;+ : uaInfo.isServer+ ? `${projectId}:${generateId()}`+ : currentDeviceId;await eventsGroupQueue.add({orderMs: new Date(timestamp).getTime(),data: {