Skip to content

Instantly share code, notes, and snippets.

@philfreo
Last active May 25, 2021 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philfreo/34a91468ac0e2efdfbd878006066b067 to your computer and use it in GitHub Desktop.
Save philfreo/34a91468ac0e2efdfbd878006066b067 to your computer and use it in GitHub Desktop.
Segment Integration JS

If you're interested in a Close Segment Destination, please email phil@close.com. We're actively working on a Segment integration and are looking for testers in the near future. If you need something immediately you can try writing code in Functions using the code below as a starting point.

const API_BASE = 'https://api.close.com/api/v1/';
const leadEndpoint = new URL(`${API_BASE}lead/`);
async function apiRequest(settings, endpoint, data, method = 'post') {
return await fetch(`https://api.close.com/api/v1/${endpoint}/`, {
body: JSON.stringify(data),
headers: new Headers({
"Authorization": 'Basic ' + btoa(settings.apiKey),
"Content-Type": "application/json",
}),
method: "post",
});
}
/**
* onIdentify takes an Identify event then POSTs it
*
* @param {SpecIdentify} event The identify event
* @param {Object.<string, any>} settings Custom settings
* @return any
*/
async function onIdentify(event, settings) {
const res = await apiRequest(settings, 'lead', {
"contacts": [
{
"name": event.traits.name,
"title": event.traits.title,
"emails": [
{
"email": event.traits.email,
}
]
}
]
});
return await res.json()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment