PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/googleanalyticsformobile/php/ga.php

http://github.com/h5bp/mobile-boilerplate
PHP | 176 lines | 115 code | 26 blank | 35 comment | 16 complexity | 6ef6f12a209f19fbcba6defece59675d MD5 | raw file
Possible License(s): Apache-2.0
  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($GIF_DATA);
  68. }
  69. // Make a tracking request to Google Analytics from this server.
  70. // Copies the headers from the original request to the new one.
  71. // If request containg utmdebug parameter, exceptions encountered
  72. // communicating with Google Analytics are thown.
  73. function sendRequestToGoogleAnalytics($utmUrl) {
  74. $options = array(
  75. "http" => array(
  76. "method" => "GET",
  77. "user_agent" => $_SERVER["HTTP_USER_AGENT"],
  78. "header" => ("Accepts-Language: " . $_SERVER["HTTP_ACCEPT_LANGUAGE"]))
  79. );
  80. if (!empty($_GET["utmdebug"])) {
  81. $data = file_get_contents(
  82. $utmUrl, false, stream_context_create($options));
  83. } else {
  84. $data = @file_get_contents(
  85. $utmUrl, false, stream_context_create($options));
  86. }
  87. }
  88. // Track a page view, updates all the cookies and campaign tracker,
  89. // makes a server side request to Google Analytics and writes the transparent
  90. // gif byte data to the response.
  91. function trackPageView() {
  92. $timeStamp = time();
  93. $domainName = $_SERVER["SERVER_NAME"];
  94. if (empty($domainName)) {
  95. $domainName = "";
  96. }
  97. // Get the referrer from the utmr parameter, this is the referrer to the
  98. // page that contains the tracking pixel, not the referrer for tracking
  99. // pixel.
  100. $documentReferer = $_GET["utmr"];
  101. if (empty($documentReferer) && $documentReferer !== "0") {
  102. $documentReferer = "-";
  103. } else {
  104. $documentReferer = urldecode($documentReferer);
  105. }
  106. $documentPath = $_GET["utmp"];
  107. if (empty($documentPath)) {
  108. $documentPath = "";
  109. } else {
  110. $documentPath = urldecode($documentPath);
  111. }
  112. $account = $_GET["utmac"];
  113. $userAgent = $_SERVER["HTTP_USER_AGENT"];
  114. if (empty($userAgent)) {
  115. $userAgent = "";
  116. }
  117. // Try and get visitor cookie from the request.
  118. $cookie = $_COOKIE[COOKIE_NAME];
  119. $visitorId = getVisitorId(
  120. $_SERVER["HTTP_X_DCMGUID"], $account, $userAgent, $cookie);
  121. // Always try and add the cookie to the response.
  122. setrawcookie(
  123. COOKIE_NAME,
  124. $visitorId,
  125. $timeStamp + COOKIE_USER_PERSISTENCE,
  126. COOKIE_PATH);
  127. $utmGifLocation = "http://www.google-analytics.com/__utm.gif";
  128. // Construct the gif hit url.
  129. $utmUrl = $utmGifLocation . "?" .
  130. "utmwv=" . VERSION .
  131. "&utmn=" . getRandomNumber() .
  132. "&utmhn=" . urlencode($domainName) .
  133. "&utmr=" . urlencode($documentReferer) .
  134. "&utmp=" . urlencode($documentPath) .
  135. "&utmac=" . $account .
  136. "&utmcc=__utma%3D999.999.999.999.999.1%3B" .
  137. "&utmvid=" . $visitorId .
  138. "&utmip=" . getIP($_SERVER["REMOTE_ADDR"]);
  139. sendRequestToGoogleAnalytics($utmUrl);
  140. // If the debug parameter is on, add a header to the response that contains
  141. // the url that was used to contact Google Analytics.
  142. if (!empty($_GET["utmdebug"])) {
  143. header("X-GA-MOBILE-URL:" . $utmUrl);
  144. }
  145. // Finally write the gif data to the response.
  146. writeGifData();
  147. }
  148. ?><?php
  149. trackPageView();
  150. ?>