Integrate your survey with Segment
IN THIS ARTICLE
1. Why integrate with Segment?
Twilio Segment is a customer data platform (CDP) that helps you collect, clean, and activate your customer data in order for you to send surveys on the right moment and at the right time.
More info: https://segment.com/
2. Set-up
2.a. Sources
Once Segment has been installed, you can start with setting up your source. This can be a lot of different options, but in this example a website has been selected. In order to install Segment the below snippet was copied and pasted in the website HTML.
3.b. Hello Customer destination
When adding Hello Customer as a destination, go to ‘functions’ and click on “+ New Function”
As a next step, set following parameters in the settings:

And copy the matching code piece below for the preferred semantic definitions that should trigger the sending of a survey:
Identify
// Learn more about destination functions API at // https://segment.com/docs/connections/destinations/destination-functions /** * Handle identify event * @param {SegmentIdentifyEvent} event * @param {FunctionSettings} settings */ async function onIdentify(event, settings) { const endpoint = 'https://api.hellocustomer.com/V1.0/EN/Campaign/' + settings.touchpointUniqueId + '/Respondent/Add'; let response; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `basic settings.apiKey`, 'Content-Type': 'application/json' }, body: JSON.stringify(transformEvent(event, settings)) }); } 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}`); } } function transformEvent(event, settings) { var returnValue = { firstName: event.properties.firstname, lastName: event.properties.lastname, email: event.properties.email, custom_UniqueID: '123456789', campaign_UniqueID: '' + settings.touchpointUniqueId + '', language_UniqueID: '1B5C1275-4F92-422F-B1FC-799BF8E30598', keyValues: { team: 'Brugge' //example of metadata } }; return returnValue; }
Track
/** * Handle track event * @param {SegmentTrackEvent} event * @param {FunctionSettings} settings */ async function onTrack(event, settings) { const endpoint = 'https://api.hellocustomer.com/V1.0/EN/Campaign/' + settings.touchpointUniqueId + '/Respondent/Add'; let response; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `basic settings.apiKey`, 'Content-Type': 'application/json' }, body: JSON.stringify(transformEvent(event, settings)) }); } 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}`); } } function transformEvent(event, settings) { var returnValue = { firstName: event.properties.firstname, lastName: event.properties.lastname, email: event.properties.email, custom_UniqueID: '123456789', campaign_UniqueID: '' + settings.touchpointUniqueId + '', language_UniqueID: '1B5C1275-4F92-422F-B1FC-799BF8E30598', keyValues: { team: 'Brugge' //example of metadata } }; return returnValue; }
Page
/** * Handle page event * @param {SegmentPageEvent} event * @param {FunctionSettings} settings */ async function onPage(event, settings) { const endpoint = 'https://api.hellocustomer.com/V1.0/EN/Campaign/' + settings.touchpointUniqueId + '/Respondent/Add'; let response; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `basic settings.apiKey`, 'Content-Type': 'application/json' }, body: JSON.stringify(transformEvent(event, settings)) }); } 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}`); } } function transformEvent(event, settings) { var returnValue = { firstName: event.properties.firstname, lastName: event.properties.lastname, email: event.properties.email, custom_UniqueID: '123456789', campaign_UniqueID: '' + settings.touchpointUniqueId + '', language_UniqueID: '1B5C1275-4F92-422F-B1FC-799BF8E30598', keyValues: { team: 'Brugge' //example of metadata } }; return returnValue; }
Screen
/** * Handle screen event * @param {SegmentScreenEvent} event * @param {FunctionSettings} settings */ async function onScreen(event, settings) { const endpoint = 'https://api.hellocustomer.com/V1.0/EN/Campaign/' + settings.touchpointUniqueId + '/Respondent/Add'; let response; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `basic settings.apiKey`, 'Content-Type': 'application/json' }, body: JSON.stringify(transformEvent(event, settings)) }); } 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}`); } } function transformEvent(event, settings) { var returnValue = { firstName: event.properties.firstname, lastName: event.properties.lastname, email: event.properties.email, custom_UniqueID: '123456789', campaign_UniqueID: '' + settings.touchpointUniqueId + '', language_UniqueID: '1B5C1275-4F92-422F-B1FC-799BF8E30598', keyValues: { team: 'Brugge' //example of metadata } }; return returnValue; }
Group
/** * Handle group event * @param {SegmentGroupEvent} event * @param {FunctionSettings} settings */ async function onGroup(event, settings) { const endpoint = 'https://api.hellocustomer.com/V1.0/EN/Campaign/' + settings.touchpointUniqueId + '/Respondent/Add'; let response; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `basic settings.apiKey`, 'Content-Type': 'application/json' }, body: JSON.stringify(transformEvent(event, settings)) }); } 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}`); } } function transformEvent(event, settings) { var returnValue = { firstName: event.properties.firstname, lastName: event.properties.lastname, email: event.properties.email, custom_UniqueID: '123456789', campaign_UniqueID: '' + settings.touchpointUniqueId + '', language_UniqueID: '1B5C1275-4F92-422F-B1FC-799BF8E30598', keyValues: { team: 'Brugge' //example of metadata } }; return returnValue; }
Alias
/** * Handle alias event * @param {SegmentAliasEvent} event * @param {FunctionSettings} settings */ async function onAlias(event, settings) { const endpoint = 'https://api.hellocustomer.com/V1.0/EN/Campaign/' + settings.touchpointUniqueId + '/Respondent/Add'; let response; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `basic settings.apiKey`, 'Content-Type': 'application/json' }, body: JSON.stringify(transformEvent(event, settings)) }); } 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}`); } } function transformEvent(event, settings) { var returnValue = { firstName: event.properties.firstname, lastName: event.properties.lastname, email: event.properties.email, custom_UniqueID: '123456789', campaign_UniqueID: '' + settings.touchpointUniqueId + '', language_UniqueID: '1B5C1275-4F92-422F-B1FC-799BF8E30598', keyValues: { team: 'Brugge' //example of metadata } }; return returnValue; }
Delete
/** * Handle delete event * @param {SegmentDeleteEvent} event * @param {FunctionSettings} settings */ async function onDelete(event, settings) { const endpoint = 'https://api.hellocustomer.com/V1.0/EN/Campaign/' + settings.touchpointUniqueId + '/Respondent/Add'; let response; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `basic settings.apiKey`, 'Content-Type': 'application/json' }, body: JSON.stringify(transformEvent(event, settings)) }); } 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}`); } } function transformEvent(event, settings) { var returnValue = { firstName: event.properties.firstname, lastName: event.properties.lastname, email: event.properties.email, custom_UniqueID: '123456789', campaign_UniqueID: '' + settings.touchpointUniqueId + '', language_UniqueID: '1B5C1275-4F92-422F-B1FC-799BF8E30598', keyValues: { team: 'Brugge' //example of metadata } }; return returnValue; }