PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/ga.php

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