SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

Remove userId Field from Segment Destination Payloads

Question

Can I remove a field such as userId from payloads that are sent to destinations in Segment?

 

Product

Twilio Segment

 

Environment

Segment Console

 

Answer

Yes, you can remove specific fields (including userId or custom identifiers) from payloads sent to your destinations by using Destination Insert Functions. Please note that the resulting payload will still need to satisfy the particular field requirements of the downstream destination, or else it could create delivery failures.

While standard Destination Filters are useful for dropping entire events or top-level properties, some destinations may automatically inject or enforce certain identity fields like userId. Implementing a custom Insert Function allows you to recursively scan the entire event payload and strip out specified keys before the data is transmitted to the destination.

 

Additional Information 

How to Implement This Solution

  1. In your Segment workspace, navigate to Connections > Destinations and select the destination you want to modify.

  2. Click on the Functions tab and choose to create or edit a Destination Insert Function.

  3. Here is an example of a recursive JavaScript function to clean your payloads. This code will inspect the event object at every level (including nested objects and arrays) and delete the specified keys.

/**
 * Recursively removes specified sensitive keys from an object or array.
 * @param {Object} obj - The event payload or nested property to clean.
 */
function cleanPayload(obj) {
    if (obj === null || typeof obj !== 'object') return;

    if (Array.isArray(obj)) {
        for (const item of obj) cleanPayload(item);
        return;
    }

    // Object.keys() only returns the object's own enumerable properties
    for (const key of Object.keys(obj)) {
        // Specify the keys you want to remove (e.g., 'userId')
        if (key === 'userId') {
            delete obj[key];
        } else {
            cleanPayload(obj[key]);
        }
    }
}

/**
 * Handle track event
 * @param  {SegmentTrackEvent} event
 * @param  {FunctionSettings} settings
 */
async function onTrack(event, settings) {
    // Learn more at https://segment.com/docs/connections/spec/track/
    
    // Remove sensitive fields like userId before sending to destination
    cleanPayload(event);

    // Log the cleaned event for verification
    console.log(event);
    
    return event;
}
  1. Test & Deploy: Use the built-in test console in the Segment Functions editor to send a mock event, verify that the userId is successfully removed from the output, and deploy the function to your live destination stream.

 

Have more questions? Submit a request
Powered by Zendesk