Added id as a parameter to identify and include it in the payload. Removed the usage of localstorage

This commit is contained in:
perso182 2025-04-01 11:24:06 +02:00
parent 224961447c
commit 37ae0374d8
2 changed files with 13 additions and 23 deletions

View file

@ -18,7 +18,7 @@ const schema = z.object({
payload: z.object({ payload: z.object({
website: z.string().uuid(), website: z.string().uuid(),
data: anyObjectParam.optional(), data: anyObjectParam.optional(),
identity: 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(),
@ -60,7 +60,7 @@ export async function POST(request: Request) {
title, title,
tag, tag,
timestamp, timestamp,
identity, id,
} = payload; } = payload;
// Cache check // Cache check
@ -99,13 +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( const sessionId = uuid(websiteId, ip, userAgent, sessionSalt, id ? id : '');
websiteId,
ip,
userAgent,
sessionSalt,
identity ? JSON.stringify(identity) : '',
);
// Find session // Find session
if (!clickhouse.enabled && !cache?.sessionId) { if (!clickhouse.enabled && !cache?.sessionId) {

View file

@ -46,6 +46,7 @@
url: currentUrl, url: currentUrl,
referrer: currentRef, referrer: currentRef,
tag: tag ? tag : undefined, tag: tag ? tag : undefined,
id: identity ? identity : undefined,
}); });
const hasDoNotTrack = () => { const hasDoNotTrack = () => {
@ -233,33 +234,27 @@
}; };
const track = (obj, data) => { const track = (obj, data) => {
let identity;
try {
const parsedIdentity = JSON.parse(localStorage.getItem('umami.identity'));
identity = parsedIdentity !== null ? parsedIdentity : undefined;
} catch (error) {
identity = undefined;
}
if (typeof obj === 'string') { if (typeof obj === 'string') {
return send({ return send({
...getPayload(), ...getPayload(),
name: obj, name: obj,
data: typeof data === 'object' ? data : undefined, data: typeof data === 'object' ? data : undefined,
identity,
}); });
} else if (typeof obj === 'object') { } else if (typeof obj === 'object') {
return send({ ...obj, identity }); return send({ ...obj, ...getPayload().id });
} else if (typeof obj === 'function') { } else if (typeof obj === 'function') {
return send({ ...obj(getPayload()), identity }); return send(obj(getPayload()));
} }
return send({ ...getPayload(), identity }); return send(getPayload());
}; };
const identify = data => { const identify = (data, id = undefined) => {
localStorage.setItem('umami.identity', JSON.stringify(data)); if (id) {
identity = id;
}
/* Clear cache since this will result in another session */ /* Clear cache since this will result in another session */
cache = ''; cache = '';
send({ ...getPayload(), data, identity: data }, 'identify'); send({ ...getPayload(), data }, 'identify');
}; };
/* Start */ /* Start */
@ -277,6 +272,7 @@
let cache; let cache;
let initialized; let initialized;
let disabled = false; let disabled = false;
let identity;
if (autoTrack && !trackingDisabled()) { if (autoTrack && !trackingDisabled()) {
if (document.readyState === 'complete') { if (document.readyState === 'complete') {