tracker: more connectors for tracker and sdk

This commit is contained in:
nick-delirium 2025-03-19 16:44:02 +01:00
parent fa2923b83d
commit 53cf91bb16
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0
5 changed files with 24 additions and 5 deletions

View file

@ -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

View file

@ -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 })
}
}

View file

@ -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<string, any> = []
sendInterval: ReturnType<typeof setInterval> | null = null

View file

@ -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)
}
}

View file

@ -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
}