I have been working with some PHP
sites lately, and considering I'm a .NET kind of guy, have actually
(surprisingly?) enjoyed the experience :)
One of the cool new platforms I found was Piwik, the open source analytics
platform that is designed to be a competitor to Google Analytics (GA).
Now, I am a fan of GA, but in this case, we needed to own the stats
data, and do a lot of custom analysis and modelling on the data, so
I had to choose something where we could host it, and that's where
Piwik came in.
It was really easy to install and I got it
up and running in no time. The plug-in framework is awesome;
extremely powerful and flexible. Its easy to
build and deploy your own plug-ins, and the API to access the
data is clean, and so far, very fast.
To deploy on each site, it is as simple as GA - just add a JavaScript
tracking code. But I have a case where the client couldn't use
assets from 3rd party sites. The JavaScript tracking code
effectively generates an IMG (image) tag in HTML, and their legal
people wouldn't allow it.
So I created the proxy class below to allow the browser request
to be sent to a file hosted on the clients site, and in turn, this
proxy will hand off the request and all important information back
to the stats server we are running. Not sure if anyone else will
need it, but thought I would share anyway :)
<?php
// DEBUGGING - comment out when live
// error_reporting(E_ALL);
// ini_set('display_errors','1');
// Our vars
$url = 'http://statsserver.com/piwik/piwik.php?';
$referer = "";
$ua = "";
$lang = "";
$cookie = "";
$ip = "0.0.0.0";
// Build up URI from query string - need encode the value
foreach($_GET as $key => $value)
{
$url .= $key . "=" . urlencode($value) .
"&";
}
// Now set some headers
if (isset($_SERVER['HTTP_REFERER']))
$referer = $_SERVER['HTTP_REFERER'];
if (isset($_SERVER['HTTP_USER_AGENT']))
$ua = $_SERVER['HTTP_USER_AGENT'];
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (isset($_SERVER['REMOTE_ADDR']))
$ip = $_SERVER['REMOTE_ADDR'];
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-Language: " . $lang . "\r\n" .
"User-Agent: " . $ua . "\r\n" .
"Referer: " . $referer . "\r\n" .
"X_Forwarded_For: " . $ip . "\r\n"
)
);
$headers = stream_context_create($opts);
// Send the request
$contents = file_get_contents($url, FILE_BINARY, $headers);
// Find the cookie from the response and pass back to the
client
$nlines = count($http_response_header);
for ($i = $nlines-1; $i >= 0; $i--)
{
$line = $http_response_header[$i];
if (substr_compare($line, 'Set-Cookie', 0, 10,
true) == 0)
{
$cookie = $line;
break;
}
}
header('Set-Cookie: ' . $cookie);
header('Content-Type: image/gif');
header('Pragma: no-cache');
header('Cache-Control: private, no-cache, proxy-revalidate');
echo $contents;
?>