Skip to content

Commit

Permalink
Merge pull request #9 from kodster28/patch-2
Browse files Browse the repository at this point in the history
Add deviceId to Zaraz cookie
  • Loading branch information
ad-astra-via authored Apr 24, 2024
2 parents 5dbed19 + 6a1bd1f commit e6d7c6b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ const getUserId = (event: MCEvent) => {
}
return userId
}

// Get the device ID stored in the client, if it does not exist, make a random one, save it in the client, and return it.
const getDeviceId = (event: MCEvent) => {
const { client } = event
let deviceId = event.payload.device_id || client.get('device_id')
if (!deviceId) {
deviceId = crypto.randomUUID()
client.set('device_id', deviceId, { scope: 'infinite' })
}
return deviceId
}

// Get the session ID stored in the client, if it does not exist, make a new one, save it in the client, and return it.

const getSessionId = (event: MCEvent) => {
Expand Down Expand Up @@ -57,7 +69,7 @@ export default async function (manager: Manager, settings: ComponentSettings) {
device_manufacturer: parsedUserAgent.device.vendor,
device_model: parsedUserAgent.device.model,
...(payload.device_id && {
device_id: payload.device_id,
device_id: getDeviceId(event),
}),
...(payload.app_version && {
app_version: payload.app_version,
Expand Down

0 comments on commit e6d7c6b

Please sign in to comment.