Last year, I had a client ask for a simple call tracking solution so that they could see in Google Analytics how many calls they were receiving to their website. I figured I could easily use Twilio to set this up, however, found the resources online lacking a simple solution. Most have created a script, then decided they would charge for that same script. After a few days of working on and off on this project, I found a solution utilizing php-ga that was designed just for this project. Here is how you can easily set this up in under 5 minutes on your website, for FREE.
Step 1: Download php-ga
This is the easiest step. You will need to download php-ga from GitHub by clicking this link. Once downloaded, place the files in “src” on your server. I would suggest placing them in an easy to remember directory. This folder should have autoload.php and then a folder called GoogleAnalytics. Nothing else needs to be done with these files.
Step 2: Create The Handler Script
Once you have added php-ga to your server, you will need to create the handler script to start tracking Twilio calls as Google Analytic events. Here is how to create that script:
// Grab php-ga
// Initialize GA Tracker with client's Analytics info
// Assemble Visitor information
$visitor = new GoogleAnalytics\Visitor();
// Assemble Session information
$session = new GoogleAnalytics\Session();
// Assemble Page information
$page = new GoogleAnalytics\Page('/PhoneCall/');
//Send event with Twilio post data
$event = new GoogleAnalytics\Event();
// Track page view
Be sure to update the script with your Analytics code and then place this file on your server in the folder you created above (same folder as autoload.php).
Step 3: Point Twilio to Your Script
All that is left is to point your Twilio number to your handler.php script. Login to Twilio and select the number you would like to track. With that number, update the voice request URL to be your full URL to handler.php on your server. Make sure that the request type is set to HTTP POST.
All that is left it so simply test your script.
NOTE: PHP-GA No Longer Being Updated
While this script still works, the developer has stopped continued support to this method. Here is what he had to say:
php-ga is no longer maintained as Google finally released an official server-side tracking API: Measurement Protocol! I couldn’t find any well-implemented client library for PHP yet, so feel free to help make php-ga 2.0 a Measurement Protocol PHP client library.