From 6a1bd1f42d7a347eccb2177ce2df4d5eed91b942 Mon Sep 17 00:00:00 2001 From: Kody Jackson Date: Fri, 19 Apr 2024 09:53:20 -0500 Subject: [PATCH] Add deviceId to Zaraz cookie 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. --- src/index.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index fd770e8..1a805d6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) => { @@ -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,