Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Nils-van-Kleef/eefb64813c17e5258f12ea8ea5855c19 to your computer and use it in GitHub Desktop.
Save Nils-van-Kleef/eefb64813c17e5258f12ea8ea5855c19 to your computer and use it in GitHub Desktop.
Server-side only: Create users and track events in Crisp
// Learn more about destination functions API at
// https://segment.com/docs/connections/destinations/destination-functions
// Crisp API documentation: https://docs.crisp.chat/api/v1/
// !! Add your website_id here
const website_id = '9a2518c0-913f-46e6-acfe-3770cda57186';
var endpoint =
'https://api.crisp.chat/v1/website/' + website_id + '/people/profile';
// Replace identifier with your identifier
const identifier =
'4855a3bf-99d1-48a3-be39-6014c3ce8f9c';
/**
* Handle track event
* @param {SegmentTrackEvent} event
* @param {FunctionSettings} settings
*/
async function onTrack(event, settings) {
// Learn more at https://segment.com/docs/connections/spec/track/
var body = '';
if (event.event === "New Conversation") { // Create a new branch in this if-else statement for each event that you'd like to capture
endpoint =
'https://api.crisp.chat/v1/website/' +
website_id +
'conversation';
var method = 'POST';
var data = {
data : {
session_id: (event.properties.session_id || Math.floor(Math.random() * 1000000))
}
}
body = JSON.stringify(data);
await sendCall(event, settings, endpoint, body, method);
}
}
/**
* Handle identify event
* @param {SegmentIdentifyEvent} event
* @param {FunctionSettings} settings
*/
async function onIdentify(event, settings) {
// Learn more at https://segment.com/docs/connections/spec/identify/
if (event.properties && typeof event.properties.email) {
var email_exists = false;
var people_id = '';
var body = '';
var email = event.properties.email;
// Query the Crisp API endpoint to get a list of profiles that resemeble the email address
endpoint =
'https://api.crisp.chat/v1/website/' +
website_id +
'/people/profiles?search_text=' +
email;
var method = 'GET';
var crisp_response = await sendCall(event, settings, endpoint, body, method);
// Run over the response to check if the email already exists in Crisp
var i = 0;
while (!email_exists && i < crisp_response.data.length) {
if (crisp_response.data[i].email == email) {
email_exists = true;
people_id = crisp_response.data[i].people_id;
}
i++;
}
// Construct identity
var identity = {
email: event.properties.email,
person: {
nickname: (event.properties.name ? event.properties.name : '')
}
};
body = JSON.stringify(identity);
if (!email_exists) {
// SEND NORMAL IDENTIFY CALL
endpoint =
'https://api.crisp.chat/v1/website/' + website_id + '/people/profile';
method = 'POST';
await sendCall(event, settings, endpoint, body, method);
} else {
// SEND UPDATE IDENTIFY CALL
method = 'PATCH';
endpoint =
'https://api.crisp.chat/v1/website/' +
website_id +
'/people/profile/' +
people_id;
await sendCall(event, settings, endpoint, body, method);
}
}
}
async function sendCall(event, settings, endpoint, body, method) {
const request = {
method: method,
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(identifier + ':' + settings.apiKey)
}
};
if (method != 'GET') {
request.body = body != '' ? body : '';
}
const response = await fetch(endpoint, request);
var crisp_response = await response.json();
return crisp_response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment