fix: set random groupId if events from a server without a profile id
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
import { getClientIp } from '@/utils/get-client-ip';
|
import { getClientIp } from '@/utils/get-client-ip';
|
||||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
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 { getSalts } from '@openpanel/db';
|
||||||
import { eventsGroupQueue, eventsQueue } from '@openpanel/queue';
|
import { eventsGroupQueue, eventsQueue } from '@openpanel/queue';
|
||||||
import { getLock, getRedisCache } from '@openpanel/redis';
|
import { getLock, getRedisCache } from '@openpanel/redis';
|
||||||
import type { PostEventPayload } from '@openpanel/sdk';
|
import type { PostEventPayload } from '@openpanel/sdk';
|
||||||
|
|
||||||
import { checkDuplicatedEvent } from '@/utils/deduplicate';
|
import { checkDuplicatedEvent } from '@/utils/deduplicate';
|
||||||
|
import { generateId } from '@openpanel/common';
|
||||||
import { getGeoLocation } from '@openpanel/geo';
|
import { getGeoLocation } from '@openpanel/geo';
|
||||||
import { getStringHeaders, getTimestamp } from './track.controller';
|
import { getStringHeaders, getTimestamp } from './track.controller';
|
||||||
|
|
||||||
@@ -62,9 +63,12 @@ export async function postEvent(
|
|||||||
|
|
||||||
const isGroupQueue = await getRedisCache().exists('group_queue');
|
const isGroupQueue = await getRedisCache().exists('group_queue');
|
||||||
if (isGroupQueue) {
|
if (isGroupQueue) {
|
||||||
|
const uaInfo = parseUserAgent(ua, request.body?.properties);
|
||||||
const groupId = request.body?.profileId
|
const groupId = request.body?.profileId
|
||||||
? `${projectId}:${request.body?.profileId}`
|
? `${projectId}:${request.body?.profileId}`
|
||||||
: currentDeviceId;
|
: uaInfo.isServer
|
||||||
|
? `${projectId}:${generateId()}`
|
||||||
|
: currentDeviceId;
|
||||||
await eventsGroupQueue.add({
|
await eventsGroupQueue.add({
|
||||||
orderMs: new Date(timestamp).getTime(),
|
orderMs: new Date(timestamp).getTime(),
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import type { FastifyReply, FastifyRequest } from 'fastify';
|
|||||||
import { path, assocPath, pathOr, pick } from 'ramda';
|
import { path, assocPath, pathOr, pick } from 'ramda';
|
||||||
|
|
||||||
import { checkDuplicatedEvent } from '@/utils/deduplicate';
|
import { checkDuplicatedEvent } from '@/utils/deduplicate';
|
||||||
|
import { generateId } from '@openpanel/common';
|
||||||
import { generateDeviceId, parseUserAgent } from '@openpanel/common/server';
|
import { generateDeviceId, parseUserAgent } from '@openpanel/common/server';
|
||||||
import { getProfileById, getSalts, upsertProfile } from '@openpanel/db';
|
import { getProfileById, getSalts, upsertProfile } from '@openpanel/db';
|
||||||
import { type GeoLocation, getGeoLocation } from '@openpanel/geo';
|
import { type GeoLocation, getGeoLocation } from '@openpanel/geo';
|
||||||
@@ -282,9 +283,12 @@ async function track({
|
|||||||
}) {
|
}) {
|
||||||
const isGroupQueue = await getRedisCache().exists('group_queue');
|
const isGroupQueue = await getRedisCache().exists('group_queue');
|
||||||
if (isGroupQueue) {
|
if (isGroupQueue) {
|
||||||
|
const uaInfo = parseUserAgent(headers['user-agent'], payload.properties);
|
||||||
const groupId = payload.profileId
|
const groupId = payload.profileId
|
||||||
? `${projectId}:${payload.profileId}`
|
? `${projectId}:${payload.profileId}`
|
||||||
: currentDeviceId;
|
: uaInfo.isServer
|
||||||
|
? `${projectId}:${generateId()}`
|
||||||
|
: currentDeviceId;
|
||||||
await eventsGroupQueue.add({
|
await eventsGroupQueue.add({
|
||||||
orderMs: new Date(timestamp).getTime(),
|
orderMs: new Date(timestamp).getTime(),
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Reference in New Issue
Block a user