From bbe65d548ceb784c94657052aac1e96b94510b39 Mon Sep 17 00:00:00 2001 From: ShiCheng Lu Date: Mon, 18 Nov 2024 15:02:11 -0500 Subject: [PATCH 1/3] save user._id to storage after track, and use in conversion event --- src/api/conversions.ts | 19 ++++++++++++++++--- src/api/track.ts | 7 +++++++ src/storage.ts | 11 +++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/api/conversions.ts b/src/api/conversions.ts index 60d10803..e5bacf96 100644 --- a/src/api/conversions.ts +++ b/src/api/conversions.ts @@ -10,17 +10,30 @@ class ConversionsAPI { const options = Config.get(); const name = params.name; - const userId = params.userId || Storage.getItem(Storage.USER_ID); - const deviceId = params.deviceId || Device.getDeviceId(); - const installId = params.installId || Device.getInstallId(); const metadata = params.metadata || {}; const createdAt = params.createdAt; + // we should only set id if none of userId/deviceId/installId are passed in, or any of them match existing one + const storedDeviceId = Device.getDeviceId(); + const storedUserId = Storage.getItem(Storage.USER_ID); + const storedInstallId = Device.getInstallId(); + let id; + if ((!params.deviceId || params.deviceId === storedDeviceId) && + (!params.userId || params.userId === storedUserId) && + (!params.installId || params.installId === storedInstallId)) { + id = Storage.getItem(Storage.ID); + } + + const userId = params.userId || storedUserId; + const deviceId = params.deviceId || storedDeviceId; + const installId = params.installId || storedInstallId; + if (params.revenue) { metadata.revenue = params.revenue; } const data: any = { + id, name, userId, deviceId, diff --git a/src/api/track.ts b/src/api/track.ts index 3862a44a..c653f6bf 100644 --- a/src/api/track.ts +++ b/src/api/track.ts @@ -182,6 +182,10 @@ class TrackAPI { _id, } as RadarTrackVerifiedResponse; + if (user) { + Storage.setItem(Storage.ID, user._id); + } + if (options.debug) { trackRes.response = response; } @@ -189,6 +193,7 @@ class TrackAPI { return trackRes; } + // unverified track response = await Http.request({ method: 'POST', path: 'track', @@ -204,6 +209,8 @@ class TrackAPI { location, } as RadarTrackResponse; + Storage.setItem(Storage.ID, user?._id); + if (options.debug) { trackRes.response = response; } diff --git a/src/storage.ts b/src/storage.ts index 1555162a..27798a37 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -3,6 +3,9 @@ import Logger from './logger'; class Storage { // local storage key definitions for identifying track users + public static get ID() { + return 'radar-id'; + } public static get USER_ID() { return 'radar-userId'; } @@ -41,6 +44,14 @@ class Storage { if (!storage) { return; } + // clear id if user_id doesn't match previous + if (key == this.USER_ID) { + const previousUserId = this.getItem(this.USER_ID); + if (previousUserId !== value) { + storage.removeItem(this.ID); + console.log("User id mismatch, removing ID"); + } + } if (value === undefined || value === null) { return; } From e4addfacd99efa0019ec1f004ee7cc94e5f7d91c Mon Sep 17 00:00:00 2001 From: ShiCheng Lu Date: Mon, 18 Nov 2024 15:34:57 -0500 Subject: [PATCH 2/3] add id field if it exists regardless of conversion param --- src/api/conversions.ts | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/api/conversions.ts b/src/api/conversions.ts index e5bacf96..12b63410 100644 --- a/src/api/conversions.ts +++ b/src/api/conversions.ts @@ -12,21 +12,11 @@ class ConversionsAPI { const name = params.name; const metadata = params.metadata || {}; const createdAt = params.createdAt; - // we should only set id if none of userId/deviceId/installId are passed in, or any of them match existing one - const storedDeviceId = Device.getDeviceId(); - const storedUserId = Storage.getItem(Storage.USER_ID); - const storedInstallId = Device.getInstallId(); - let id; - if ((!params.deviceId || params.deviceId === storedDeviceId) && - (!params.userId || params.userId === storedUserId) && - (!params.installId || params.installId === storedInstallId)) { - id = Storage.getItem(Storage.ID); - } - - const userId = params.userId || storedUserId; - const deviceId = params.deviceId || storedDeviceId; - const installId = params.installId || storedInstallId; + const id = Storage.getItem(Storage.ID); + const userId = params.userId || Storage.getItem(Storage.USER_ID); + const deviceId = params.deviceId || Device.getDeviceId(); + const installId = params.installId || Device.getInstallId(); if (params.revenue) { metadata.revenue = params.revenue; From 49c363f9dbea50e28d26b5a9f90f00cc7cbc2a7d Mon Sep 17 00:00:00 2001 From: ShiCheng Lu Date: Mon, 18 Nov 2024 17:04:26 -0500 Subject: [PATCH 3/3] also send id with getConfig and track --- src/api/config.ts | 5 ++++- src/api/track.ts | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/api/config.ts b/src/api/config.ts index 5a3841e7..d544b176 100644 --- a/src/api/config.ts +++ b/src/api/config.ts @@ -4,6 +4,7 @@ import Http from '../http'; import Logger from '../logger'; import Session from '../session'; import Navigator from '../navigator'; +import Storage from '../storage'; import type { RadarTrackParams } from '../types'; @@ -16,12 +17,14 @@ class ConfigAPI { return; } + const id = Storage.getItem(Storage.ID) || undefined; const deviceId = params.deviceId || Device.getDeviceId(); const installId = params.installId || Device.getInstallId(); const sessionId = Session.getSessionId(); const locationAuthorization = await Navigator.getPermissionStatus(); - const data = { + const data: any = { + id, deviceId, installId, sessionId, diff --git a/src/api/track.ts b/src/api/track.ts index c653f6bf..4970d6b9 100644 --- a/src/api/track.ts +++ b/src/api/track.ts @@ -36,6 +36,7 @@ class TrackAPI { } // user indentification fields + const id = Storage.getItem(Storage.ID) || undefined; // `undefined` so it's removed from any fields const userId = params.userId || Storage.getItem(Storage.USER_ID); const deviceId = params.deviceId || Device.getDeviceId(); const installId = params.installId || Device.getInstallId(); @@ -84,6 +85,7 @@ class TrackAPI { userId, tripOptions, timeZone, + id, }; let response: any; @@ -99,6 +101,7 @@ class TrackAPI { method: 'GET', path: 'config', data: { + id, deviceId, installId, sessionId,