Last active
July 24, 2023 05:48
-
-
Save jessehanley/25abff692af36c022f127b140c5afd1a to your computer and use it in GitHub Desktop.
Bento Function for Segment
This file contains 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.email, | |
details: event.properties | |
}; | |
// We pluck out the first/last name if present | |
if (event.firstName || event.lastName) { | |
bentoEvent.fields = {}; | |
if (event.firstName) { | |
bentoEvent.fields.first_name = event.firstName; | |
} | |
if (event.lastName) { | |
bentoEvent.fields.last_name = event.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 | |
const bentoSubscriber = event.traits; | |
bentoSubscriber.email = event.email; | |
// 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