Phone Call Attribution with Twilio and Google Universal Analytics

It’s a given – you should be tracking conversions on your website. In Google Analytics, conversions (aka “goals”) allow you to see how often visitors complete specific actions. Usually, the primary “goal” for a business is to get a new customer. This typically happens when someone either fills out a form on the website or places a phone call to the business.

This article discusses a self-hosted solution using Twilio and Google Universal Analytics for phone call attribution.

Form submissions are fairly easy to track as a goal, and most business are already (or should be) doing this. The latter is a bit more difficult and more expensive. Unfortunately, for many businesses, customers may prefer to call rather than fill out a form. It largely depends on the business, but some clients have seen 70% of new customers initially contact via phone, and 30% using a form on the website.

The point is this – if you can’t tie phone calls back to the ad campaign, landing page, or other visitor dimensions that are significant in converting the customer, you’re very likely to be misinformed when it comes to optimizing your ad spend and website content.

Tutorial Summary

Basically, what we are doing is buying a phone number from Twilio that we will make specific to a campaign, landing page, and/or other dimensions you want to track phone calls against. When someone calls your Twilio number, the call is forwarded to your existing office phone. After the phone call ends, Twilio is programmed to run a PHP script that exists on your domain, which sends an event to your Google Analytics account (including those details of the call such as call length, campaign source, landing page, or other dimensions you find important to track). Once this event is stored in Google Analytics, we then set up a goal for that event so that we can see the number of phone calls in the standard conversion reports alongside other goals like number of form conversions.

For a “plug and play” yet more expensive solution, check out Mongoose Metrics or IfByPhone.

Steps to Complete

  1. Check your Google Analytics (“GA”) version
  2. Configure and upload tracking script to your domain
  3. Sign up for Twilio. Purchase, program, and test phone numbers
  4. Test phone call event tracking in GA
  5. Set up phone call event as a goal in GA

Step 1 – Check Google Analytics version

We will be taking advantage of a feature of the new version of Google Analytics called Measurement Protocol. Measurement Protocol allows you to send interaction events (such as a phone call) directly to Google Analytics servers using HTTP requests.

The new version of Google Analytics is called Universal Analytics, and is currently in beta. Eventually, everyone’s Analytics accounts will be migrated to Universal Analytics, but as of right now it is a manual upgrade process. If you have not migrated to this new version, please follow the instructions outlined here.

The migration period on Google’s side takes a few days, after which point they will inform you to update the tracking code on your website. You will not lose historical data and Google Analytics will continue tracking new data while the migration is taking place.

Step 2 – Configure and Upload Tracking Script

This is the PHP script that Twilio will fire after each phone call to send data to Google Analytics. It must be configured with your GA account details and the dimensions you want to track for different phone numbers. Save it as a .php file (named track.php in this example) and upload it to your server.

<!--?php 
// Twilio-Google Analytics Phone Call Tracking
// http://dbgonz.com/tutorials/phone-call-attribution-with-twilio-and-google-universal-analytics/
//
//set your timezone
//full list of supported timezones here: https://php.net/manual/en/timezones.php
date_default_timezone_set("America/Chicago");
 
//Fill in your Google Analytics Tracking ID (looks like with UA-XXXXXXX-X)
//This can be found in the property settings of your Google Analytics account
$gatid = 'UA-XXXXXXX-X';
 
//Create a unique ID (required for all measurement protocol hits)
$uuid = uniqid();
 
//Lave these alone. Twilio is providing the GET variables with the call data
$datetime = date('m-d-Y--H:i:s');
$callDuration=$_GET['CallDuration'];
$campaignNum=$_GET['To'];
 
//Add a new elseif statement for each Twilio phone number you want to track.
//The dimensions included (source, medium, campaign name) are only a few of the available dimensions you can send to GA
//See full list of parameters available in measurement protocol here:
//https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
 
if ($campaignNum == '+15555555555') {
	$campaignSource = 'google';
	$campaignMedium = 'cpc';
	$campaignName = 'Some%20Campaign';
} elseif ($campaignNum == '+15555555555') {
	$campaignSource = 'google';
	$campaignMedium = 'cpc';
	$campaignName = 'Another%20Campaign';
}
 
header("Location: http://www.google-analytics.com/collect?v=1&tid=$gatid&cid=$uuid&t=event&ec=Twilio&ea=Call&el=$datetime&ev=$callDuration&cs=$campaignSource&cn=$campaignName&cm=$campaignMedium");
 
exit;
?-->

Step 3 – Sign up and Configure Twilio

