Build Call Tracking with Twilio Programmable Voice

This tutorial demonstrates a web application that uses Twilio to track the effectiveness of various marketing channels through call tracking.

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 to do it yourself in this tutorial. This sample application has three main features:

  • Purchases phone numbers from Twilio to use in different marketing campaigns (like a billboard or a bus advertisement)
  • Forwards incoming calls for those phone numbers to a salesperson
  • Displays charts showing data about the phone numbers and the calls they receive
Loading Code Samples...
Language
from django.conf import settings
from twilio.rest import Client

import os

# Uses credentials defined in TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN
# environment variables
account_sid = settings.TWILIO_ACCOUNT_SID
auth_token = settings.TWILIO_AUTH_TOKEN

client = Client(account_sid, auth_token)


def search_phone_numbers(area_code=None):
    """Queries the Twilio REST API to get phone numbers available for puchase"""
    # You can change the country argument to search outside the US
    # area_code is an optional parameter
    numbers = client.available_phone_numbers("US") \
                    .local \
                    .list(area_code=area_code)

    # Returns 30 by default - let's trim the list for UX purposes
    return numbers[:10]


def purchase_phone_number(phone_number):
    """Purchases a new phone number from the Twilio API"""
    # Use a TwiML Application SID so all our numbers use the same voice URL
    number = client.incoming_phone_numbers.create(
        phone_number=phone_number,
        voice_application_sid=settings.TWIML_APPLICATION_SID)

    return number
call_tracking/utils.py
Search for phone numbers available for purchase

call_tracking/utils.py

Find your language and framework of choice below to get to the source code in your language and follow step-by-step instructions to learn how to build call tracking into your own applications:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

Loading Code Samples...
from django.conf import settings
from twilio.rest import Client

import os

# Uses credentials defined in TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN
# environment variables
account_sid = settings.TWILIO_ACCOUNT_SID
auth_token = settings.TWILIO_AUTH_TOKEN

client = Client(account_sid, auth_token)


def search_phone_numbers(area_code=None):
    """Queries the Twilio REST API to get phone numbers available for puchase"""
    # You can change the country argument to search outside the US
    # area_code is an optional parameter
    numbers = client.available_phone_numbers("US") \
                    .local \
                    .list(area_code=area_code)

    # Returns 30 by default - let's trim the list for UX purposes
    return numbers[:10]


def purchase_phone_number(phone_number):
    """Purchases a new phone number from the Twilio API"""
    # Use a TwiML Application SID so all our numbers use the same voice URL
    number = client.incoming_phone_numbers.create(
        phone_number=phone_number,
        voice_application_sid=settings.TWIML_APPLICATION_SID)

    return number