<?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();// 获取请求页面URIif($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 depthif(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 URLreturn 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;}
}

使用Google Analytics来统计手机网站的流量相关推荐

  1. 使用Google Analytics来统计网站流量

    Google Analytics真的是非常强大的而且免费,而且操作起来非常的简单.当你的网站搭建起来之后,可以在五分钟之内添加Google Analytics的服务. 首先你要有一个谷歌帐号,gmai ...

  2. Vue项目和Tauri接入Google Analytics谷歌统计流程

    Google Analytics官网:https://analytics.google.com/ vue-gtag官网:Get started - vue-gtag 先要在Google Analyti ...

  3. Vue 自定义指令上报 Google Analytics 事件统计

    发现问题 一般前端开发离不开数据统计,我们经常需要接入统计服务以方便运营,例如现在需要统计一个按钮 <template><button @click="handleClic ...

  4. 分析Google Analytics中的流量来源

    <strong>对学习 google analytics 很有帮助,转载如下:</strong> 最近在查看一个网站的数据时发现,该网站流量来源中直接流量部分占了40%.这说明 ...

  5. Microsoft adCenter Analytics统计与Google Analytics、雅虎统计对比

    Microsoft adCenter Analytics统计与Google Analytics.雅虎统计对比 from 有梦有未来-飞行.航空.航天.飞机 by itofly 放上Microsoft ...

  6. 页面应用访问统计 - GA [Google Analytics]

    1.应用场景 用于页面应用访问统计, 包括多页面和单页面. 2.学习/操作 开发环境: Windows10 64位 专业版 laravel 5.8 / 6.x Google Analytics 百度统 ...

  7. Google Analytics统计代码GA.JS中文教程

    今天我们讲一讲Google Analytics新统计代码GA.JS,关于GA.JS脚本在这里跟大家深入浅出地进行探讨和学习. 代码示例: 1 2 3 4 5 6 7 8 9 10 <script ...

  8. 流量的秘密—Google Analytics网站分析与优化技巧(第2版)

    本文转自蓝鲸的网站分析笔记 文章地址http://bluewhale.cc/2012-05-07/advanced-web-metrics-with-google-analytics-2nd-edit ...

  9. (转载)Google Analytics(Google分析)使用技巧

    Google Analytics(Google分析)是Google的一款免费的网站分析服务,自从其诞生以来,即广受好评.Google Analytics功能非常强大,只要在网站的页面上加入一段代码,就 ...

  10. Google Analytics是什么

    Google分析是一个由Google所提供的网站流量统计服务.Google 分析现在是互联网上使用最广泛的网络分析服务.Google Analytics还提供了一个SDK,允许从iOS和Android ...

最新文章

  1. keras各种模型大全
  2. linux hdparm 测试磁盘io,hdparm测试硬盘性能
  3. mysql带AND关键字的多条件查询
  4. window.event.returnValue=false
  5. oracle 无法解析指定的连接标识符
  6. Python爬虫之解析网页
  7. 顶尖的语音识别软件――Nuance Recognizer_语音识别_CTI论坛
  8. 程序员的《学习之道》
  9. 【零基础 快速学Java】韩顺平 笔记整理(到p170 跑路了)
  10. html网页表单设计实验报告,网页设计实验报告(学生).doc
  11. VSCode配置vue用户代码片段Snippets
  12. 你看不到的华为两大“笨功夫”
  13. 公司企业邮箱怎么注册申请,教你选择适合的企业邮箱
  14. nosql包括的数据库
  15. 以下关于python函数说法错误的是def_以下关于Python函数的描述中,错误的是()
  16. 【三维几何学习】DiffusionNet: Discretization Agnostic Learning on Surfaces
  17. 12月5日计算机考试浙江卷英语答案,英语b级试卷?2019年12月b级真题试卷。
  18. 反激式开关电源芯片是什么?如何对反激开关电源mos管选型?
  19. 使用pymysql连接数据库
  20. 太卷了,华为某领导说招外包只要985!

热门文章

  1. 手机电池校正代码_手机电池校正!iPhone手机电池校正设置
  2. 【惊】手机将走向末路,混合现实(MR)难道就是5G时代的方向
  3. 【Linux】VirtualBox设置静态ip
  4. 典型的人工神经网络由很多层构成,但不包括
  5. Vue中使用地图平台MapboxGL
  6. MIMO技术杂谈(一):鱼与熊掌能否兼得?--浅谈分集与复用的权衡
  7. 短视频社交应用Socialcam关闭背后,是又一个圈钱阴谋?
  8. 攻防演练中防守方的骚姿势
  9. 滴滴拉屎v1.20 一款非常好用的找厕所工具
  10. 惯性导航系统可替代基于GPS的跟踪