Updated id logic.

This commit is contained in:
Mike Cao 2025-04-25 03:19:37 -07:00
parent 340cdce1dc
commit e8a933f80e
2 changed files with 21 additions and 15 deletions

View file

@ -18,7 +18,6 @@ const schema = z.object({
payload: z.object({ payload: z.object({
website: z.string().uuid(), website: z.string().uuid(),
data: anyObjectParam.optional(), data: anyObjectParam.optional(),
id: z.string().optional(),
hostname: z.string().max(100).optional(), hostname: z.string().max(100).optional(),
language: z.string().max(35).optional(), language: z.string().max(35).optional(),
referrer: urlOrPathParam.optional(), referrer: urlOrPathParam.optional(),
@ -30,6 +29,7 @@ const schema = z.object({
ip: z.string().ip().optional(), ip: z.string().ip().optional(),
userAgent: z.string().optional(), userAgent: z.string().optional(),
timestamp: z.coerce.number().int().optional(), timestamp: z.coerce.number().int().optional(),
id: z.string().optional(),
}), }),
}); });
@ -55,7 +55,7 @@ export async function POST(request: Request) {
title, title,
tag, tag,
timestamp, timestamp,
id, id = '',
} = payload; } = payload;
// Cache check // Cache check
@ -99,7 +99,7 @@ export async function POST(request: Request) {
const sessionSalt = hash(startOfMonth(createdAt).toUTCString()); const sessionSalt = hash(startOfMonth(createdAt).toUTCString());
const visitSalt = hash(startOfHour(createdAt).toUTCString()); const visitSalt = hash(startOfHour(createdAt).toUTCString());
const sessionId = uuid(websiteId, ip, userAgent, sessionSalt, id ? id : ''); const sessionId = uuid(websiteId, ip, userAgent, sessionSalt, id);
// Find session // Find session
if (!clickhouse.enabled && !cache?.sessionId) { if (!clickhouse.enabled && !cache?.sessionId) {

View file

@ -233,28 +233,34 @@
} }
}; };
const track = (obj, data) => { const track = (name, data) => {
if (typeof obj === 'string') { if (typeof name === 'string') {
return send({ return send({
...getPayload(), ...getPayload(),
name: obj, name,
data: typeof data === 'object' ? data : undefined, data,
}); });
} else if (typeof obj === 'object') { } else if (typeof name === 'object') {
return send({ ...obj, ...getPayload().id }); return send({ ...name });
} else if (typeof obj === 'function') { } else if (typeof name === 'function') {
return send(obj(getPayload())); return send(name(getPayload()));
} }
return send(getPayload()); return send(getPayload());
}; };
const identify = (data, id = undefined) => { const identify = (id, data) => {
if (id && typeof id === 'string') { if (typeof id === 'string') {
identity = id; identity = id;
} }
/* Clear cache since this will result in another session */
cache = ''; cache = '';
send({ ...getPayload(), data }, 'identify'); return send(
{
...getPayload(),
data: typeof id === 'object' ? id : data,
},
'identify',
);
}; };
/* Start */ /* Start */