OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / m / g_analytics / ga.php
1 <?php
2
3 /**
4   Copyright 2009 Google Inc. All Rights Reserved.
5 **/
6
7   // Tracker version.
8   define("VERSION", "4.4sh");
9
10   define("COOKIE_NAME", "__utmmobile");
11
12   // The path the cookie will be available to, edit this to use a different
13   // cookie path.
14   define("COOKIE_PATH", "/");
15
16   // Two years in seconds.
17   define("COOKIE_USER_PERSISTENCE", 63072000);
18
19   // 1x1 transparent GIF
20   $GIF_DATA = array(
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)
27   );
28
29   // The last octect of the IP address is removed to anonymize the user.
30   function getIP($remoteAddress) {
31     if (empty($remoteAddress)) {
32       return "";
33     }
34
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";
40     } else {
41       return "";
42     }
43   }
44
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) {
49
50     // If there is a value in the cookie, don't change it.
51     if (!empty($cookie)) {
52       return $cookie;
53     }
54
55     $message = "";
56     if (!empty($guid)) {
57       // Create the visitor id using the guid.
58       $message = $guid . $account;
59     } else {
60       // otherwise this is a new user, create a new random id.
61       $message = $userAgent . uniqid(getRandomNumber(), true);
62     }
63
64     $md5String = md5($message);
65
66     return "0x" . substr($md5String, 0, 16);
67   }
68
69   // Get a random number string.
70   function getRandomNumber() {
71     return rand(0, 0x7fffffff);
72   }
73
74   // Writes the bytes of a 1x1 transparent gif into the response.
75   function writeGifData() {
76     global $GIF_DATA;
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");
82     echo join($GIF_DATA);
83   }
84
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) {
90     $options = array(
91       "http" => array(
92           "method" => "GET",
93           "user_agent" => $_SERVER["HTTP_USER_AGENT"],
94           "header" => ("Accepts-Language: " . $_SERVER["HTTP_ACCEPT_LANGUAGE"]))
95     );
96     if (!empty($_GET["utmdebug"])) {
97       $data = file_get_contents(
98           $utmUrl, false, stream_context_create($options));
99     } else {
100       $data = @file_get_contents(
101           $utmUrl, false, stream_context_create($options));
102     }
103   }
104
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() {
109     $timeStamp = time();
110     $domainName = $_SERVER["SERVER_NAME"];
111     if (empty($domainName)) {
112       $domainName = "";
113     }
114
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
117     // pixel.
118     $documentReferer = $_GET["utmr"];
119     if (empty($documentReferer) && $documentReferer !== "0") {
120       $documentReferer = "-";
121     } else {
122       $documentReferer = urldecode($documentReferer);
123     }
124     $documentPath = $_GET["utmp"];
125     if (empty($documentPath)) {
126       $documentPath = "";
127     } else {
128       $documentPath = urldecode($documentPath);
129     }
130
131     $account = $_GET["utmac"];
132     $userAgent = $_SERVER["HTTP_USER_AGENT"];
133     if (empty($userAgent)) {
134       $userAgent = "";
135     }
136
137     // Try and get visitor cookie from the request.
138     $cookie = $_COOKIE[COOKIE_NAME];
139
140     $guidHeader = $_SERVER["HTTP_X_DCMGUID"];
141     if (empty($guidHeader)) {
142       $guidHeader = $_SERVER["HTTP_X_UP_SUBNO"];
143     }
144     if (empty($guidHeader)) {
145       $guidHeader = $_SERVER["HTTP_X_JPHONE_UID"];
146     }
147     if (empty($guidHeader)) {
148       $guidHeader = $_SERVER["HTTP_X_EM_UID"];
149     }
150
151     $visitorId = getVisitorId($guidHeader, $account, $userAgent, $cookie);
152
153     // Always try and add the cookie to the response.
154     setrawcookie(
155         COOKIE_NAME,
156         $visitorId,
157         $timeStamp + COOKIE_USER_PERSISTENCE,
158         COOKIE_PATH);
159
160     $utmGifLocation = "http://www.google-analytics.com/__utm.gif";
161
162     // Construct the gif hit url.
163     $utmUrl = $utmGifLocation . "?" .
164         "utmwv=" . VERSION .
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"]);
173
174     sendRequestToGoogleAnalytics($utmUrl);
175
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);
180     }
181     // Finally write the gif data to the response.
182     writeGifData();
183   }
184 ?><?php
185   trackPageView();
186 ?>