From d205ddc61017c1c3cc4bbacf56aa308396b5b4a1 Mon Sep 17 00:00:00 2001 From: Oliver Browne Date: Wed, 23 Oct 2024 12:00:10 +0300 Subject: [PATCH] fix --- index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4ca18d2..6e438cd 100644 --- a/index.js +++ b/index.js @@ -68,6 +68,16 @@ async function onTrack(event, settings) { event.properties['$groups'] = { segment_group: event.context.groupId } } + // In alias cases, we set the distinct id explicitly and it has to be what's expected. In all other + // cases, we use the userId if it exists, otherwise the anonymousId. + let distinct_id = null; + + if (event.event === '$create_alias') { + distinct_id = event.properties.distinct_id; + } else { + distinct_id = event.userId || event.anonymousId; + } + const res = await fetch(endpoint, { body: JSON.stringify({ uuid, @@ -77,7 +87,7 @@ async function onTrack(event, settings) { properties: { ...parseContext(event.context), ...event.properties, - distinct_id: event.userId || event.anonymousId, + distinct_id: distinct_id, $lib: 'Segment', }, }),