当前位置:Gxlcms > PHP教程 > 使用GoogleAnalytics来统计手机网站的流量

使用GoogleAnalytics来统计手机网站的流量

时间:2021-07-01 10:21:17 帮助过:45人阅读

使用Google Analytics来统计手机网站的流量

  1. <?php
  2. class GoogleAnlayticsMobile {
  3. private $utma; // utma cookie 记录唯一身份访问者
  4. private $utma_c_time = 63072000; // 两年 (默认情况下是两年)
  5. private $utmb; // utmb cookie 记录用户的一次Session失效时间
  6. private $utmb_c_time = 1800; // 30分钟 (默认情况下是两年)
  7. private $utmc; // utmc cookie 用户退出浏览器后失效
  8. private $utmz; // utmz cookie 主要功能记录访问来源
  9. private $utmz_c_time = 1800; // 30分钟 (默认情况下是半年)
  10. private $ga_utmhn; // 网站域名
  11. private $ga_utmac; // GA 账号
  12. private $ga_utmwv = "4.9.2"; // 谷歌跟踪代码版本
  13. private $ga_hash; // GA域名哈希值
  14. private $ga_img = "http://www.google-analytics.com/utm.gif";
  15. private $ga_search = array(array("baidu","wd"),array("baidu","word")array("google","q"),
  16. array("sogou","q"),array("soso","w"),array("youdao","q"),array("bing","q"),
  17. array("sogou","yahoo"),array("114118","kw"));
  18. private $ga_referer;
  19. // private $remote_address;
  20. private $time;
  21. private $html;
  22. function construct($ga_utmac, $ga_utmhn, $URI = NULL, $ga_params = array()) {
  23. $this->ga_utmac = $ga_utmac;
  24. $this->ga_utmhn = $ga_utmhn;
  25. $this->ga_hash = $this->Hash($ga_utmhn);
  26. $this->time = time();
  27. // 获取请求页面URI
  28. if($URI==NULL) $URI = $_SERVER['REQUEST_URI'];
  29. // 获取引荐网站URL
  30. $this->ga_referer = $_SERVER['HTTP_REFERER'];
  31. // 获取用户IP地址只保存前三位
  32. $this->remote_address = $_SERVER['REMOTE_ADDR'];
  33. // 获取流量来源
  34. $source = $this->GetTrafficSource();
  35. if($source["utmgclid"]!="") $source_str = "utmgclid=".$source["utmgclid"];
  36. else $source_str = "utmcsr=".$source["utmcsr"];
  37. $source_str .= "|utmccn=".$source["utmccn"]."|utmcmd=".$source["utmcmd"];
  38. if($source["utmctr"]!="") $source_str .= "|utmctr=".$source["utmctr"];
  39. if($source["utmcct"]!="") $source_str .= "|utmcct=".$source["utmcct"];
  40. // Set all extra parameters like screen resolution, color depth
  41. if(is_array($ga_params)) foreach ($ga_params as $key => $value)
  42. $ga_set_params .= "&".$key."=".rawurlencode($value);
  43. else $ga_set_params = "";
  44. // 检查"utma"是否存在
  45. if(isset($_COOKIE["utma"])) {
  46. $this->utma = $_COOKIE["utma"];
  47. $this->utmb = $_COOKIE["utmb"];
  48. $this->utmz = $_COOKIE["utmz"];
  49. $utmb = split("\.",$this->utmb);
  50. if(strpos($this->utmz,"utmgclid")>-1) $pos = strpos($this->utmz,"utmgclid");
  51. else $pos = strpos($this->utmz,"utmcsr");
  52. $utmz = split("\.",substr($this->utmz,0,$pos));
  53. $utmz[4] = substr($this->utmz,$pos);
  54. $utma = split("\.",$this->utma);
  55. // 检查"utmc"是否存在,如果不存在,更新"utma"中的访问次数。
  56. if(!isset($_COOKIE["utmc"])) {
  57. // 增加访问次数
  58. $utma[5] = $utma[5]+1;
  59. // 更新访问时间
  60. $utma[3] = $utma[4];
  61. $utma[4] = $this->time;
  62. // 保存Cookies
  63. $this->utma = join(".",$utma);
  64. setcookie("utma", $this->utma, $this->time+$this->utma_c_time, "/", ".".$this->ga_utmhn);
  65. setcookie("utmc", $utma[0], 0, "/", ".".$this->ga_utmhn);
  66. // 生成"utmb"或更新"utmb"中的访问页数。
  67. if(isset($_COOKIE["utmb"])) $utmb[1] = 1;
  68. else $utmb = array($utma[0], 1, 10, $this->time);
  69. }
  70. else $utmb[1] = $utmb[1]+1; // 在"utmb"中添加访问页数
  71. // 更新流量来源
  72. if($utmz[4]!=$source_str && $source["utmcsr"]!="(direct)")
  73. $utmz = array($utmz[0], $this->time, $utma[5], $utmz[3]+1, $source_str);
  74. // 保存 "utmb" and "utmz"
  75. $this->utmb = join(".",$utmb);
  76. setcookie("utmb", $this->utmb, $this->time+$this->utmb_c_time, "/", ".".$this->ga_utmhn);
  77. $this->utmz = join(".",$utmz);
  78. setcookie("utmz", $this->utmz, $this->time+$this->utmz_c_time, "/", ".".$this->ga_utmhn);
  79. }
  80. else {
  81. $c_id = sprintf("%f", (rand(1000000000,2147483647) ^ $this->ga_hash) * 2147483647);
  82. $c_id = split("\.",$c_id);
  83. $this->utma = $this->ga_hash.".".$c_id[0].".".$this->time.".".$this->time.".".$this->time.".1";
  84. $this->utmb = $this->ga_hash.".1.10.".$this->time;
  85. $this->utmc = $this->ga_hash;
  86. $this->utmz = $this->ga_hash.".".$this->time.".1.1.".$source_str;
  87. setcookie("utma", $this->utma, $this->time+$this->utma_c_time, "/", ".".$this->ga_utmhn);
  88. setcookie("utmb", $this->utmb, $this->time+$this->utmb_c_time, "/", ".".$this->ga_utmhn);
  89. setcookie("utmc", $this->utmc, 0, "/", ".".$this->ga_utmhn);
  90. setcookie("utmz", $this->utmz, $this->time+$this->utmz_c_time, "/", ".".$this->ga_utmhn);
  91. }
  92. // 发送页面图片请求
  93. $this->html .= "<img src=\"".$this->ga_img.
  94. "?utmwv=".$this->ga_utmwv.
  95. "&utmn=".rand(1000000000,9999999999).
  96. "&utmhn=".$this->ga_utmhn."".$ga_set_params.
  97. "&utmhid=".rand(1000000000,9999999999).
  98. "&utmr=".rawurlencode($this->ga_referer).
  99. "&utmp=".rawurlencode($URI).
  100. // "&utmip=".rawurlencode($this->remote_address).
  101. "&utmac=".$this->ga_utmac."&utmcc=utma%3D".$this->utma."%3B%2Butmz%3D".
  102. rawurlencode($this->utmz)."%3B\" width=\"1\" height=\"1\" />\n";
  103. }
  104. //哈希算法
  105. function Hash($d) {
  106. if(!$d || $d=="") return 1;
  107. $h=0; $g=0;
  108. for($i=strlen($d)-1;$i>=0;$i--) {
  109. $c = (int)(ord($d[$i]));
  110. $h = (($h << 6) & 0xfffffff) + $c + ($c << 14);
  111. $g = ($h & 0xfe00000);
  112. if($g!=0) $h = ($h ^ ($g >> 21));
  113. }
  114. return $h;
  115. }
  116. function GetTrafficSource() {
  117. if(isset($_GET["utm_source"]) && isset($_GET["utm_medium"])) {
  118. // 链接中设置了来源
  119. $utmccn = isset($_GET["utm_campaign"]) ? $_GET["utm_campaign"] : "(not set)";
  120. $utmcct = isset($_GET["utm_content"]) ? $_GET["utm_content"] : "(not set)";
  121. return array("utmgclid"=>"", "utmcsr"=>$_GET["utm_source"], "utmccn"=>$utmccn,
  122. "utmcmd"=>$_GET["utm_medium"], "utmctr"=>$_GET["utm_term"], "utmcct"=>$utmcct);
  123. }
  124. else if($this->ga_referer!="") {
  125. // 从引荐网站过来的流量
  126. $search_engine = $this->GetSearchEngine();
  127. // 判断是否是搜索引擎
  128. if($search_engine) return $search_engine;
  129. else if(!isset($_COOKIE["utmc"])) {
  130. // 如果是新用户或不是搜索引擎,设置referer
  131. $ref = $this->GetReferer();
  132. if(substr($ref["host"],0,4)=="www.") $ref["host"] = substr($ref["host"],4); // Remove www from URL
  133. return array("utmgclid"=>"", "utmcsr"=>$ref["host"], "utmccn"=>"(referral)",
  134. "utmcmd"=>"referral", "utmctr"=>"", "utmcct"=>$ref["uri"]);
  135. }
  136. }
  137. return array("utmgclid"=>"", "utmcsr"=>"(direct)", "utmccn"=>"(direct)",
  138. "utmcmd"=>"(none)", "utmctr"=>"", "utmcct"=>"");
  139. }
  140. function GetSearchEngine() {
  141. $ref = $this->GetReferer();
  142. for($ii=0;$ii<count($this->ga_search);$ii++) {
  143. if(strpos(strtolower($ref["host"]), strtolower($this->ga_search[$ii][0]))>-1) {
  144. $test1 = strpos($ref["referer"], "?".$this->ga_search[$ii][1]."=");
  145. $test2 = strpos($ref["referer"], "&".$this->ga_search[$ii][1]."=");
  146. $i = ($test1 > -1) ? $test1 : $test2;
  147. if($i>-1) {
  148. $k = substr($ref["referer"], $i+strlen($this->ga_search[$ii][1])+2, strlen($ref["referer"]));
  149. $i = strpos($k,"&");
  150. if($i > -1) $k = substr($k,0,$i);
  151. if(isset($_GET["gclid"])) return array("utmgclid"=>$_GET["gclid"],
  152. "utmcsr"=>"", "utmccn"=>"(not set)", "utmcmd"=>"(not set)", "utmctr"=>$k, "utmcct"=>"");
  153. else return array("utmgclid"=>"", "utmcsr"=>$this->ga_search[$ii][0],
  154. "utmccn"=>"(organic)", "utmcmd"=>"organic", "utmctr"=>$k, "utmcct"=>"");
  155. }
  156. }
  157. }
  158. return false;
  159. }
  160. function GetReferer() {
  161. $referer_tmp = $this->ga_referer;
  162. $pos = strpos($referer_tmp, "://");
  163. if($pos>0) $referer_tmp = $referer_tmp = substr($referer_tmp,$pos+3);
  164. $pos = strpos($referer_tmp, "/");
  165. if($pos>0) return array("host"=>substr($referer_tmp, 0, $pos),
  166. "uri"=>substr($referer_tmp, $pos), "referer"=>$this->ga_referer);
  167. else return array("host"=>$referer_tmp, "uri"=>"", "referer"=>$this->ga_referer);
  168. }
  169. function SetTransaction($order_id, $amount, $shipping, $tax, $city, $region, $country) {
  170. // 交易记录
  171. $this->html .= "<img src=\"".$this->ga_img."?utmwv=".$this->ga_utmwv.
  172. "&utmn=".rand(1000000000,9999999999)."&utmhn=".$this->ga_utmhn.
  173. "&utmt=tran&utmtid=".$order_id."&utmtto=".$amount."&utmttx=".$tax.
  174. "&utmtsp=".$shipping."&utmtci=".rawurlencode($city)."&utmtrg=".rawurlencode($region).
  175. "&utmtco=".rawurlencode($country)."&utmac=".$this->ga_utmac."&utmcc=utma%3D".
  176. $this->utma."%3B%2Butmz%3D".rawurlencode($this->utmz)."%3B\" width=\"1\" height=\"1\" />\n";
  177. }
  178. function SetTransactionItem($order_id, $item_id, $category, $name, $price, $quantity) {
  179. // 交易商品记录
  180. $this->html .= "<img src=\"".$this->ga_img."?utmwv=".$this->ga_utmwv.
  181. "&utmn=".rand(1000000000,9999999999)."&utmhn=".$this->ga_utmhn.
  182. "&utmt=item&utmtid=".$order_id."&utmipc=".$item_id."&utmipn=".rawurlencode($name).
  183. "&utmiva=".rawurlencode($category)."&utmipr=".$price."&utmiqt=".$quantity."&utmac=".
  184. $this->ga_utmac."&utmcc=utma%3D".$this->utma."%3B%2Butmz%3D".rawurlencode($this->utmz)."%3B\"
  185. width=\"1\" height=\"1\" />\n";
  186. }
  187. function GetTrackingCode() {
  188. // 执行跟踪程序
  189. return $this->html;
  190. }
  191. }

以上就是使用Google Analytics来统计手机网站的流量的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行