Skip to content

Commit

Permalink
Add deviceId to Zaraz cookie
Browse files Browse the repository at this point in the history
Based on [how Amplitude merges data](https://help.amplitude.com/hc/en-us/articles/115003135607-Track-unique-users), deviceID should be added to the cookie value to help with cross-domain tracking.
  • Loading branch information
kodster28 authored Apr 19, 2024
1 parent 5dbed19 commit 6a1bd1f
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 6a1bd1f

Please sign in to comment.