Personalize your Twilio IVR with Segment

November 11, 2021
Written by
Reviewed by

Segment Twilio IVR

With Segment, it has never been easier to obtain, aggregate, and act on customer interaction with any product your company provides. You can use this information to improve your product, send tailored marketing messages, or tailor your customer service to your customers' needs.

As customers, we all hate repetitive questions, being forwarded from one agent to another, having a very time sensitive issue but still having to wait for ages to resolve this, wondering if the company you are calling would still make you wait if they just knew how bad of a problem you have. If you already use Segment, you know that with the vast amount of information you have about your customers, you can probably guess what their problem is.

Today, I will show you how you can leverage your Segment data with Twilio Studio, Functions, and TaskRouter to properly route your customers to the right agent and prioritize critical issues.

Tutorial prerequisites

Before we can get started building a more advanced integration, you need to make sure you have accounts with Twilio and Segment. You will need:

  • A Free Twilio Account. If you sign up through this link, you'll receive an extra $10 if you upgrade
  • A Flex Project. If you don't have one yet, these quickstarts will help guide you. The Flex project is great for this tutorial as it has all the connections between Studio, TaskRouter, and an UI already set up. The same functionality applies for other projects, though!
  • A Twilio Segment Business Account. If you do not have one yet, book a demo and talk to sales about personas

And with that, you're ready to start.

How it works

With Segment, you can create events from almost everywhere. A business account allows you to create personas, compute attributes, and assign these personas to audiences you would like to interact with in a personalized way.

Twilio, in turn, can interact with Segment over HTTPS. With Twilio and Segment, one could

  • Write events (e.g., an incoming call, the outcome of a conversation) to Segment via the HTTP API in order to augment personas
  • Read out persona information

Twilio Segment Architecture

Creating the interactions between the two systems forms the Integration.

Why and when would you want to integrate Twilio and Segment

When your organization has (or is planning to build up) information about your customers and wants to tailor your communication strategy to the individual requirements of your customers, integrating Twilio and Segment is a straightforward option that should be part of your customer experience strategy.

Some scenarios when you might want to make use of your Segment data in your communication:

  • You want to store information about call behavior and outcome, e.g., to ask the customer for a review after your customer had a great experience
  • You want to use information from other sources to route your call
  • You want to prioritize customers who are at risk of churn
  • You want to present information about your customer to your agents (this is covered in a great article, Segment Personas + Twilio Flex: Bringing Personalization to the Contact Center)
  • ... and so on.

Let's walk through this in detail.

Developer Environment Setup

Let's make sure you have the software you need to integrate Twilio and Segment. For this, you will need:

Create data in Segment

In the following steps, we will

  • Create a profile via HTTP
  • Add example events via HTTP
  • Configure computed traits and audiences

Before we can start creating profiles and audiences that we can act upon from Twilio, we first need to enable the personas feature (exclusive to the business account). Go to personas, and a wizard will guide you through the process.

We also must provide some data to Segment. Go to your Segment Account, click on Connections -> Sources, then create a new HTTP API source. In the following screen, you will find a write key – hold on to this, we will need it on several occasions.

Segment write key

With this key, we add 3 data points to Segment:

  • create a profile (the customer you are going to complete the tutorial with)
  • add a past issue (your credit card was blocked)
  • fake a phone call

We already do this now, so we have enough information to set up some computed traits and to be able to define an audience in Segment. This only works once Segment knows about your data structure, so we feed it with some example data. After each API request, you can verify that Segment captured the data by going to Connections -> Sources -> HTTP API -> Debugger.

