Skip to content

Commit e8a933f

Browse files
committed
Updated id logic.
1 parent 340cdce commit e8a933f

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/app/api/send/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const schema = z.object({
1818
payload: z.object({
1919
website: z.string().uuid(),
2020
data: anyObjectParam.optional(),
21-
id: z.string().optional(),
2221
hostname: z.string().max(100).optional(),
2322
language: z.string().max(35).optional(),
2423
referrer: urlOrPathParam.optional(),
@@ -30,6 +29,7 @@ const schema = z.object({
3029
ip: z.string().ip().optional(),
3130
userAgent: z.string().optional(),
3231
timestamp: z.coerce.number().int().optional(),
32+
id: z.string().optional(),
3333
}),
3434
});
3535

@@ -55,7 +55,7 @@ export async function POST(request: Request) {
5555
title,
5656
tag,
5757
timestamp,
58-
id,
58+
id = '',
5959
} = payload;
6060

6161
// Cache check
@@ -99,7 +99,7 @@ export async function POST(request: Request) {
9999
const sessionSalt = hash(startOfMonth(createdAt).toUTCString());
100100
const visitSalt = hash(startOfHour(createdAt).toUTCString());
101101

102-
const sessionId = uuid(websiteId, ip, userAgent, sessionSalt, id ? id : '');
102+
const sessionId = uuid(websiteId, ip, userAgent, sessionSalt, id);
103103

104104
// Find session
105105
if (!clickhouse.enabled && !cache?.sessionId) {

src/tracker/index.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,28 +233,34 @@
233233
}
234234
};
235235

236-
const track = (obj, data) => {
237-
if (typeof obj === 'string') {
236+
const track = (name, data) => {
237+
if (typeof name === 'string') {
238238
return send({
239239
...getPayload(),
240-
name: obj,
241-
data: typeof data === 'object' ? data : undefined,
240+
name,
241+
data,
242242
});
243-
} else if (typeof obj === 'object') {
244-
return send({ ...obj, ...getPayload().id });
245-
} else if (typeof obj === 'function') {
246-
return send(obj(getPayload()));
243+
} else if (typeof name === 'object') {
244+
return send({ ...name });
245+
} else if (typeof name === 'function') {
246+
return send(name(getPayload()));
247247
}
248248
return send(getPayload());
249249
};
250250

251-
const identify = (data, id = undefined) => {
252-
if (id && typeof id === 'string') {
251+
const identify = (id, data) => {
252+
if (typeof id === 'string') {
253253
identity = id;
254254
}
255-
/* Clear cache since this will result in another session */
255+
256256
cache = '';
257-
send({ ...getPayload(), data }, 'identify');
257+
return send(
258+
{
259+
...getPayload(),
260+
data: typeof id === 'object' ? id : data,
261+
},
262+
'identify',
263+
);
258264
};
259265

260266
/* Start */

0 commit comments

Comments
 (0)