From 53cf91bb16f20e0385c6238edd067757cb5ac248 Mon Sep 17 00:00:00 2001 From: nick-delirium Date: Wed, 19 Mar 2025 16:44:02 +0100 Subject: [PATCH] tracker: more connectors for tracker and sdk --- tracker/tracker/src/main/entry.ts | 1 + tracker/tracker/src/main/index.ts | 6 ++++++ tracker/tracker/src/main/modules/analytics/events.ts | 3 +++ tracker/tracker/src/main/modules/analytics/index.ts | 12 +++++++++--- tracker/tracker/src/main/modules/analytics/people.ts | 7 +++++-- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tracker/tracker/src/main/entry.ts b/tracker/tracker/src/main/entry.ts index 27113b1bd..b6edf09a7 100644 --- a/tracker/tracker/src/main/entry.ts +++ b/tracker/tracker/src/main/entry.ts @@ -3,4 +3,5 @@ import TrackerClass from './index' export { default as App } from './app/index' export { default as tracker, default as openReplay } from './singleton' export { SanitizeLevel, Messages, Options } from './index' +export { default as Analytics } from './modules/analytics/index' export default TrackerClass diff --git a/tracker/tracker/src/main/index.ts b/tracker/tracker/src/main/index.ts index 765890123..26c0f2fc2 100644 --- a/tracker/tracker/src/main/index.ts +++ b/tracker/tracker/src/main/index.ts @@ -184,6 +184,8 @@ export default class API { options.localStorage ?? localStorage, options.sessionStorage ?? sessionStorage, this.getAnalyticsToken, + this.app?.timestamp ?? Date.now, + this.setUserID, ) this.app = app if (!this.crossdomainMode) { @@ -324,6 +326,9 @@ export default class API { if (this.app === null) { return Promise.reject("Browser doesn't support required api, or doNotTrack is active.") } + if (startOpts?.userID) { + this.analytics?.people.identify(startOpts.userID, { fromTracker: true }) + } return this.app.start(startOpts) } else { return Promise.reject('Trying to start not in browser.') @@ -459,6 +464,7 @@ export default class API { setUserID(id: string): void { if (typeof id === 'string' && this.app !== null) { this.app.session.setUserID(id) + this.analytics?.people.identify(id, { fromTracker: true }) } } diff --git a/tracker/tracker/src/main/modules/analytics/events.ts b/tracker/tracker/src/main/modules/analytics/events.ts index 42e7434c9..e6050c44a 100644 --- a/tracker/tracker/src/main/modules/analytics/events.ts +++ b/tracker/tracker/src/main/modules/analytics/events.ts @@ -1,6 +1,9 @@ import SharedProperties from './sharedProperties.js' import { isObject } from './utils.js' +const maxProperties = 100; +const maxPropLength = 100; + export default class Events { queue: Record = [] sendInterval: ReturnType | null = null diff --git a/tracker/tracker/src/main/modules/analytics/index.ts b/tracker/tracker/src/main/modules/analytics/index.ts index de7117f50..6f91cb3b1 100644 --- a/tracker/tracker/src/main/modules/analytics/index.ts +++ b/tracker/tracker/src/main/modules/analytics/index.ts @@ -15,15 +15,21 @@ export default class Analytics { * @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 + * @param getToken Function that returns token to bind events to a session + * + * @param getTimestamp returns current timestamp + * + * @param setUserId callback for people.identify * */ constructor( private readonly localStorage: StorageLike, private readonly sessionStorage: StorageLike, private readonly getToken: () => string, + private readonly getTimestamp: () => number, + private readonly setUserId: (user_id: string) => void, ) { this.sharedProperties = new SharedProperties(localStorage, sessionStorage) - this.events = new Events(this.sharedProperties, getToken, Date.now) - this.people = new People(this.sharedProperties, getToken, Date.now) + this.events = new Events(this.sharedProperties, getToken, getTimestamp) + this.people = new People(this.sharedProperties, getToken, getTimestamp, setUserId) } } diff --git a/tracker/tracker/src/main/modules/analytics/people.ts b/tracker/tracker/src/main/modules/analytics/people.ts index ec134b2f5..d95f7e4e8 100644 --- a/tracker/tracker/src/main/modules/analytics/people.ts +++ b/tracker/tracker/src/main/modules/analytics/people.ts @@ -8,11 +8,14 @@ export default class People { private readonly sharedProperties: SharedProperties, private readonly getToken: () => string, private readonly getTimestamp: () => number, + private readonly onId: (user_id: string) => void, ) {} - identify = (user_id: string) => { + identify = (user_id: string, options?: { fromTracker: boolean }) => { this.sharedProperties.setUserId(user_id) - + if (!options?.fromTracker) { + this.onId(user_id) + } // TODO: fetch endpoint when it will be here }