Posted By

nfreear on 07/20/10


Tagged

OLnet Piwik nojsstats


Versions (?)

No-JS redirect to Piwik/ Google Analytics webbug


 / Published in: PHP
 

URL: http://olnet.org/node/149

A solution to tracking for example RSS feed usage - no Javascript required. * Note, an Apache .htaccess/httpd.conf edit is required - see code comment. * Example usage - see code comment. * Inspired by, nojsstats.appspot.com (Part of the OLnet/ OER tracking project.)

  1. <?php
  2. /**
  3.   Redirect to a Piwik webbug (1 pixel image), or the Google-Analytics one.
  4.   OLnet.org / OER tracking project.
  5.   @copyright 2010 The Open University.
  6.  
  7.   Usage - Piwik - title=X, r=referrer are optional,
  8.   <img alt="" src="http://localhost/track/PI-1/example.org/path/to/123?title=My+Title" />
  9.  
  10.   Usage - Google-Analytics,
  11.   <img alt="" src="http://localhost/track/UA-1234-5/example.org/path/to/123?title=My+Title&r=http%3A//referer.example.com/" />
  12.  
  13.  
  14.   An Apache .htaccess/httpd.conf edit is required:
  15.  
  16.   <IfModule mod_rewrite.c>
  17.   RewriteEngine on
  18.   # If the file/dir is NOT real go to index
  19.   RewriteCond %{REQUEST_FILENAME} !-f
  20.   RewriteCond %{REQUEST_FILENAME} !-d
  21.   RewriteRule ^(.*)$ index.php/$1 [QSA,L]
  22.   </IfModule>
  23.   <IfModule !mod_rewrite.c>
  24.   # If mod_rewrite is NOT installed go to index.php
  25.   ErrorDocument 404 index.php
  26.   </IfModule>
  27.  
  28. */
  29. // Substitute your local Piwik here.
  30. #define('PIWIK_TRACKER', "http://localhost/my_piwik/piwik.php");
  31. define('PIWIK_TRACKER', "http://piwik.org/demo/piwik.php");
  32. define('GOOGLE_TRACKER', "http://www.google-analytics.com/__utm.gif");
  33.  
  34. date_default_timezone_set('Europe/London');#Prevent warnings.
  35.  
  36. $local_url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  37. $path = parse_url($local_url, PHP_URL_PATH);
  38.  
  39. // Optional Referrer/title - use parse_url for crude parsing.
  40. $referer = isset($_GET['r']) && 'array'==gettype(@parse_url($_GET['r'])) ? $_GET['r'] : NULL;
  41. $referer = !$referer && isset($_SERVER['HTTP_REFERER']) ? isset($_SERVER['HTTP_REFERER']) : $referer;
  42. $title = isset($_GET['title']) ? $_GET['title'] : NULL;
  43.  
  44. $request_string = NULL;
  45. if (preg_match('#/PI-(\d+?)/(.+)$#', $path, $matches)) {
  46. // Piwik
  47. $request_string = piwik_analytics_bug_url($matches, $title, $referer);
  48. }
  49. elseif (preg_match('#/(UA-\d+?(-\d+)?)/(.+)$#', $path, $matches)) {
  50. // Google/Urchin?
  51. $request_string = google_analytics_bug_url($matches, $title, $referer);
  52. }
  53. //ELSE: Yahoo?
  54.  
  55. // Error.
  56. if (!$request_string) {
  57. @header("HTTP/1.1 400", TRUE, 400);
  58. die("Error, bad request.");
  59. }
  60.  
  61. // OK, do the redirect to the web bug (temporary).
  62. @header("HTTP/1.1 302 Moved", TRUE, 302);
  63. header("Location: $request_string"); exit;
  64.  
  65. echo " DEBUG, redirect: $request_string ";
  66.  
  67.  
  68. /* Based on,
  69.  http://dev.piwik.org/svn/trunk/core/Tracker/Visit.php
  70.  http://www.burtonkent.com/wp-content/uploads/piwik-tag.php
  71. */
  72. function piwik_analytics_bug_url($matches, $title=NULL, $referer=NULL) {
  73. // non-Javascript solution can't get screen-resolution, Flash, Java, other plugin capabilities.
  74. $request = array();
  75.  
  76. # idsite - Piwik site ID.
  77. $request['idsite'] = $matches[1];
  78.  
  79. # url - Requested URL.
  80. $scheme = 80==$_SERVER['SERVER_PORT'] ? 'http://' : 'https://';
  81. $request['url'] = $scheme . $matches[2];
  82.  
  83. # urlref - Referrer.
  84. $request['urlref'] = $referer;
  85.  
  86. # title, action_name?
  87. $request['title'] = $title;
  88. $request['action_name']= $title;
  89.  
  90. # cookie - Are cookies enabled?
  91. /*if (isset($_SERVER['HTTP_COOKIE']) && $_SERVER['HTTP_COOKIE'] != '') {
  92.   $request['cookie'] = 1;
  93.   }
  94.   else {
  95.   $request['cookie'] = 0; # or possibly not set? Hmm, not convinced!
  96.   }*/
  97.  
  98. # (d,) h, m, s - hours, minutes, seconds (, days?)
  99. list($request['d'], $request['h'], $request['m'], $request['s'])
  100. = explode('|', date('d|H|i|s', $_SERVER['REQUEST_TIME']));
  101.  
  102. # rand - random number - quick 17 precision random number.
  103. $request['rand'] = '0.' . mt_rand(0, mt_getrandmax());
  104.  
  105. # rec - record? 1 by default
  106. $request['rec'] = 1;
  107.  
  108. return PIWIK_TRACKER .'?'. http_build_query($request);
  109. }
  110.  
  111.  
  112. /* Based on http://nojsstats.blogspot.com/ */
  113. function google_analytics_bug_url($matches, $title=NULL, $referer=NULL) {
  114. $request = array('utmwv'=>1, 'utmsr'=>'-', 'utmsc'=>'-', 'utmul'=>'-',
  115. 'utmje'=>'0', 'utmfl'=>'-', 'utmjv'=>'-'); //'hid' Another number?
  116.  
  117. $request['utmac'] = $matches[1];
  118.  
  119. $p = parse_url('http://'.$matches[3]);
  120. $request['utmhn'] = $p['host'];
  121. $request['utmp' ] = $p['path']. (isset($p['query']) ? '?'.$p['query'] : '');
  122.  
  123. $request['utmdt'] = $title;
  124. $request['utmr' ] = $referer;
  125.  
  126. # utmn - Random number?
  127. $request['utmn' ] = mt_rand(0, mt_getrandmax());
  128.  
  129. return GOOGLE_TRACKER .'?'. http_build_query($request);
  130. }

Report this snippet  

You need to login to post a comment.