Last active
January 24, 2020 22:09
-
-
Save jlee9595/1ede6b77691036c717d31533bc197bfd to your computer and use it in GitHub Desktop.
Segment->Iterable Identify Update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 anupdateUser
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 anupdateEmail
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:
updateEmail
(if applicable) andupdateUser
calls follow the order specified in the doc for each case