Create Computed Traits
Once your engagement metrics can be sent from Twilio and Sendgrid to Segment by setting up the webhooks described above, these events can be used to create Computed Traits in Segment. Computed Traits are how you will be able to set a “preferred channel” for a user once the user has engaged with that channel most frequently.
Before we can create a Computed Trait in Segment, you will need to send some events to Segment. Get started by running your Node application with the terminal command node index.js
in the root directory of your application.
You should see a text, email, or call come into the number or email address you set in your .env
file. Answer the call, open the email, or send a confirmation text back to see events come into your Segment account in the debugger. While all the events have the name “Notification Engaged”, they are separated by their Source Function which will inform the Computed Trait of the user’s engagement channel.
Restart your server a few times to send more events to Segment – do this at least five times. Once you’ve done that, navigate to your Segment dashboard, then navigate to "Engage", then “Computed Traits”, and finally click “New Computed Trait”. Select “Most Frequent”, and then for the “Event Name” select “Notification Engaged” with the “Minimum Frequency” set to 3
.
You can change the number, but for this demonstration let’s use 3
“Notification Engaged” events at minimum before calculating which channel—email, SMS, or call—is a user’s most frequent.
Once a user has a Preferred Channel Computed Trait assigned to them, they will receive notifications on that channel exclusively in the future. For example, if the user’s preferred channel is “email”, then your Node application will check Segment using the Profile API and set the channel to “email” rather than allowing the channel to be picked randomly. Additionally, you can see what a user’s Computed Trait is on the Engage dashboard within your Segment account.
Nice work – you’ve hopefully now got a working app which computes your users’ preferred notification channels. Let’s look at a few more things you can try, and cover some of the problems you might hit.
For more interesting results:
Restart your Node app server a few times until you get a Computed Trait, then try changing the user’s name and user id in your Node app to create additional users.
Troubleshooting not seeing events in Segment:
Make sure your Source Functions are set up correctly, and that the Call Source Function URL is added to you .env
file. Make sure the Email Source Function URL is added to your Sendgrid dashboard as the event webhook and the SMS Source Function URL is added to your number as the incoming message webhook in your Twilio dashboard.
You can double-check whether these events are coming in by checking the Source Function debugger in your Segment dashboard.
Troubleshooting not getting an SMS, email, or call:
Take it to the next level:
Set up an analytics Destination in Segment to send conversion events to your analytics tool and measure what channel performs best across all of your users.
Congratulations on connecting Segment, Twilio, and Sendgrid! With this demo app, you can automatically select a user’s preferred channel by tracking what they engage with and assigning a Segment Computed Trait. This is a powerful way to personalize your communications with your users and customers on a one-to-one level.
Once you have this under your belt, explore what other Segment Computed Traits you can use to power a Twilio and Sendgrid communication. We can’t wait to see what you build!