First time I heard about Measurement Protocol (MP) from Paweł Matkowski  few days after Universal Analytics (UA) was launched in Poland. That was one of Meet Google breakfasts in 2013. During that meeting Paweł showed us how work time can be measured in… google analytics (GA). All you need is GA with MP and some time and knowledge. That was interesting but in fact I didn’t have time to do anything with that… Stupid me…

In October 2014 I took part in “Think Analytics 2014”, event organised by Google in Warsaw (page doesn’t exist any more). One of the speakers was Piotrek Rudnicki from Google who showed us his “google analytics car” project.

His idea was simple – everything can be measured – even your daily car travels.

He connected 5 things together:

  1. car (yes, he did)
  2. raspberry pi computer
  3. GPS chip
  4. google maps API
  5. google analytics (with measurement protocol)
Piotr Rudnicki
Piotr Rudnicki and his analytics car

I was fascinated how everything can be connected together, fascinated so much that I bought my own rasperry pi however not so determined to buy GPS module and… start creating my own project.

That’s my problem: in the begining everything seems to be so cool but then there is always ‘yes, but…’ On the other hand I am very lazy so boring and repetitive tasks  make me sick.  Everybody who is working in corporation knows that those kind of task are our bosses favourites.

That was a reason I decided to start my adventure with measurement protocol ones again. Because if everyting can be measured it meens everything. 

The most important things about Measurement Protocol.

  1. if anything can be expressed in numbers – it can be measured
  2. we can send all numbers to google analytics
  3. it’s all about one link – yes, all you have to do is to understand how measurment protocol link is built

All you have to remember about is:

  • send POST request

[php]
POST /collect HTTP/1.1
Host: www.google-analytics.com
[/php]

  • add payload_data with required parameters

[php]
v=1 // Version.
&tid=UA-XXXX-Y // Tracking ID / Property ID.
&cid=555 // Anonymous Client ID.
&t= // Hit Type.
[/php]

  • add desired hits (for events tracking)

[php]
v=1 // Version.
&tid=UA-XXXX-Y // Tracking ID / Property ID.
&cid=555 // Anonymous Client ID.

&t=event // Event hit type
&ec=video // Event Category. Required.
&ea=play // Event Action. Required.
&el=holiday // Event label.
&ev=300 // Event value.
[/php]

Creating payload_data in php:

[php]
function payloadData($v, $tid, $cid, $t, $ec, $ea, $el, $ev){
$data = array(
‘v’ => $v,
‘tid’ => $tid,
‘cid’ => $cid,
‘t’ => $t,
‘ec’ => $ec,
‘ea’ => $ea,
‘el’ => $el,
‘ev’ => $ev
);
$payloadData = http_build_query($data);
return($payloadData);
}
[/php]

Fire!

[php]
$url = ‘http://www.google-analytics.com/collect’;
$payloadData = payloadData($v, $tid, $cid, $t, $ec, $ea, $el, $ev);

$options = array(
‘header’ => "Content-type: application/x-www-form-urlencoded\r\n",
‘method’ => ‘POST’,
‘content’ => $payloadData,
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
[/php]

Your first hit sent via Measurement Protocol has already arrived to google analytics! Now it’s high time to start creating new sets of data which can be quite easily put in your reports.

Measurement Protocol hit
Measurement Protocol hit

Leave a comment

Your email address will not be published. Required fields are marked *