Python | Django
Call Tracking helps you measure the effectiveness of different marketing campaigns. By assigning a unique phone number to different advertisements, you can track which ones have the best call rates and get some data about the callers themselves.
Companies like Trulia and Datalot use Twilio to power their call tracking. Learn how do it yourself in this tutorial - scroll down to get started!
Use Twilio to track the effectiveness of your marketing campaigns. Let's get started!Start Tutorial
This Django web application shows how you can use Twilio to track the effectiveness of different marketing channels.
This application has three main features:
In this tutorial, we'll point out the key bits of code that make this application work. Check out the project README on GitHub to see how to run the code yourself.
Let's get started! Click the right arrow above to advance to the next code snippet.
Call tracking requires us to search for and buy phone numbers on demand, associating a specific phone number with a lead source. This utility module uses the twilio-python helper library to search for phone numbers by area code and return a list of numbers that are available for purchase.
We display a form to the user on the app's home page which allows them to search for a new phone number by area code. At the view level, we use the utility module we created earlier to actually search for numbers, and display an HTML page to the user with a list of numbers they can choose to buy.
purchase_phone_number utility function takes a phone number as its
sole parameter and uses our Twilio API client to actually purchase one of
the available phone numbers we searched for earlier.
Once we search for and buy a Twilio number, we need to associate it with a lead source in our database. This is the core of a call tracking application. Any phone calls to our new Twilio number will be attributed to this source.
LeadSource model associates a Twilio number to a named lead source
(like "Wall Street Journal Ad" or "Dancing guy with sign"). It also tracks
a phone number to which we'd like all the calls redirected, like your sales
or support help line.
Lead represents a phone call generated by a
LeadSource. Each time
somebody calls a phone number associated with a
LeadSource, we'll use the
Lead model to
record some of the data Twilio gives us
about their call.
Whenever a customer calls one of our Twilio numbers, Twilio will send a POST
request to the URL associated with this view function (should be
We use the incoming call data to create a new
Lead for a
then return TwiML that connects our caller
forwarding_number of our
One useful statistic we can get from our data is how many calls each
LeadSource has received. We use the Django ORM's annotate feature to make a list containing each
LeadSource and a count of its
Lead models. We create a custom model manager
so that we can easily access this data in our views.
That's it! Our Django application is now ready to purchase new phone numbers, forward incoming calls, and record some statistics for our business.
If you're a Python developer working with Twilio, you might also enjoy these tutorials:
Put a button on your web page that connects visitors to live support or sales people via telephone.
Send your customers automatic reminders ahead of appoiments using Twilio SMS.
Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Tweet @twilio to let us know what you think.