Implement Google Analytics server side without JavaScript to track RSS and XML based content
If you've ever been frustrated at the need for developers to add javascript tracking code, or you simply want to track pages like RSS and mobile pages where javascript is not allowed, then the following solution is for you.
We've done several server side solutions like this, such as the integration of phone calls into Google Analytics directly from the phone routing server (stay tuned for more!). I was reading through some articles researching and i came across an elegant post that i thought i should share:
http://www.vdgraaf.info/google-analytics-without-javascript.html
The PHP code to fake the image request for Google is as follows (also found at http://www.vdgraaf.info/wp-content/uploads/tracker.txt), although a similar technique can be used for Omniture or other platforms. This post is pretty old, but the code should still be fine, the principle is definitely still ok!
Oh and this is also useful if you want to add other information to your Analytics, but you want to keep it from prying eyes! If you find this post useful, you may also want to check out one of our posts on network packet sniffing, which is becoming a serious alternative to javascript page tagging. See http://blog.datalicious.com/atomic-labs-pion-implementing-omniture-withou
For more help with these types of integrations, contact us at insights@datalicious.com.
<?php
$var_utmac='UA-000000-1'; //enter the new urchin code
$var_utmhn='yourdomain.com'; //enter your domain
$var_utmn=rand(1000000000,9999999999); //random request number
$var_cookie=rand(10000000,99999999); //random cookie number
$var_random=rand(1000000000,2147483647); //number under 2147483647
$var_today=time(); //today
$var_referer=$_SERVER['HTTP_REFERER']; //referer url
$var_uservar='-'; //enter your own user defined variable
$var_utmp='tracker/'.$_GET['url'].'.'.$_GET['filetype']; //this example adds a fake file request to the (fake) tracker directory (the image/pdf filename).
$urchinUrl='http://www.google-analytics.com/__utm.gif?utmwv=1&utmn='.$var_utmn.'&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn='.$var_utmhn.'&utmr='.$var_referer.'&utmp='.$var_utmp.'&utmac='.$var_utmac.'&utmcc=__utma%3D'.$var_cookie.'.'.$var_random.'.'.$var_today.'.'.$var_today.'.'.$var_today.'.2%3B%2B__utmb%3D'.$var_cookie.'%3B%2B__utmc%3D'.$var_cookie.'%3B%2B__utmz%3D'.$var_cookie.'.'.$var_today.'.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D'.$var_cookie.'.'.$var_uservar.'%3B';
$handle = fopen ($urchinUrl, "r");
$test = fgets($handle);
fclose($handle);
switch ($_GET['filetype']){
case 'jpg':
header('Content-Type: image/jpeg');
break;
case 'gif':
header('Content-type: image/gif');
break;
case 'pdf':
header('Content-type: application/pdf');
break;
// add your own content types where needed
}
$imageurl = fopen ('http://www.yourdomain.com/'.$_GET['url'].'.'.$_GET['filetype'], "r"); //this is where the real file should be located
while (!feof ($imageurl)) {
$image = fgets($imageurl, 4096);
echo $image;
}
fclose($imageurl);
?>