Sign up for a new account at twilio.com and purchase phone number(s). These are the settings you should use when configuring your number:

Request URL is what Twilio should do when someone calls your new number. Twilio has set up common codes (such as call forwarding) called “twimlets”, all you have to do is input your parameters. The only parameter is the phone number you want calls to be forwarded to, which is your existing phone line.

1
http://twimlets.com/forward?PhoneNumber=555-555-5555

Status Callback URL is the code you configured in step 2, which Twilio will run after the phone call ends. Also, make sure the dropdown is set to GET, otherwise those variables will not be received by your script.

1
http://www.YOURDOMAIN.com/track.php

Screenshot: 

twilio-config

Step 4 – Test Phone Call Event Tracking in Google Analytics

Place a call to your Twilio number. It should ring the line you specified it to forward to. Open up Google Analytics and navigate to the Real Time -> Events report. A few seconds after hanging up your test call, you should see the event register on screen. Not all dimension details (eg. Campaign, source, medium, landing page, etc) are shown in this report, but at least from here you can see that the event is successfully sent to Google Analytics. I recommend doing this test for every phone number.

Step 5 – Set up Phone Call Event as Goal in Google Analytics

Now we want to set up the event we’ve captured as a goal, so we can see the goal conversions in standard reports alongside other goals. If you do not know how to set up a goal in Google Analytics, check out this article.

We will need to match the event conditions with what was set in the script above (not discussed, but it’s in there). Set category to “Twilio” and action to “Call”.

The value being sent to Google Analytics in the script is the length of the call (in seconds). If you only want to record the event as a conversion for phone calls lasting over X minutes, you can put a cutoff value in here (eg. greater than 120 will only count calls lasting longer than 2 minutes as a conversion).

twilio-event-goal

Conclusion

That’s it! Now your phone calls can be seen across any dimension reports you’ve specified in the call tracking script. In the included script, I am tracking Google AdWords campaigns, so I can see Twilio calls in the Campaign report ( Acquisition -> AdWords -> Campaign ) and Channels report ( Acquisition -> Channels ).

You can also track offline advertising channels in this way by setting unique campaign sources, for example “Yellow%20Pages%20Phone%20Book” (the %20 is a special way to send a blank space in the HTTP request). Twilio numbers are very cheap ($1/month), so it is affordable to track how many calls you are getting from various phone books, postcards, mailings, coupon books, etc all within Google Analytics.

If you plan on using more than 5 or so numbers or want to record lots of dimensions, it may be more manageable to set up a MySQL database table and edit the script to pull in the dimension values from there (using phone number as primary key).

In an upcoming article, I will show how to use javascript to dynamically update the phone number on your website by simply adding a parameter to the end of a link to your website. That way, when someone visits your website from, say, an AdWords campaign they will see the special Twilio phone number as they browse your website.

Questions, comments, feedback? Comment below or contact me.

  • david s brooks

    Damian: Excellent post. The tutorial we were looking for. Do you know if these custom events capture the source/medium referral (and other data) that AdWords needs to import goals back into active campaigns? Our use case would be to cycle through an available pool of tracking numbers. When AdWords # gets pulled – the the goal converts (min call duration or IVR response), then a conversion pixel is fired. However, to close the circle, we’d like to reimport goals back into the AdWords account (connected as user).

    I see that you’re planning a follow-up and dynamic number insertion. Can’t wait!

    • http://dbgonz.com Damian Gonzalez

      Hey David, sorry for missing this. This is a new website, thought I would get new comment notifications!

      To answer your question, in step 2 I am setting Campaign Source, Medium, and Name. This is what is unique to each phone number. AdWords may also need the Ad Group set to properly import this conversion from Google Analytics (I am testing this now).

      Send me an email (mail@dbgonz.com) if you would like a copy of the dynamic # insertion script. It is two parts:
      – First, if a new person lands on your website and the destination URL is tagged with a “campaign” parameter (eg. dbgonz.com?campaign=123 ), then the campaign code is stored as a cookie.
      – Second, there is a code that runs each time the page loads. It checks which value is in the cookie to determine which phone number to show. If the cookie is NULL then it will show the default number. I have done this with Javascript and PHP, I prefer the PHP method.

  • gersfirst

    Hi Damian how are you dynamic adding the ppc number, are your ppc ads auto tagged or have you added an extra feild to the end of each ad, i am wondering how i target ppc traffic alone.

    great post also the bit about multiple numbers was a great help

    • http://dbgonz.com Damian Gonzalez

      Hi gersfirst, from the comment on the other post it looks like you found what you need. Let me know if you still need help.