Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Use props passed to identify to define session

perso1822c80aa0b2479
diff --git a/src/app/api/send/route.ts b/src/app/api/send/route.ts
index bd255eaf8..c520a02b2 100644
--- a/src/app/api/send/route.ts
+++ b/src/app/api/send/route.ts
@@ -18,6 +18,7 @@ const schema = z.object({
payload: z.object({
website: z.string().uuid(),
data: anyObjectParam.optional(),
+ identity: anyObjectParam.optional(),
hostname: z.string().max(100).optional(),
language: z.string().max(35).optional(),
referrer: urlOrPathParam.optional(),
@@ -59,6 +60,7 @@ export async function POST(request: Request) {
title,
tag,
timestamp,
+ identity,
} = payload;
// Cache check
@@ -97,7 +99,13 @@ export async function POST(request: Request) {
const sessionSalt = hash(startOfMonth(createdAt).toUTCString());
const visitSalt = hash(startOfHour(createdAt).toUTCString());
- const sessionId = uuid(websiteId, ip, userAgent, sessionSalt);
+ const sessionId = uuid(
+ websiteId,
+ ip,
+ userAgent,
+ sessionSalt,
+ identity ? JSON.stringify(identity) : '',
+ );
// Find session
if (!clickhouse.enabled && !cache?.sessionId) {
diff --git a/src/tracker/index.js b/src/tracker/index.js
index c423a66b5..2f0fe8267 100644
--- a/src/tracker/index.js
+++ b/src/tracker/index.js
@@ -233,21 +233,28 @@
};
const track = (obj, data) => {
+ const identity = JSON.parse(localStorage.getItem('umami.identity'));
if (typeof obj === 'string') {
return send({
...getPayload(),
name: obj,
data: typeof data === 'object' ? data : undefined,
+ identity: identity !== null ? identity : undefined,
});
} else if (typeof obj === 'object') {
- return send(obj);
+ return send({ ...obj, identity: identity !== null ? identity : undefined });
} else if (typeof obj === 'function') {
- return send(obj(getPayload()));
+ return send({ ...obj(getPayload()), identity: identity !== null ? identity : undefined });
}
- return send(getPayload());
+ return send({ ...getPayload(), identity: identity !== null ? identity : undefined });
};
- const identify = data => send({ ...getPayload(), data }, 'identify');
+ const identify = data => {
+ localStorage.setItem('umami.identity', JSON.stringify(data));
+ /* Clear cache since this will result in another session */
+ cache = '';
+ send({ ...getPayload(), data, identity: data }, 'identify');
+ };
/* Start */