PageRenderTime 75ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/vendors/ga.php

https://github.com/hashing/basercms
PHP | 193 lines | 131 code | 27 blank | 35 comment | 19 complexity | 14d2185f14afbbaeeee531370f1d18a6 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. Copyright 2009 Google Inc. All Rights Reserved.
  4. **/
  5. // Tracker version.
  6. define("VERSION", "4.4sh");
  7. define("COOKIE_NAME", "__utmmobile");
  8. // The path the cookie will be available to, edit this to use a different
  9. // cookie path.
  10. define("COOKIE_PATH", "/");
  11. // Two years in seconds.
  12. define("COOKIE_USER_PERSISTENCE", 63072000);
  13. // 1x1 transparent GIF
  14. $GIF_DATA = array(
  15. chr(0x47), chr(0x49), chr(0x46), chr(0x38), chr(0x39), chr(0x61),
  16. chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x80), chr(0xff),
  17. chr(0x00), chr(0xff), chr(0xff), chr(0xff), chr(0x00), chr(0x00),
  18. chr(0x00), chr(0x2c), chr(0x00), chr(0x00), chr(0x00), chr(0x00),
  19. chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x00), chr(0x02),
  20. chr(0x02), chr(0x44), chr(0x01), chr(0x00), chr(0x3b)
  21. );
  22. // The last octect of the IP address is removed to anonymize the user.
  23. function getIP($remoteAddress) {
  24. if (empty($remoteAddress)) {
  25. return "";
  26. }
  27. // Capture the first three octects of the IP address and replace the forth
  28. // with 0, e.g. 124.455.3.123 becomes 124.455.3.0
  29. $regex = "/^([^.]+\.[^.]+\.[^.]+\.).*/";
  30. if (preg_match($regex, $remoteAddress, $matches)) {
  31. return $matches[1] . "0";
  32. } else {
  33. return "";
  34. }
  35. }
  36. // Generate a visitor id for this hit.
  37. // If there is a visitor id in the cookie, use that, otherwise
  38. // use the guid if we have one, otherwise use a random number.
  39. function getVisitorId($guid, $account, $userAgent, $cookie) {
  40. // If there is a value in the cookie, don't change it.
  41. if (!empty($cookie)) {
  42. return $cookie;
  43. }
  44. $message = "";
  45. if (!empty($guid)) {
  46. // Create the visitor id using the guid.
  47. $message = $guid . $account;
  48. } else {
  49. // otherwise this is a new user, create a new random id.
  50. $message = $userAgent . uniqid(getRandomNumber(), true);
  51. }
  52. $md5String = md5($message);
  53. return "0x" . substr($md5String, 0, 16);
  54. }
  55. // Get a random number string.
  56. function getRandomNumber() {
  57. return rand(0, 0x7fffffff);
  58. }
  59. // Writes the bytes of a 1x1 transparent gif into the response.
  60. function writeGifData() {
  61. global $GIF_DATA;
  62. header("Content-Type: image/gif");
  63. header("Cache-Control: " .
  64. "private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
  65. header("Pragma: no-cache");
  66. header("Expires: Wed, 17 Sep 1975 21:32:10 GMT");
  67. echo join(array(
  68. chr(0x47), chr(0x49), chr(0x46), chr(0x38), chr(0x39), chr(0x61),
  69. chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x80), chr(0xff),
  70. chr(0x00), chr(0xff), chr(0xff), chr(0xff), chr(0x00), chr(0x00),
  71. chr(0x00), chr(0x2c), chr(0x00), chr(0x00), chr(0x00), chr(0x00),
  72. chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x00), chr(0x02),
  73. chr(0x02), chr(0x44), chr(0x01), chr(0x00), chr(0x3b)
  74. ));
  75. }
  76. // Make a tracking request to Google Analytics from this server.
  77. // Copies the headers from the original request to the new one.
  78. // If request containg utmdebug parameter, exceptions encountered
  79. // communicating with Google Analytics are thown.
  80. function sendRequestToGoogleAnalytics($utmUrl) {
  81. $options = array(
  82. "http" => array(
  83. "method" => "GET",
  84. "user_agent" => $_SERVER["HTTP_USER_AGENT"],
  85. "header" => ("Accepts-Language: " . $_SERVER["HTTP_ACCEPT_LANGUAGE"]))
  86. );
  87. if (!empty($_GET["utmdebug"])) {
  88. $data = file_get_contents(
  89. $utmUrl, false, stream_context_create($options));
  90. } else {
  91. $data = @file_get_contents(
  92. $utmUrl, false, stream_context_create($options));
  93. }
  94. }
  95. // Track a page view, updates all the cookies and campaign tracker,
  96. // makes a server side request to Google Analytics and writes the transparent
  97. // gif byte data to the response.
  98. function trackPageView() {
  99. $timeStamp = time();
  100. $domainName = $_SERVER["SERVER_NAME"];
  101. if (empty($domainName)) {
  102. $domainName = "";
  103. }
  104. // Get the referrer from the utmr parameter, this is the referrer to the
  105. // page that contains the tracking pixel, not the referrer for tracking
  106. // pixel.
  107. $documentReferer = $_GET["utmr"];
  108. if (empty($documentReferer) && $documentReferer !== "0") {
  109. $documentReferer = "-";
  110. } else {
  111. $documentReferer = urldecode($documentReferer);
  112. }
  113. $documentPath = $_GET["utmp"];
  114. if (empty($documentPath)) {
  115. $documentPath = "";
  116. } else {
  117. $documentPath = urldecode($documentPath);
  118. }
  119. $account = $_GET["utmac"];
  120. $userAgent = $_SERVER["HTTP_USER_AGENT"];
  121. if (empty($userAgent)) {
  122. $userAgent = "";
  123. }
  124. // Try and get visitor cookie from the request.
  125. $cookie = @$_COOKIE[COOKIE_NAME];
  126. $guidHeader = @$_SERVER["HTTP_X_DCMGUID"];
  127. if (empty($guidHeader)) {
  128. $guidHeader = @$_SERVER["HTTP_X_UP_SUBNO"];
  129. }
  130. if (empty($guidHeader)) {
  131. $guidHeader = @$_SERVER["HTTP_X_JPHONE_UID"];
  132. }
  133. if (empty($guidHeader)) {
  134. $guidHeader = @$_SERVER["HTTP_X_EM_UID"];
  135. }
  136. $visitorId = getVisitorId($guidHeader, $account, $userAgent, $cookie);
  137. // Always try and add the cookie to the response.
  138. setrawcookie(
  139. COOKIE_NAME,
  140. $visitorId,
  141. $timeStamp + COOKIE_USER_PERSISTENCE,
  142. COOKIE_PATH);
  143. $utmGifLocation = "http://www.google-analytics.com/__utm.gif";
  144. // Construct the gif hit url.
  145. $utmUrl = $utmGifLocation . "?" .
  146. "utmwv=" . VERSION .
  147. "&utmn=" . getRandomNumber() .
  148. "&utmhn=" . urlencode($domainName) .
  149. "&utmr=" . urlencode($documentReferer) .
  150. "&utmp=" . urlencode($documentPath) .
  151. "&utmac=" . $account .
  152. "&utmcc=__utma%3D999.999.999.999.999.1%3B" .
  153. "&utmvid=" . $visitorId .
  154. "&utmip=" . getIP($_SERVER["REMOTE_ADDR"]);
  155. sendRequestToGoogleAnalytics($utmUrl);
  156. // If the debug parameter is on, add a header to the response that contains
  157. // the url that was used to contact Google Analytics.
  158. if (!empty($_GET["utmdebug"])) {
  159. header("X-GA-MOBILE-URL:" . $utmUrl);
  160. }
  161. // Finally write the gif data to the response.
  162. writeGifData();
  163. }
  164. ?><?php
  165. trackPageView();
  166. ?>