From 529965486c9641176f10b3e3b1571fce08a27401 Mon Sep 17 00:00:00 2001 From: nick-delirium Date: Tue, 18 Mar 2025 17:20:35 +0100 Subject: [PATCH] tracker: move userid to common props, use email/device as dist id --- tracker/tracker/src/main/modules/analytics/index.ts | 9 +++++++++ tracker/tracker/src/main/modules/analytics/people.ts | 9 ++++++--- .../src/main/modules/analytics/sharedProperties.ts | 9 ++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/tracker/tracker/src/main/modules/analytics/index.ts b/tracker/tracker/src/main/modules/analytics/index.ts index b852332eb..de7117f50 100644 --- a/tracker/tracker/src/main/modules/analytics/index.ts +++ b/tracker/tracker/src/main/modules/analytics/index.ts @@ -8,6 +8,15 @@ export default class Analytics { public readonly sharedProperties: SharedProperties public readonly people: People + /** + * @param localStorage Class or Object that implements Storage-like interface that stores + * values persistently like window.localStorage or any other file-based storage + * + * @param sessionStorage Class or Object that implements Storage-like interface that stores values + * on per-session basis like window.sessionStorage or any other in-memory storage + * + * @param getToken Function that returns token to bind events to a se + * */ constructor( private readonly localStorage: StorageLike, private readonly sessionStorage: StorageLike, diff --git a/tracker/tracker/src/main/modules/analytics/people.ts b/tracker/tracker/src/main/modules/analytics/people.ts index e9c03303f..761ef2ff6 100644 --- a/tracker/tracker/src/main/modules/analytics/people.ts +++ b/tracker/tracker/src/main/modules/analytics/people.ts @@ -3,7 +3,6 @@ import { isObject } from './utils.js' export default class People { ownProperties: Record = {} - user_id: string | null = null constructor( private readonly sharedProperties: SharedProperties, @@ -12,14 +11,18 @@ export default class People { ) {} identify = (user_id: string) => { - this.user_id = user_id + this.sharedProperties.setUserId(user_id) // TODO: fetch endpoint when it will be here } + get user_id() { + return this.sharedProperties.user_id + } + // TODO: what exactly we're removing here besides properties and id? deleteUser = () => { - this.user_id = null + this.sharedProperties.setUserId(null) this.ownProperties = {} // TODO: fetch endpoint when it will be here diff --git a/tracker/tracker/src/main/modules/analytics/sharedProperties.ts b/tracker/tracker/src/main/modules/analytics/sharedProperties.ts index 6347efa56..82b362a9f 100644 --- a/tracker/tracker/src/main/modules/analytics/sharedProperties.ts +++ b/tracker/tracker/src/main/modules/analytics/sharedProperties.ts @@ -47,6 +47,7 @@ export default class SharedProperties { utmCampaign: string | null deviceId: string searchEngine: string | null + user_id: string | null = null constructor( private readonly localStorage: StorageLike, @@ -83,7 +84,9 @@ export default class SharedProperties { [`${prefix}utm_source`]: this.utmSource, [`${prefix}utm_medium`]: this.utmMedium, [`${prefix}utm_campaign`]: this.utmCampaign, - [`${prefix}distinct_id`]: this.deviceId, + [`${prefix}device_id`]: this.deviceId, + [`${prefix}user_id`]: this.user_id, + [`${prefix}distinct_id`]: this.user_id ?? this.deviceId, [`${prefix}sdk_edition`]: 'web', [`${prefix}sdk_version`]: 'TRACKER_VERSION', [`${prefix}timezone`]: getUTCOffsetString(), @@ -91,6 +94,10 @@ export default class SharedProperties { } } + setUserId = (user_id: string | null) => { + this.user_id = user_id + } + public get defaultPropertyKeys() { return Object.keys(this.all) }