Skip to content

Instantly share code, notes, and snippets.

@mmcspiritt
Created May 15, 2020 20:58
Show Gist options
  • Save mmcspiritt/9ffbd18e29efb33fbf536dffd5cf259f to your computer and use it in GitHub Desktop.
Save mmcspiritt/9ffbd18e29efb33fbf536dffd5cf259f to your computer and use it in GitHub Desktop.
// Learn more about source functions API at
// https://segment.com/docs/connections/sources/source-functions
/**
* Handle incoming HTTP request
*
* @param {FunctionRequest} request
* @param {FunctionSettings} settings
*****Sample Payload*****
{
"Events": [
{
"CustomFields": [
{
"Key": "website",
"Value": "http://example.org"
}
],
"Date": "2010-12-14 11:32:00",
"EmailAddress": "test@example.org",
"Name": "Test Subscriber",
"SignupIPAddress": "53.78.123.243",
"Type": "Subscribe"
}
],
"ListID": "96c0bbdaa54760c8d9e62a2b7ffa2e13"
}
*/
async function onRequest(request, settings) {
const body = request.json();
(body.Events || []).map((event) => {
// check if there's a subscriber as part of the payload
if (event.Type == "Subscribe" && event.EmailAddress) {
// See https://segment.com/docs/connections/spec/identify/
Segment.identify({
userId: event.EmailAddress,
traits: {
email: event.EmailAddress,
campaign_monitor_subscribe_name: event.Name,
campaign_monitor_list_name: event.ListID,
campaign_monitor_ip: SignupIPAddress,
},
});
}
if (event.Type == "Deactivate" && event.EmailAddress) {
// See https://segment.com/docs/connections/spec/identify/
Segment.track({
event: "Unsubscribed",
userId: event.EmailAddress,
properties: {
email: event.EmailAddress,
campaign_monitor_list_name: event.ListID,
},
});
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment