Skip to content

Instantly share code, notes, and snippets.

@jlee9595
Last active January 24, 2020 22:09
Show Gist options
  • Save jlee9595/1ede6b77691036c717d31533bc197bfd to your computer and use it in GitHub Desktop.
Save jlee9595/1ede6b77691036c717d31533bc197bfd to your computer and use it in GitHub Desktop.
Segment->Iterable Identify Update
exports.identify = function (identify) {
const traits = formatDates(Object.assign(identify.traits({
phone: 'phoneNumber',
timestamp: 'profileUpdatedAt'
}), { met: identify.created() }))
del(traits, 'id')
const contextWithExtraFields = Object.assign(identify.context(), {
timeZone: identify.timezone(),
userDevice: identify.device()
})
const whitelist = [
'app', 'userDevice', 'ip', 'locale', 'location', 'page', 'timeZone', 'userAgent'
]
const context = formatDates(pick(contextWithExtraFields, whitelist))
const anonymousId = identify.anonymousId()
let email = identify.email()
if (!email && anonymousId) email = anonymousId + '@placeholder.email'
const payload = {
email,
userId: identify.userId(),
dataFields: reject(Object.assign(context, traits)),
}
// if we receive a real email address we need to update the formerly anonymous user's email
if (email && !email.contains('@placeholder.email') && anonymousId) {
const updateEmailPayload = {
currentEmail: anonymousId + '@placeholder.email',
newEmail: payload.email
}
return [updateEmailPayload, payload]
}
return payload
}
@jlee9595
Copy link
Author

jlee9595 commented Dec 14, 2018

This is a method that takes the customer Segment payload as input and outputs either one or two mapped payloads depending on whether updateEmail needs to be called.

Code is unchanged until line 20. At line 20 we now set the email to '[anon id]@placeholder.email.com' if we don't receive an email. Lines 29-34 ensure we populate an additional payload to make an updateEmail call in the case that the user had sent a real email and we have an anonymousId from which to generate the older email address. In all cases a payload for an updateUser call is sent.

Note 1: We don't need to worry about sending updateEmail calls for existing users who currently send us a placeholder email as the criteria for sending an updateEmail call is the lack of '@placeholder.email' and Iterable has required that exact format for placeholder emails.

Note 2: There's a counterpart file to this 'mapping' file that receives the output of this file. In addition to actually sending the API request(s) to Iterable, this file also does the following:

  • Ensures the order we send the updateEmail (if applicable) and updateUser calls follow the order specified in the doc for each case
  • Rejects the payload if there is none of userid, anonymousid, or email included in the payload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment