How to Send Personalized SMS Using Twilio Segment & Twilio

February 18, 2021
Written by

How to Send Personalized SMS Using Twilio Segment & Twilio

SMS is one of the most powerful engagement channels in a developer’s toolkit. Yet for consumers, it’s also one of the easiest to ignore. 

Every day, thousands of spammy, poorly targeted messages are blasted out to consumers using legacy communications technology. These tools can’t reference basic user attributes like name, location, and activity right, let alone add the required personalization drive open rates and engagement.

Text message from a brand promoting a new selection of sneakers and inviting customers to visit the website.

Thankfully, the technology today is WAY more sophisticated than it used to be, so this type of attritional marketing doesn’t have to be the norm.

Customer data platforms like Twilio Segment helps you collect clean, accurate customer data to build upon. You can understand who your users are, what geo they’re in, what actions (if any) they’ve taken with your brand, and what their preferences are. 

What’s more, it’s easy to have all that data stitched together in one profile. By using and combining these data points, you can create SMS marketing campaigns hyper-personalized to the behavior of the recipient, and therefore more likely to be effective.

In this tutorial, we’ll walk through how to use Twilio Segment and Twilio Programmable Messaging to send a personalized SMS campaign.

Step 1: Connect your customer data with Twilio Segment

Let’s say you’re an e-commerce business and would like to send personalized SMS messages based on a user’s prior website activity.

This is where Twilio Segment comes in. By instrumenting Twilio Segment on your website, you can implement event tracking to understand your website visitor’s behavior, whether that’s clicking on promotions, viewing products, completing an order, etc. 

To do this, simply sign up or log in to your Twilio Segment account.

Next, create a Source to collect data on your e-commerce website. A Source is where actions taken by customers such as Product Viewed and Order Completed are collected as “events” and sent to Twilio Segment. Since you’re tracking website behavior, you’ll want to start with a Javascript source. Click Add Source and give the source a name e.g twilio-segment-store.com

Once you’ve connected your source, you’ll want to install the Twilio Segment snippet on your website. This snippet means that your website can easily call the Twilio Segment APIs when a visitor takes a certain action. Simply copy and paste the snippet from the JavaScript Source overview page into the <head> tag of your site. 

Twilio Segment source page with installation instructions for a Javascript snippet and a function to load analytics, showing the source status and a Twilio Function destination.

👉 Check out these docs to learn more about setting up your source. 

After you set up your Source, you’ll begin to implement events. Events are actions such as page views or button clicks performed by your users on your website. A good comprehensive list of what e-commerce events you can track can be found in our e-commerce spec.

Table listing various customer events related to the checkout process, including user actions like 'Product Clicked,' 'Cart Viewed,' 'Checkout Started,' and 'Order Completed,' with descriptions.

If you want to check if your data is flowing correctly, head on over to the Debugger to see the data flowing in real-time.

Debugging interface showing event tracking logs with a list of recorded events and their details, including timestamps and data payloads.

Step 2: Deliver your personalized SMS with Twilio

Once you’ve set up event tracking with Twilio Segment, it’s time to connect to Twilio's APIs to send your personalized SMS notification

In order to do so, we have to create a “destination” for Twilio. Destinations are where you want your route customer data to once it’s been collected.

Image 7

Twilio Segment’s official Twilio destination is in the works, so in the meantime, we can use Twilio Segment Functions to build a seamless integration. 

Functions is a hosted service offered by Twilio Segment that allows you to build a custom source to ingest data from anywhere into Twilio Segment or build a custom destination to send data you're collecting on Twilio Segment into any third-party API endpoint by just writing a few lines of JavaScript. 

Selection screen for creating a new function, showing options for source (ingest data) and destination (send data to endpoint), with a "Build" button.

For the purpose of this tutorial, let’s use the Twilio function template, which makes it even easier for you to get started.

Code editor window showing a Node.js script that sends a message to a Twilio destination, with a list of available templates on the right and a "Save & Deploy" button.

This template does four things:

  1. Listens to each and every event that's coming from Twilio Segment.
  2. Infers the item purchased by looking at the Order Completed properties.
  3. Stitches together a personalized SMS message.
  4. Calls the Twilio API endpoint to send the SMS notification to customers.

💡 ​​Bonus tip: If you’d like to use the code we are using in this recipe, check it out here.

We can test this out in practice by using a sample event. 

On the right-hand side of the screen, click Use Sample Event. Then, select the data source you set up for your e-commerce website, and the specific event that you’d like to trigger the message such as Order Completed. Click Save Event. This will bring you back to the Functions homepage, where you can see the event and associated properties in the Payload section of the screen.

Code editor window with an open modal showing the details of a sample event, including order status, payload data, and a button to "Use Event."

Simply add your Twilio account ID, auth token, from and to numbers to the right-hand side of your screen, as well as your Event Name that will trigger the message (in this case Order Completed).

Code editor window showing a Twilio Function script for sending messages, with input fields for various configuration parameters and a "Save & Deploy" button.

It’s time to deploy!

Hit Run and you’ll see a success message appear confirming that the API call is being made to Twilio and that an SMS is being made. You can log in to the Twilio Console to confirm the SMS has been sent.

SMS message details from Twilio, including message ID, source, creation time, sender and receiver numbers, message body, delivery status, and inspector notes about message age and log retention.

Wrapping up

As a recap, here’s what we’ve done in this recipe:

  • Created a Twilio Segment source and implemented event tracking to understand user behavior on our website.

  • Connected the Twilio Segment source with Twilio via Functions.

  • Triggered personalized SMS via Twilio Programmable SMS.

You’re ready to enjoy all the benefits that come from personalized SMS – better open rates, increase engagement, and a better customer experience.

Want to implement personalized SMS for your own business? Get in touch with a member of the team or start building today.

Ready to see what Twilio Segment can do for you?

The Customer Data Platform Report 2025

Drawing on anonymized insights from thousands of Twilio customers, the Customer Data Platform report explores how companies are using CDPs to unlock the power of their data.