Send Twilio Events to Segment using Event Streams

June 27, 2022
Written by

Banner for Send Twilio Events to Segment using Event Streams

In this post, you will learn how to send Twilio events to Segment using Twilio Event Streams with a Segment Sink. This is an out of the box integration that requires no code to get started. We will also cover the benefits of using this integration, plus show you some use cases to help you optimize using Twilio and Segment together.

Prerequisites

To get started you will need:

  • A Twilio account with access to Event Streams – sign up here for a free developer account. See our documentation here on how to access Event Streams.
  • You will also need access to the Segment Sink – the documentation linked shows how to access it.
  • A Segment account – Sign up here for a trial account.

What are Event Streams in Twilio?

Event Streams is an API that allows you to configure centralized streams of event data directly into your infrastructure, so that your data is easily accessible, automatically interpretable, and reliable ensuring you can make well-informed decisions. Teams who heavily integrate Twilio in their infrastructure and rely on webhooks for monitoring and logging Twilio events find using Event Streams a powerful way to centralize Twilio events instead of setting up a webhook for each individual service.

Event Streams currently supports the following Event Types:

  • Error Logs
  • Messaging
  • Studio
  • Super SIM
  • TaskRouter
  • Video Insights Log Analyzer
  • Voice Insights
  • Conference Insights

For a full list of events for the Event Types list, see our documentation.

In addition to supporting a Segment Sink, Event Streams supports Sinks with Amazon Kinesis and Webhooks.

Why use the Segment Sink

Using the Segment Sink with Event Streams is straightforward to set up, and requires no additional code if you’re setting up the Sink in your Twilio console. Twilio provides APIs for every product, and you can set up your Sinks and Subscriptions programmatically, too.

With a few clicks you can have actionable Twilio insights ready to be delivered to your data warehouse, marketing automation tools, CRM, product analytics tools, or any other Destination you have in Segment.

For example, with the Segment Sink with Event Streams you can:

  • Send SMS data to your product analytics tool, such as Amplitude or Heap, to see how your SMS campaigns affect downstream metrics such as purchases or ROI
  • Send Studio Flow progress to your product analytics tool to see how your funnel is converting or if users are getting stuck on a certain step in your IVR. See our post here on Capturing Studio Flow Progress with Segment.
  • Create logic within Segment to update a user’s preferred channel for notifications if calls or SMS delivery continually fail. See our post here on Personalize Communication Channel Outreach using Segment and Twilio.
  • If you’re creating new features using Twilio’s APIs such as Video or Voice, it’s straightforward to aggregate data and error logs in a product analytics tool or data warehouse with a feature flagging tool (such as LaunchDarkly) to quickly surface insights on feature rollouts. See our post here on Sending Twilio Voice Data to Segment using Event Streams.

Aside from being able to combine Twilio data with your Segment Destinations, you can also use the Event Stream events with Segment Personas to make your data more actionable. For example, you can use these events to create Computed Traits or Audiences, or activate a user for a Journey.

You may also be familiar with the Twilio Source integration that provides the same event types as the Event Streams Segment Sink. Here are differences between the Twilio Source integration and the Event Streams Segment Sink:

  • Twilio Source connection polls Twilio every 3 hours, whereas the Event Stream Segment Sink publishes events in real time.
  • Twilio Source connection publishes only to your Segment warehouse, whereas the Event Stream Segment Sink can send and enrich data to any of your destinations.
  • Events from the Event Stream Segment Sink can be used with any of the Personas features including Audiences, Journeys, and Computed Traits.

Setting up Twilio Event Streams to use with Segment Personas

To set up Twilio Event Streams for Segment, see the Quickstart in our documentation. This will walk you through setting up the Source in Segment, as well as show you how to subscribe to Twilio events in the Twilio console or via the Twilio API.

Once the Twilio Event Streams Source is set up in Segment and you have subscribed to events in Twilio, you should see Twilio events populate under your Source Debugger. See below for a screenshot of what this should look like in your Segment Debugger:

Screenshot of Segment debugger

Keep in mind that these events will be the only data that Segment receives from Twilio. There isn’t a way to retrieve event history from Twilio, so you will only have access to data that Segment collects after you successfully enable this Source.

To use these Twilio events in Personas, navigate to your Personas dashboard and click Settings. Next click Sources on the top of the page, click the Connect Sources button, and select your Twilio Event Streams source. See the screenshot below to see what your Personas Settings should look like.

Screenshot of Personas Settings

Next, click Identity Resolution on the top and click the Add Identifier button. When prompted, select Custom Identifiers, and enter phone for Trait/Property key to match on. Click Add New Identifier to save this. Your Identity Resolution dashboard should look like this:

Screenshot of Segment Identity Resolution Settings

Now that you have added phone as a user trait, Segment can merge the events that come from the Twilio Event Streams Source to users that have been created or updated with a Segment identify() call. Keep in mind that Segment will only be able to associate these events with a user if they have phone as a trait.

Here is an example of an identify call that includes the phone trait:

  var Analytics = require('analytics-node');
  var analytics = new Analytics('SEGMENT_KEY');

  analytics.identify({
    userId: '1232323234',
    traits: {
      name: 'Michael Jordan',
      email: 'mj@example.com',
      phone: '+15551234567'
    }
  });

Now when you create an Audience, Computed Trait, or Journey, the Event Streams will be available as events to use.

Screenshot of the Segment dashboard showing the Event Streams events that show up when configuring your audience.

Only some event types support a merge on the phone identifier in the Identity Resolution Configuration in the Personas settings. Check back on the Event Streams documentation to see when additional event types are added.

Now that you have your Twilio Event Streams connected to Segment, go ahead and create some events to see your data flowing into Segment. If you subscribed to Messaging events, sending or receiving any message to any Twilio number with that Account SID will populate Messaging Events in Segment. The same applies for any of the other listed Event Streams Event types – as long as the Event Type is subscribed to in your Event Stream in Twilio. Once you have events populating in Segment, you should be able to click into a user profile in the User Explorer under the Profiles tab in Personas and see the Event Streams events associated with the user based on their phone trait.

Screenshot of User Profile Events in Segment

What to do next

Now you should be able to seamlessly stream events from Twilio to Segment and use these events to enrich other data sources and orchestrate audiences based on engagement data. Try it out for yourself by setting up one of our other Event Streams tutorials such as Sending Twilio Voice Data to Segment using Event Streams. We can’t wait to see what you build with your Twilio data in Segment!

Heather Hargreaves is a Solutions Engineer at Twilio and enjoys helping people build better products through analytics and experimentation. When she’s not writing blog posts she’s playing with her dog in New York.

Jonathan Kim is a Solutions Engineer at Twilio Segment, helping brands drive more personalized experiences for their customers. Outside of the work, Jonathan can typically be found out on the golf course or making beats on the cajon.