All Collections
Integrations
Segment userId syncing to Featurebase on .identify()
Segment userId syncing to Featurebase on .identify()

This article goes through the process of syncing your segment userids into Featurebase to allow tracking your users better.

Robi Rohumaa avatar
Written by Robi Rohumaa
Updated over a week ago

To start, go to Connections โ†’ Destinations within the Segment dashboard and click on "Add destination"

Select the 'Functions' tab and click on 'Create Function'

Pick "Destination" and click on 'Build'

Delete all of the code inside the function and replace it with this:

/**
* Map the incoming event traits to Featurebase identify fields. This let's you see the users data in the Users tab (dashboard) and on each post they create.
* If you do not wish to send along this data, add a "return null;" to the beginning of it.
* This function is intended to be customized by you based on your specific event.traits structure.
* You should map their traits to the corresponding fields in the featurebaseIdentify object.
*
* You are advised to modify this function to suit your needs. If your traits object has different field names, adjust the return object accordingly.
*
* Feel free to remove any optional fields if you do not have them in your incoming data.
*
* @param {Object} event - The event object from the incoming request
* @return {Object | null} - The mapped featurebaseIdentify object, or null if mandatory fields are not present in the traits
*/
function mapFeaturebaseIdentify(event) {
/**
* Example `featurebaseIdentify` object:
*
* let featurebaseIdentify = {
* "email": "youruser@example.com", // User's email address (required)
* "name": "John Steezy", // User's name (required)
* "id": "123456789", // User's ID (required)
* "profilePicture": "https://example.com/profile.jpg", // URL to the user's profile picture (optional)
* "createdAt": "2023-05-19T15:35:49.915Z", // Date and time when the user was created (optional)
* "customFields": { // Additional information about the user. Key-value pairs of type 'string' allowed. (optional)
* "title": "Product Manager",
* "plan": "Premium"
* },
* "companies": [ // List of companies associated with the user (optional)
* {
* "id": "987654321", // Company's ID (required)
* "name": "Business Inc.", // Company's name (required)
* "monthlySpend": 500, // Company's monthly spend (optional)
* "createdAt": "2023-05-19T15:35:49.915Z", // Date and time when the company was created (optional)
* "customFields": { // Additional information about the company. Key-value pairs of type 'string' allowed. (optional)
* "location": "Canada",
* "language": "French"
* }
* }
* ]
* }
*/

/* This is the part you need to customize */
let featurebaseIdentify = {
name: event.traits.name,
email: event.traits.email,
id: event.userId,
profilePicture: event.traits.profilePicture || null,
createdAt: event.traits.createdAt || null,
customFields: event.traits.customFields || {},
companies: event.traits.companies || []
};

let mandatoryFields = ['name', 'email', 'id'];
let isValid = mandatoryFields.every(field => featurebaseIdentify[field]);

if (!isValid) {
console.error(
'You did not provide all the mandatory fields. Please provide id, name and email to identify your users in Featurebase. Skipping identification.'
);
}
// Return the featurebaseIdentify object if all mandatory fields are present, else return null
return isValid ? featurebaseIdentify : null;
}

/**
* Handle identify event for Featurebase.
*
* !!! IMPORTANT !!!
* Make sure to replace YOURORGNAME with your own organizations name.
*
* @param {SegmentIdentifyEvent} event
* @param {FunctionSettings} settings
*/
async function onIdentify(event, settings) {
const endpoint =
'https://robiorg.featurebase.app/api/v1/webhook/segment/identify';
let response;

// Map event traits to featurebaseIdentify
event.featurebaseIdentify = mapFeaturebaseIdentify(event);

try {
response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(event)
});
} 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}`);
}
}

Now you have to change both mapFeaturebaseIdentify and onIdentify functions.

It is VERY IMPORTANT that you modify both mapFeaturebaseIdentify and onIdentify in the code. It will not work otherwise. Please read the comments above each of the functions to better understand what is needed from you.

By properly customizing mapFeaturebaseIdentify, you get access to their data in the Users tab in the dashboard and on each post they create. If you do not wish to send along this information, add a return null; to the beginning of mapFeaturebaseIdentify function.

Once you've pasted the code, modified or disabled the mapFeaturebaseIdentify function and replaced YOURORGNAME in onIdentify, click on 'Configure':

Enter a name for the destination. Use something like 'Featurebase'. Then click on 'Create Function'

We are almost done now! All we have to do is connect the destination to something. Click on 'Connect Destination'

And select a source, then click 'Next'.

Put a destination name and click 'Save'

You've now successfully started syncing your segment userIds into Featurebase.

Did this answer your question?