Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
},
}),
Expand Down Expand Up @@ -174,8 +184,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
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
})
Expand Down
Loading