Skip to content

Instantly share code, notes, and snippets.

@juhaelee
Last active November 4, 2020 22:10
Show Gist options
  • Save juhaelee/4b18f27e57c33986c20a9a415cd45283 to your computer and use it in GitHub Desktop.
Save juhaelee/4b18f27e57c33986c20a9a415cd45283 to your computer and use it in GitHub Desktop.
// Handler function, invoked on every identify event
// Argument: event = event payload, settings = function settings, as of Jan 2020 it provides access to API Key value.
// Step 1. Invoke func on identify event
async function onIdentify(event, settings) {
event.traits = event.traits || {};
var email = event.traits.email; // Step 2. Pick up email from 'traits' obj of the incoming event
var user_id = event.userId; // Pick up userId from the incoming event
// Step 3. Make HTTP request to Profile API with the email from previous step
// You can use userId value instead of email as a lookup field. In this case, insert "user_id:${userId_variable_name}" in Profile API URL instead of "email:${email_variable_name}"
// Note '?' parameters. You can omit 'include' parameter to return all traits from the profile. 'Limit' parameter defines how many traits is returned in JSON.
const profileAPIEndpoint = `https://profiles.segment.com/v1/spaces/${settings.personasSpaceId}/collections/users/profiles/email:${email}/traits?include=lastName,company,name&limit=200`; // URL constructor
// HTTP request to Profile API
const req = await fetch(profileAPIEndpoint, {
headers: new Headers({
Authorization: 'Basic ' + btoa(settings.apiKey + ':'), //note 'settings.apikey' - this grabs Personas Access Secret from the 'API Key' field. btoa - base64 encoding
'Content-Type': 'application/json'
}),
method: 'get'
});
const user = await req.json(); // JSON, returned by Profile API
// Step 4. Grab 'lastname' and 'company' traits
var lastname =
user.traits.lastName || user.traits.name.split(' ').splice(-1)[0]; // If last name is not in the traits, grab last word from 'name' trait
var company = user.traits.company || 'n/a';
// Merge lastname and company into a single object
var ln_comp = {
lastname: lastname,
company: company
};
// Step 5. Create new event sans writekey, timestamp, messageID, which will be added automatically by HTTP API source
var traits = _.merge(ln_comp, event.traits); // Create new traits object with lodash library module _.merge
var http_api_event = {
type: 'identify',
userId: user_id,
traits: {...traits, "segmentid": user_id},
integrations: {
Salesforce: true
}
}; // New identify() event payload
console.log(JSON.stringify(http_api_event));
// Step 6. Send new event to Segment API, i.e. emit the event from HTTP API source
const res = await fetch('https://api.segment.io/v1/identify', {
body: JSON.stringify(http_api_event),
headers: new Headers({
Authorization: 'Basic ' + btoa(settings.segmentWriteKey + ':'),
'Content-Type': 'application/json'
}),
method: 'post'
});
return await res.json();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment