4 Copyright 2009 Google Inc. All Rights Reserved.
8 define("VERSION", "4.4sh");
10 define("COOKIE_NAME", "__utmmobile");
12 // The path the cookie will be available to, edit this to use a different
14 define("COOKIE_PATH", "/");
16 // Two years in seconds.
17 define("COOKIE_USER_PERSISTENCE", 63072000);
19 // 1x1 transparent GIF
21 chr(0x47), chr(0x49), chr(0x46), chr(0x38), chr(0x39), chr(0x61),
22 chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x80), chr(0xff),
23 chr(0x00), chr(0xff), chr(0xff), chr(0xff), chr(0x00), chr(0x00),
24 chr(0x00), chr(0x2c), chr(0x00), chr(0x00), chr(0x00), chr(0x00),
25 chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x00), chr(0x02),
26 chr(0x02), chr(0x44), chr(0x01), chr(0x00), chr(0x3b)
29 // The last octect of the IP address is removed to anonymize the user.
30 function getIP($remoteAddress) {
31 if (empty($remoteAddress)) {
35 // Capture the first three octects of the IP address and replace the forth
36 // with 0, e.g. 124.455.3.123 becomes 124.455.3.0
37 $regex = "/^([^.]+\.[^.]+\.[^.]+\.).*/";
38 if (preg_match($regex, $remoteAddress, $matches)) {
39 return $matches[1] . "0";
45 // Generate a visitor id for this hit.
46 // If there is a visitor id in the cookie, use that, otherwise
47 // use the guid if we have one, otherwise use a random number.
48 function getVisitorId($guid, $account, $userAgent, $cookie) {
50 // If there is a value in the cookie, don't change it.
51 if (!empty($cookie)) {
57 // Create the visitor id using the guid.
58 $message = $guid . $account;
60 // otherwise this is a new user, create a new random id.
61 $message = $userAgent . uniqid(getRandomNumber(), true);
64 $md5String = md5($message);
66 return "0x" . substr($md5String, 0, 16);
69 // Get a random number string.
70 function getRandomNumber() {
71 return rand(0, 0x7fffffff);
74 // Writes the bytes of a 1x1 transparent gif into the response.
75 function writeGifData() {
77 header("Content-Type: image/gif");
78 header("Cache-Control: " .
79 "private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
80 header("Pragma: no-cache");
81 header("Expires: Wed, 17 Sep 1975 21:32:10 GMT");
85 // Make a tracking request to Google Analytics from this server.
86 // Copies the headers from the original request to the new one.
87 // If request containg utmdebug parameter, exceptions encountered
88 // communicating with Google Analytics are thown.
89 function sendRequestToGoogleAnalytics($utmUrl) {
93 "user_agent" => $_SERVER["HTTP_USER_AGENT"],
94 "header" => ("Accepts-Language: " . $_SERVER["HTTP_ACCEPT_LANGUAGE"]))
96 if (!empty($_GET["utmdebug"])) {
97 $data = file_get_contents(
98 $utmUrl, false, stream_context_create($options));
100 $data = @file_get_contents(
101 $utmUrl, false, stream_context_create($options));
105 // Track a page view, updates all the cookies and campaign tracker,
106 // makes a server side request to Google Analytics and writes the transparent
107 // gif byte data to the response.
108 function trackPageView() {
110 $domainName = $_SERVER["SERVER_NAME"];
111 if (empty($domainName)) {
115 // Get the referrer from the utmr parameter, this is the referrer to the
116 // page that contains the tracking pixel, not the referrer for tracking
118 $documentReferer = $_GET["utmr"];
119 if (empty($documentReferer) && $documentReferer !== "0") {
120 $documentReferer = "-";
122 $documentReferer = urldecode($documentReferer);
124 $documentPath = $_GET["utmp"];
125 if (empty($documentPath)) {
128 $documentPath = urldecode($documentPath);
131 $account = $_GET["utmac"];
132 $userAgent = $_SERVER["HTTP_USER_AGENT"];
133 if (empty($userAgent)) {
137 // Try and get visitor cookie from the request.
138 $cookie = $_COOKIE[COOKIE_NAME];
140 $guidHeader = $_SERVER["HTTP_X_DCMGUID"];
141 if (empty($guidHeader)) {
142 $guidHeader = $_SERVER["HTTP_X_UP_SUBNO"];
144 if (empty($guidHeader)) {
145 $guidHeader = $_SERVER["HTTP_X_JPHONE_UID"];
147 if (empty($guidHeader)) {
148 $guidHeader = $_SERVER["HTTP_X_EM_UID"];
151 $visitorId = getVisitorId($guidHeader, $account, $userAgent, $cookie);
153 // Always try and add the cookie to the response.
157 $timeStamp + COOKIE_USER_PERSISTENCE,
160 $utmGifLocation = "http://www.google-analytics.com/__utm.gif";
162 // Construct the gif hit url.
163 $utmUrl = $utmGifLocation . "?" .
165 "&utmn=" . getRandomNumber() .
166 "&utmhn=" . urlencode($domainName) .
167 "&utmr=" . urlencode($documentReferer) .
168 "&utmp=" . urlencode($documentPath) .
169 "&utmac=" . $account .
170 "&utmcc=__utma%3D999.999.999.999.999.1%3B" .
171 "&utmvid=" . $visitorId .
172 "&utmip=" . getIP($_SERVER["REMOTE_ADDR"]);
174 sendRequestToGoogleAnalytics($utmUrl);
176 // If the debug parameter is on, add a header to the response that contains
177 // the url that was used to contact Google Analytics.
178 if (!empty($_GET["utmdebug"])) {
179 header("X-GA-MOBILE-URL:" . $utmUrl);
181 // Finally write the gif data to the response.