Segment Integration

Learn to integrate Featurebase with Segment and the key features.

MP

Written By Markus Palm

Last updated 15 days ago

Overview

The Featurebase ↔ Segment integration enables you to easily track all events (like new posts, comments, and votes) that your users make on your Public Portal.

Key features:

  • Track different events, page views, and user actions on your Featurebase Portal

  • Sync your users’ data from Segment to Featurebase for better feedback prioritization, automatic authentication, etc.

Example: If you already have your user data in Segment, you can sync it with their feedback in Featurebase to see their info (e.g. plan, company size, etc.) next to their posts.

You can also automatically authenticate them to Featurebase so they don’t have to create a separate account or log in.


Setting up the Segment integration

1. Install the Segment integration

  1. Start in Segment by going to the connections tab, and click Add Source

  2. Select Javascript and click next on the bottom right corner

  3. Enter your website name (e.g. “yourcompany feedback portal”)

  4. Add the website URL, which is your Featurebase Portal’s URL (e.g. https://yourcompany.featurebase.app/)

  5. Click Create Source

  6. Copy the text (Write key) in the analytics.load() function as seen below 👇

  7. Go to Featurebase Settings → Integrations

  8. Click set your API key, paste in your Segment write key, and click save

All done! 🎉 You can now see all the events made on your feedback portal appear under the Debugger in Segment.

2. Sync Segment userIDs to Featurebase

This part of the set up goes through the process of syncing your Segment userIDs into Featurebase to allow tracking your users better.

  1. In Segment, go to Connections → Destinations, and click Add Destination

  2. Select the Functions tab and click on Create Function

  3. Pick Destination and click on Build on the bottom right

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

    Example
    /** * 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://YOURORGNAME.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}`); } }
  5. Change both mapFeaturebaseIdentifyand 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.)

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

  7. Enter a name for the destination (e.g. Featurebase) and click on Create Function

  8. To connect the destination to something, click on Connect Destination

  9. Select a source and click Next

  10. Add a destination name and click Save

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


And that's it! 🎉 If you have any ideas about how we can improve the Segment integration, please post on our feedback board.