tracker: user/device id hardreset method

This commit is contained in:
nick-delirium 2025-03-18 17:41:22 +01:00
parent 529965486c
commit fa2923b83d
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0
3 changed files with 16 additions and 3 deletions

View file

@ -16,6 +16,12 @@ export default class People {
// TODO: fetch endpoint when it will be here
}
// add "hard" flag to force generate device id as well ?
reset = () => {
this.sharedProperties.resetUserId()
this.ownProperties = {}
}
get user_id() {
return this.sharedProperties.user_id
}

View file

@ -98,6 +98,11 @@ export default class SharedProperties {
this.user_id = user_id
}
resetUserId = () => {
this.user_id = null
this.deviceId = this.getDistinctDeviceId(true)
}
public get defaultPropertyKeys() {
return Object.keys(this.all)
}
@ -106,9 +111,9 @@ export default class SharedProperties {
return this.deviceId
}
private getDistinctDeviceId = () => {
private getDistinctDeviceId = (force?: boolean) => {
const potentialStored = this.localStorage.getItem(distinctIdKey)
if (potentialStored) {
if (potentialStored && !force) {
return potentialStored
} else {
const distinctId = `${Math.random().toString(36).slice(2)}-${Math.random().toString(36).slice(2)}-${Math.random().toString(36).slice(2)}`

View file

@ -157,7 +157,9 @@ describe('SharedProperties', () => {
$utm_medium: 'test_medium',
$utm_campaign: 'test_campaign',
$distinct_id: expect.stringMatching(/^[a-z0-9]{6,12}-[a-z0-9]{6,12}-[a-z0-9]{6,12}$/),
$search_engine: null
$search_engine: null,
$user_id: null,
$device_id: expect.stringMatching(/^[a-z0-9]{6,12}-[a-z0-9]{6,12}-[a-z0-9]{6,12}$/),
})
})