From fbee0cb995962656fb2d28f9f7305d5d2d17924a Mon Sep 17 00:00:00 2001 From: Oliver Browne Date: Wed, 23 Oct 2024 11:43:32 +0300 Subject: [PATCH 1/3] fix: alias has user and previous swapped --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f8938c7..4ca18d2 100644 --- a/index.js +++ b/index.js @@ -174,8 +174,8 @@ async function onAlias(event, settings) { ...event, event: '$create_alias', properties: { - alias: event.previousId, - distinct_id: event.userId, + alias: event.userId, + distinct_id: event.previousId, }, }, settings From 0854bcc52fac7a4144a82e8749db0e0a9b1fe14f Mon Sep 17 00:00:00 2001 From: Oliver Browne Date: Wed, 23 Oct 2024 11:46:57 +0300 Subject: [PATCH 2/3] Update test.js --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 102dfc4..66efc9e 100644 --- a/test.js +++ b/test.js @@ -270,8 +270,8 @@ describe('Segment Integration', () => { segment_ip: '8.8.8.8', segment_userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36', - alias: '39239-239239-239239-23923', - distinct_id: '507f191e81', + alias: '507f191e81', + distinct_id: '39239-239239-239239-23923', $lib: 'Segment', }, }) From d205ddc61017c1c3cc4bbacf56aa308396b5b4a1 Mon Sep 17 00:00:00 2001 From: Oliver Browne Date: Wed, 23 Oct 2024 12:00:10 +0300 Subject: [PATCH 3/3] 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', }, }),