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

31 篇文章 1 订阅
13 篇文章 0 订阅
<?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%2B__utmz%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%2B__utmz%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%2B__utmz%3D".rawurlencode($this->__utmz)."%3B\" width=\"1\" height=\"1\" />\n";
 }
 
 function GetTrackingCode() {
  // 执行跟踪程序
  return $this->html;
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值