- Create a custom Segment destination for Novu
- Map Segment identify calls to Novu subscribers
- Trigger notification workflows from Segment track events
- Handle errors and retry logic for reliable delivery
Before you start, ensure you have:
- A Segment account with access to Functions (check your workspace permissions)
- A Novu account with an API key (find this in your Novu dashboard under Settings > API Keys)
Create a Destination Function in Segment
- Log in to your Segment account
- Navigate to Connections > Functions in the left sidebar
- Click New Function and select Destination
- Name your function (e.g., Novu Destination) and click Create Function
Configure the Destination Function
The Destination Function will handle two key Segment event types:- identify: Creates or updates a subscriber in Novu
- track: Triggers a notification workflow in Novu
Code Explanation
Code Explanation
onIdentify:- Maps Segment traits (e.g., firstName, lastName, email) to Novu subscriber fields
- Uses Novu’s
/v2/subscribersendpoint - Creates or updates subscribers (Novu’s API is idempotent for existing
subscriberIds)
onTrack:- Maps Segment
trackevents to Novu workflows usingEVENT_TO_WORKFLOW_MAPPINGS - Sends the event properties as the payload to trigger a workflow via
/v1/events/trigger
- Maps Segment
- Error Handling: Retries on server errors (5xx) or rate limits (429), fails fast on other errors
Deploy the Function
- Click Save in the Function editor
- Enable the function by toggling it to Active
Connect the Function to a Source
- Go to Connections > Select your Source (e.g., website, app)
- In the Destinations tab, click Add Destination
- Choose your Novu Destination Function from the list
- Click Connect. When prompted, enter your Novu API key in the apiKey field
- Save the configuration
Testing the Integration
Verify everything works:1. Send an identify event
1. Send an identify event
Example:Check Novu’s Subscribers list to confirm the subscriber appears.
2. Send a track event
2. Send a track event
Example:Verify the
welcome workflow triggers in Novu’s activity feed.Troubleshooting
Troubleshooting
- 401 Unauthorized: Double-check your Novu API key in the function settings
- Subscriber Not Created: Ensure userId is included in the identify event
- Workflow Not Triggering: Confirm the event name matches a key in EVENT_TO_WORKFLOW_MAPPINGS and the workflow exists in Novu
- API Errors: Check Segment’s logs for detailed error messages
Additional Notes
Additional Notes
- Subscriber Updates: Novu’s /v1/subscribers endpoint updates existing subscribers if the subscriberId matches, keeping data current with each identify event
- Expanding Functionality: Add more event types (e.g., group, page) by defining additional handlers like onGroup in the code