curl -X "POST" "" \
     -H 'Content-Type: application/json' \
     -u 'YOUR_WRITE_KEY:' \
     -d $'{
  "userId": "YOUR_PHONE_NUMBER",
  "traits": {
    "name": "YOUR_NAME"

Configure Segment Computed Traits and Audiences

By now, you should see your profile in Segment under "Personas". Click on your profile, in the events you should see the error and the call we posted earlier.

Persona events

Now that Segment knows your data structure, we can go ahead and compute some traits.

In this example, we will not push the computed traits to any destination. Be sure to tick the checkbox "Compute without enabled destinations" wherever applicable. Otherwise, your computed traits will not calculate or update.

When the customer had multiple issues or calls, it becomes hard to make sense of all these data streams. With computed traits, we can aggregate that information to useful insights. Specifically, we want to look at:

  • What was the average waiting time of the customer?
  • How often did the customer fail to reach a service rep?
  • What is the sum of the issue severity he has?
  • What was the customers last issue? Which department would be the right one to connect?

Old data becomes less relevant; we are only interested in all these aspects for the last 7 days. Under Personas, click on computed traits and start creating the following traits (be sure to run computation over the last 7 days for all traits):

Error severityAggregationAggregation type: sum, event type: error, property: severity
Last errorLastEvent name: error, property: name (text)
last error departmentLastEvent name: error, property: department (text)
calls last 7 daysEvent CounterEvent name: call
avg wait last 7 daysAggregationAggregation type: average, event type: call, property: wait_time
disconnected last 7 daysEvent CounterEvent name: call, condition: "connected equals false"

Error severity computed trait

Now that we have all our computed traits, we want to know which users had a bad experience so we can prevent churn.

It might take a while until Segment has calculated these traits – if the list looks light when you are creating your audience, please wait for a while.

We will now add an audience called "bad experience last 7 days" with the following criteria:

Bad experience audience

That is all there is to do on Segment side. Within minutes, we have created a model that calculates core information about our customers and is able to detect if someone had a bad experience in the last 7 days. Let us act on this in Twilio, by greeting the customer personally, routing them to the right department, and prioritizing them if we feel we should give special attention to this customer!

Read and write Segment data with Twilio Functions

In order to read and write data with Twilio, we will utilize Twilio Functions, the serverless envioronment provided by Twilio. The easiest way to develop and deploy a function is with the Twilio Serverless Toolkit. In your Terminal, create a new project and install dependencies with:

twilio serverless:init SegmentIVR
cd SegmentIVR
npm install axios

The CLI will now upload your functions to Twilio and return their URL. Write them down, we will need them in the last step – they should look something like /

Twilio CLI upload

So far, we have uploaded some sample data to Segment and created computed traits and audiences to obtain meaningful data about our customers. We have then deployed two functions that allows us to read and write Segment data from Twilio. All that is left to do is to trigger these functions from Twilio!

Act on Segment data from Twilio Studio

When a customer calls in, the first thing we want to do is query Segment to find this person's traits and audience membership. If they had a recent issue, we will confirm the customer is calling for the same reason, which helps us route them to the proper agent. In any case, we will check if the customer is part of the bad_experience_last_7_days audience and prioritize them accordingly.

Twilio IVR acting on Segment data

To create this IVR, create a new Studio Flow, pick "Import from JSON" and add the content of this JSON. Do not forget to replace the URL in the fetch_segment widget with your function URL and adapt the TaskRouter workspaces and flows in both "Send to Flex" widgets. Once you have saved your flow, link your phone number to that flow – you can find instructions here.

Call your phone number, you should be greeted by name, your past issue should be confirmed and you should be prioritized! The number of disconnects and the average wait time of the calls play an important role in determining wether a customer is part of the bad experience audience. With TaskRouter and the handleCall function, we can track exactly that. Head over to your TaskRouter workspace, click on settings, and set the event callback URL to your handleCall function. We are only interested in "Reservation Accepted" and "Task Canceled" events. For more details about the task lifecycle, you can read up here.

TaskRouter Callback Settings

Call yourself and hang up after a while. In Segment, you will see the events flowing in and, after a while, be reflected in our computed metrics. Now, even if we have not detected an error with our customer, we can prioritize them due to many calls, long past wait time and more.

I hope these examples gave you an idea on how to integrate Twilio and Segment. The examples given are not exhaustive, so the real strength of this is that you can tailor this to your companies individual needs and metrics. I cannot wait to see what you build with Segment and Twilio!

You can find the full code in the GitHub repositories for the Twilio Segment Integration.

Andrej is a Principal Solutions Engineer at Twilio. He's currently helping companies in EMEA design great customer engagement solutions powered by Twilio. He can be reached at asaweljew [at], or you can collaborate with him on GitHub at