时间:2021-07-01 10:21:17 帮助过:45人阅读
- <?php
- class GoogleAnlayticsMobile {
- private $utma; // utma cookie 记录唯一身份访问者
- private $utma_c_time = 63072000; // 两年 (默认情况下是两年)
- private $utmb; // utmb cookie 记录用户的一次Session失效时间
- private $utmb_c_time = 1800; // 30分钟 (默认情况下是两年)
- private $utmc; // utmc cookie 用户退出浏览器后失效
- private $utmz; // utmz cookie 主要功能记录访问来源
- private $utmz_c_time = 1800; // 30分钟 (默认情况下是半年)
- private $ga_utmhn; // 网站域名
- private $ga_utmac; // GA 账号
- private $ga_utmwv = "4.9.2"; // 谷歌跟踪代码版本
- private $ga_hash; // GA域名哈希值
- private $ga_img = "http://www.google-analytics.com/utm.gif";
- private $ga_search = array(array("baidu","wd"),array("baidu","word")array("google","q"),
- array("sogou","q"),array("soso","w"),array("youdao","q"),array("bing","q"),
- array("sogou","yahoo"),array("114118","kw"));
- private $ga_referer;
- // private $remote_address;
- private $time;
- private $html;
- function construct($ga_utmac, $ga_utmhn, $URI = NULL, $ga_params = array()) {
- $this->ga_utmac = $ga_utmac;
- $this->ga_utmhn = $ga_utmhn;
- $this->ga_hash = $this->Hash($ga_utmhn);
- $this->time = time();
- // 获取请求页面URI
- if($URI==NULL) $URI = $_SERVER['REQUEST_URI'];
- // 获取引荐网站URL
- $this->ga_referer = $_SERVER['HTTP_REFERER'];
- // 获取用户IP地址只保存前三位
- $this->remote_address = $_SERVER['REMOTE_ADDR'];
- // 获取流量来源
- $source = $this->GetTrafficSource();
- if($source["utmgclid"]!="") $source_str = "utmgclid=".$source["utmgclid"];
- else $source_str = "utmcsr=".$source["utmcsr"];
- $source_str .= "|utmccn=".$source["utmccn"]."|utmcmd=".$source["utmcmd"];
- if($source["utmctr"]!="") $source_str .= "|utmctr=".$source["utmctr"];
- if($source["utmcct"]!="") $source_str .= "|utmcct=".$source["utmcct"];
- // Set all extra parameters like screen resolution, color depth
- if(is_array($ga_params)) foreach ($ga_params as $key => $value)
- $ga_set_params .= "&".$key."=".rawurlencode($value);
- else $ga_set_params = "";
- // 检查"utma"是否存在
- if(isset($_COOKIE["utma"])) {
- $this->utma = $_COOKIE["utma"];
- $this->utmb = $_COOKIE["utmb"];
- $this->utmz = $_COOKIE["utmz"];
- $utmb = split("\.",$this->utmb);
- if(strpos($this->utmz,"utmgclid")>-1) $pos = strpos($this->utmz,"utmgclid");
- else $pos = strpos($this->utmz,"utmcsr");
- $utmz = split("\.",substr($this->utmz,0,$pos));
- $utmz[4] = substr($this->utmz,$pos);
- $utma = split("\.",$this->utma);
- // 检查"utmc"是否存在,如果不存在,更新"utma"中的访问次数。
- if(!isset($_COOKIE["utmc"])) {
- // 增加访问次数
- $utma[5] = $utma[5]+1;
- // 更新访问时间
- $utma[3] = $utma[4];
- $utma[4] = $this->time;
- // 保存Cookies
- $this->utma = join(".",$utma);
- setcookie("utma", $this->utma, $this->time+$this->utma_c_time, "/", ".".$this->ga_utmhn);
- setcookie("utmc", $utma[0], 0, "/", ".".$this->ga_utmhn);
- // 生成"utmb"或更新"utmb"中的访问页数。
- if(isset($_COOKIE["utmb"])) $utmb[1] = 1;
- else $utmb = array($utma[0], 1, 10, $this->time);
- }
- else $utmb[1] = $utmb[1]+1; // 在"utmb"中添加访问页数
- // 更新流量来源
- if($utmz[4]!=$source_str && $source["utmcsr"]!="(direct)")
- $utmz = array($utmz[0], $this->time, $utma[5], $utmz[3]+1, $source_str);
- // 保存 "utmb" and "utmz"
- $this->utmb = join(".",$utmb);
- setcookie("utmb", $this->utmb, $this->time+$this->utmb_c_time, "/", ".".$this->ga_utmhn);
- $this->utmz = join(".",$utmz);
- setcookie("utmz", $this->utmz, $this->time+$this->utmz_c_time, "/", ".".$this->ga_utmhn);
- }
- else {
- $c_id = sprintf("%f", (rand(1000000000,2147483647) ^ $this->ga_hash) * 2147483647);
- $c_id = split("\.",$c_id);
- $this->utma = $this->ga_hash.".".$c_id[0].".".$this->time.".".$this->time.".".$this->time.".1";
- $this->utmb = $this->ga_hash.".1.10.".$this->time;
- $this->utmc = $this->ga_hash;
- $this->utmz = $this->ga_hash.".".$this->time.".1.1.".$source_str;
- setcookie("utma", $this->utma, $this->time+$this->utma_c_time, "/", ".".$this->ga_utmhn);
- setcookie("utmb", $this->utmb, $this->time+$this->utmb_c_time, "/", ".".$this->ga_utmhn);
- setcookie("utmc", $this->utmc, 0, "/", ".".$this->ga_utmhn);
- setcookie("utmz", $this->utmz, $this->time+$this->utmz_c_time, "/", ".".$this->ga_utmhn);
- }
- // 发送页面图片请求
- $this->html .= "<img src=\"".$this->ga_img.
- "?utmwv=".$this->ga_utmwv.
- "&utmn=".rand(1000000000,9999999999).
- "&utmhn=".$this->ga_utmhn."".$ga_set_params.
- "&utmhid=".rand(1000000000,9999999999).
- "&utmr=".rawurlencode($this->ga_referer).
- "&utmp=".rawurlencode($URI).
- // "&utmip=".rawurlencode($this->remote_address).
- "&utmac=".$this->ga_utmac."&utmcc=utma%3D".$this->utma."%3B%2Butmz%3D".
- rawurlencode($this->utmz)."%3B\" width=\"1\" height=\"1\" />\n";
- }
- //哈希算法
- function Hash($d) {
- if(!$d || $d=="") return 1;
- $h=0; $g=0;
- for($i=strlen($d)-1;$i>=0;$i--) {
- $c = (int)(ord($d[$i]));
- $h = (($h << 6) & 0xfffffff) + $c + ($c << 14);
- $g = ($h & 0xfe00000);
- if($g!=0) $h = ($h ^ ($g >> 21));
- }
- return $h;
- }
- function GetTrafficSource() {
- if(isset($_GET["utm_source"]) && isset($_GET["utm_medium"])) {
- // 链接中设置了来源
- $utmccn = isset($_GET["utm_campaign"]) ? $_GET["utm_campaign"] : "(not set)";
- $utmcct = isset($_GET["utm_content"]) ? $_GET["utm_content"] : "(not set)";
- return array("utmgclid"=>"", "utmcsr"=>$_GET["utm_source"], "utmccn"=>$utmccn,
- "utmcmd"=>$_GET["utm_medium"], "utmctr"=>$_GET["utm_term"], "utmcct"=>$utmcct);
- }
- else if($this->ga_referer!="") {
- // 从引荐网站过来的流量
- $search_engine = $this->GetSearchEngine();
- // 判断是否是搜索引擎
- if($search_engine) return $search_engine;
- else if(!isset($_COOKIE["utmc"])) {
- // 如果是新用户或不是搜索引擎,设置referer
- $ref = $this->GetReferer();
- if(substr($ref["host"],0,4)=="www.") $ref["host"] = substr($ref["host"],4); // Remove www from URL
- return array("utmgclid"=>"", "utmcsr"=>$ref["host"], "utmccn"=>"(referral)",
- "utmcmd"=>"referral", "utmctr"=>"", "utmcct"=>$ref["uri"]);
- }
- }
- return array("utmgclid"=>"", "utmcsr"=>"(direct)", "utmccn"=>"(direct)",
- "utmcmd"=>"(none)", "utmctr"=>"", "utmcct"=>"");
- }
- function GetSearchEngine() {
- $ref = $this->GetReferer();
- for($ii=0;$ii<count($this->ga_search);$ii++) {
- if(strpos(strtolower($ref["host"]), strtolower($this->ga_search[$ii][0]))>-1) {
- $test1 = strpos($ref["referer"], "?".$this->ga_search[$ii][1]."=");
- $test2 = strpos($ref["referer"], "&".$this->ga_search[$ii][1]."=");
- $i = ($test1 > -1) ? $test1 : $test2;
- if($i>-1) {
- $k = substr($ref["referer"], $i+strlen($this->ga_search[$ii][1])+2, strlen($ref["referer"]));
- $i = strpos($k,"&");
- if($i > -1) $k = substr($k,0,$i);
- if(isset($_GET["gclid"])) return array("utmgclid"=>$_GET["gclid"],
- "utmcsr"=>"", "utmccn"=>"(not set)", "utmcmd"=>"(not set)", "utmctr"=>$k, "utmcct"=>"");
- else return array("utmgclid"=>"", "utmcsr"=>$this->ga_search[$ii][0],
- "utmccn"=>"(organic)", "utmcmd"=>"organic", "utmctr"=>$k, "utmcct"=>"");
- }
- }
- }
- return false;
- }
- function GetReferer() {
- $referer_tmp = $this->ga_referer;
- $pos = strpos($referer_tmp, "://");
- if($pos>0) $referer_tmp = $referer_tmp = substr($referer_tmp,$pos+3);
- $pos = strpos($referer_tmp, "/");
- if($pos>0) return array("host"=>substr($referer_tmp, 0, $pos),
- "uri"=>substr($referer_tmp, $pos), "referer"=>$this->ga_referer);
- else return array("host"=>$referer_tmp, "uri"=>"", "referer"=>$this->ga_referer);
- }
- function SetTransaction($order_id, $amount, $shipping, $tax, $city, $region, $country) {
- // 交易记录
- $this->html .= "<img src=\"".$this->ga_img."?utmwv=".$this->ga_utmwv.
- "&utmn=".rand(1000000000,9999999999)."&utmhn=".$this->ga_utmhn.
- "&utmt=tran&utmtid=".$order_id."&utmtto=".$amount."&utmttx=".$tax.
- "&utmtsp=".$shipping."&utmtci=".rawurlencode($city)."&utmtrg=".rawurlencode($region).
- "&utmtco=".rawurlencode($country)."&utmac=".$this->ga_utmac."&utmcc=utma%3D".
- $this->utma."%3B%2Butmz%3D".rawurlencode($this->utmz)."%3B\" width=\"1\" height=\"1\" />\n";
- }
- function SetTransactionItem($order_id, $item_id, $category, $name, $price, $quantity) {
- // 交易商品记录
- $this->html .= "<img src=\"".$this->ga_img."?utmwv=".$this->ga_utmwv.
- "&utmn=".rand(1000000000,9999999999)."&utmhn=".$this->ga_utmhn.
- "&utmt=item&utmtid=".$order_id."&utmipc=".$item_id."&utmipn=".rawurlencode($name).
- "&utmiva=".rawurlencode($category)."&utmipr=".$price."&utmiqt=".$quantity."&utmac=".
- $this->ga_utmac."&utmcc=utma%3D".$this->utma."%3B%2Butmz%3D".rawurlencode($this->utmz)."%3B\"
- width=\"1\" height=\"1\" />\n";
- }
- function GetTrackingCode() {
- // 执行跟踪程序
- return $this->html;
- }
- }
以上就是使用Google Analytics来统计手机网站的流量的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!