Last active
September 29, 2023 16:51
-
-
Save jaygilmore/cc6e17e3cb6c532f01a95105e182ba20 to your computer and use it in GitHub Desktop.
Bento Segment Function
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
// Learn more about destination functions API at | |
// https://segment.com/docs/connections/destinations/destination-functions | |
/** | |
* Handle track event | |
* @param {SegmentTrackEvent} event | |
* @param {FunctionSettings} settings | |
*/ | |
async function onTrack(event, settings) { | |
// Learn more at https://segment.com/docs/connections/spec/track/ | |
const endpoint = `https://app.bentonow.com/api/v1/batch/events?site_uuid=${settings.siteUuid}`; // replace with your endpoint | |
let response; | |
// We reshape the event | |
const bentoEvent = { | |
type: event.event, | |
email: event.properties.email, | |
details: event.properties | |
}; | |
// We pluck out the first/last name if present | |
if (event.properties.firstName || event.properties.lastName) { | |
bentoEvent.fields = {}; | |
if (event.properties.firstName) { | |
bentoEvent.fields.first_name = event.properties.firstName; | |
} | |
if (event.properties.lastName) { | |
bentoEvent.fields.last_name = event.properties.lastName; | |
} | |
} | |
try { | |
response = await fetch(endpoint, { | |
method: 'POST', | |
headers: { | |
Authorization: `Basic ${btoa( | |
settings.publishableKey + ':' + settings.secretKey | |
)}`, | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ events: [bentoEvent] }) | |
}); | |
} catch (error) { | |
// Retry on connection error | |
throw new RetryError(error.message); | |
} | |
if (response.status >= 500 || response.status === 429) { | |
// Retry on 5xx (server errors) and 429s (rate limits) | |
throw new RetryError(`Failed with ${response.status}`); | |
} | |
} | |
/** | |
* Handle identify event | |
* @param {SegmentIdentifyEvent} event | |
* @param {FunctionSettings} settings | |
*/ | |
async function onIdentify(event, settings) { | |
// Learn more at https://segment.com/docs/connections/spec/identify/ | |
const endpoint = `https://app.bentonow.com/api/v1/batch/subscribers?site_uuid=${settings.siteUuid}`; // replace with your endpoint | |
let response; | |
// We reshape the event | |
let bentoSubscriber = { | |
email: event.traits.email | |
}; | |
// We pluck out the first/last name if present | |
if (event.traits) { | |
// spread bentoSubscriber last to use the email provided by Segment | |
bentoSubscriber = { ...event.traits, ...bentoSubscriber }; | |
} | |
// if email is present, we submit else we skip | |
if (bentoSubscriber.email === undefined) { | |
return; | |
} else { | |
try { | |
response = await fetch(endpoint, { | |
method: 'POST', | |
headers: { | |
Authorization: `Basic ${btoa( | |
settings.publishableKey + ':' + settings.secretKey | |
)}`, | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ subscribers: [bentoSubscriber] }) | |
}); | |
} catch (error) { | |
// Retry on connection error | |
throw new RetryError(error.message); | |
} | |
if (response.status >= 500 || response.status === 429) { | |
// Retry on 5xx (server errors) and 429s (rate limits) | |
throw new RetryError(`Failed with ${response.status}`); | |
} | |
} | |
} | |
/** | |
* Handle group event | |
* @param {SegmentGroupEvent} event | |
* @param {FunctionSettings} settings | |
*/ | |
async function onGroup(event, settings) { | |
// Learn more at https://segment.com/docs/connections/spec/group/ | |
throw new EventNotSupported('group is not supported'); | |
} | |
/** | |
* Handle page event | |
* @param {SegmentPageEvent} event | |
* @param {FunctionSettings} settings | |
*/ | |
async function onPage(event, settings) { | |
// Learn more at https://segment.com/docs/connections/spec/page/ | |
throw new EventNotSupported('page is not supported'); | |
} | |
/** | |
* Handle screen event | |
* @param {SegmentScreenEvent} event | |
* @param {FunctionSettings} settings | |
*/ | |
async function onScreen(event, settings) { | |
// Learn more at https://segment.com/docs/connections/spec/screen/ | |
throw new EventNotSupported('screen is not supported'); | |
} | |
/** | |
* Handle alias event | |
* @param {SegmentAliasEvent} event | |
* @param {FunctionSettings} settings | |
*/ | |
async function onAlias(event, settings) { | |
// Learn more at https://segment.com/docs/connections/spec/alias/ | |
throw new EventNotSupported('alias is not supported'); | |
} | |
/** | |
* Handle delete event | |
* @param {SegmentDeleteEvent} event | |
* @param {FunctionSettings} settings | |
*/ | |
async function onDelete(event, settings) { | |
// Learn more at https://segment.com/docs/partners/spec/#delete | |
throw new EventNotSupported('delete is not supported'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment