/

  / 

Google Analytics Measurement Protocol for PHP – Campaign Tracking

I'm going to show you my PHP implementation of the Google Analytics Measurement Protocol. I'm using this implementation to track campaigns. What I'm using this for is to track phone calls, which I'll discuss in a later post. FYI: I am not a programmer. If you're a real programmer and you see opportunities for improving the script, your suggestions are welcome.

Universal Analytics

Before we look at the script, make sure you've set up a Universal Analytics property. Analytics web properties that were created before the release of the Universal Analytics public Beta will not work with the measurement protocol. So, if you haven't yet created a Universal Analytics property, do it now.

Required Parameters

<?php
function gen_uuid() { // Generates a UUID. A UUID is required for the measurement protocol.
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}

$data = array( // This is an associative array that will contain all the parameters that we'll send to Google Analytics
'v' => 1, // The version of the measurement protocol
'tid' => $_REQUEST['ga_acc_id']; // Google Analytics account ID (UA-98765432-1)
'cid' => gen_uuid(), // The UUID
't' => 'pageview' // Hit Type
);
?>
 

Campaign Parameters

I'll be passing the campaign data via URL. So, the first thing I need to do is grab that data and get it ready to be sent to Google Analytics.
<?php
$data['dh'] = (isset($_REQUEST['domain']) ? $_REQUEST['domain'] : ""); // The domain of the site that is associated with the Google Analytics ID
$data['dl'] = (isset($_REQUEST['path']) ? $_REQUEST['path'] : ""); // The landing page
$data['dr'] = $_SERVER['HTTP_REFERER']; // The URL of the site that is sending the visit. Format: http%3A%2F%2Fexample.com
$data['dp'] = (isset($_REQUEST['path']) ? $_REQUEST['path'] : ""); // The page that will receive the pageview
$data['dt'] = (isset($_REQUEST['page_title']) ? $_REQUEST['page_title'] : ""); // The title of the page that receives the pageview. In my case, this is a "virtual" page. So, I'm passing the title through the URL.
$data['cs'] = (isset($_REQUEST['utm_source']) ? $_REQUEST['utm_source'] : ""); // The source of the visit (e.g. google)
$data['cm'] = (isset($_REQUEST['utm_medium']) ? $_REQUEST['utm_medium'] : ""); // The medium (e.g. cpc)
$data['cn'] = (isset($_REQUEST['utm_campaign']) ? $_REQUEST['utm_campaign'] : ""); // The name of the campaign
$data['ck'] = (isset($_REQUEST['utm_term']) ? $_REQUEST['utm_term'] : ""); // The keyword that the user searched for
$data['cc'] = (isset($_REQUEST['utm_content']) ? $_REQUEST['utm_content'] : ""); // Used to differentiate ads or links that point to the same URL.
?>
 

The HTTP Request Variables

<?php
$url = 'http://www.google-analytics.com/collect'; // This is the URL to which we'll be sending the post request.
$content = http_build_query($data); // The body of the post must include exactly 1 URI encoded payload and must be no longer than 8192 bytes. See http_build_query.
$content = utf8_encode($content); // The payload must be UTF-8 encoded.
$user_agent = 'TargetClickCallTracker/1.0 (http://targetclickmarketing.com/)'; // Throwing in a user agent just for good measure.
?>
 

Send the HTTP POST Request

I'm using cURL to send the post request.
<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_exec($ch);
curl_close($ch);
?>
 

Executing the Measurement Protocol Script

As I mentioned earlier, we're using this for our call tracking system. The call tracking system posts call data to the URL of our choice. Now with the measurement protocol and this script, we can send to Google Analytics all kinds of great data about phone calls. We just load up a URL like:
http://targetclickmarketing.com/mp?ga_acc_id=UA-98765432-1&domain=whatever.com&path=%2Fphonecall&page_title=Phone%20Call&utm_source=google&utm_medium=cpc