Use Google Analytics to count mobile site traffic

Source: Internet
Author: User
Tags setcookie
use Google Analytics to count mobile site traffic

<?phpclass googleanlayticsmobile {private $utma;   Utma Cookie Record Unique visitor private $utma _c_time = 63072000;       Two years (by default, two years) private $UTMB;    UTMB Cookie records the user's session expiration time private $UTMB _c_time = 1800;      30 minutes (two years by default) private $UTMC;      UTMC cookie user exits the browser and fails the private $UTMZ;   UTMZ Cookie main function record access source private $UTMZ _c_time = 1800;      30 minutes (by default half a year) private $ga _utmhn;      Website domain name private $ga _utmac;   GA account Private $ga _UTMWV = "4.9.2";      Google tracking code version private $ga _hash; GA domain name hash value private $ga _img = "Http://www.google-analytics.com/utm.gif"; Private $ga _search = 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-&GT;GA_UTMHN = $ga _utmhn;   $this->ga_hash = $this->hash ($ga _utmhn);  $this->time = time ();  Gets the request page URI if ($URI ==null) $URI = $_server[' Request_uri ');  Get the Referral website URL $this->ga_referer = $_server[' http_referer ');     Get the User IP address save only the first three bits $this->remote_address = $_server[' remote_addr ');  Get traffic Source $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 =&gt ; $value) $ga _set_params. = "&". $key. "  = ". Rawurlencode ($value);   else $ga _set_params = ""; Check if "Utma" exists if (Isset ($_cookie["Utma")) {$this->utma = $_cookie["Utma"];   $this-&GT;UTMB = $_cookie["UTMB"];      $this-&GT;UTMZ = $_cookie["UTMZ"];      $UTMB = Split ("\.", $this-&GT;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);    Check if "UTMC" is present and update the number of accesses in "UTMA" if it does not exist.    if (!isset ($_cookie["UTMC")) {//increase the number of accesses $utma [5] = $utma [5]+1;    Update Access Time $UTMA [3] = $UTMA [4];    $UTMA [4] = $this->time;    Save cookies $this->utma = Join (".", $utma); Setcookie ("Utma", $this->utma, $this->time+ $this->utma_c_time, "/", ".".    $this-&GT;GA_UTMHN); Setcookie ("UTMC", $utma [0], 0, "/", ".".    $this-&GT;GA_UTMHN);    Generate "UTMB" or update the number of access pages in "UTMB".    if (Isset ($_cookie["UTMB")) $UTMB [1] = 1;   else $UTMB = Array ($utma [0], 1, ten, $this->time); } else $UTMB [1] = $UTMB [1]+1; Add access Pages in "UTMB"//Update traffic source if ($UTMZ[4]!= $source _str && $source ["UTMCSR"]!= "(direct)") $UTMZ = Array ($utmz [0], $this->time, $utma [5], $UTMZ [3]+    1, $source _str);   Save "UTMB" and "UTMZ" $this-&GT;UTMB = Join (".", $UTMB); Setcookie ("UTMB", $this-&GT;UTMB, $this->time+ $this->utmb_c_time, "/", ".".   $this-&GT;GA_UTMHN);   $this-&GT;UTMZ = Join (".", $UTMZ); Setcookie ("UTMZ", $this->utmz, $this->time+ $this->utmz_c_time, "/", ".".  $this-&GT;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-&GT;UTMB = $this->ga_hash. ".   1.10. ". $this->time;   $this-&GT;UTMC = $this->ga_hash; $this-&GT;UTMZ = $this->ga_hash. ".". $this->time. "   1.1. ". $source _str; Setcookie ("Utma", $this->utma, $this->time+ $this->utma_c_time, "/", ".".   $this-&GT;GA_UTMHN); Setcookie ("UTMB", $this->utMB, $this->time+ $this->utmb_c_time, "/", ".".   $this-&GT;GA_UTMHN); Setcookie ("UTMC", $this->utmc, 0, "/", ".".   $this-&GT;GA_UTMHN); Setcookie ("UTMZ", $this->utmz, $this->time+ $this->utmz_c_time, "/", ".".  $this-&GT;GA_UTMHN);  }//Send page picture request $this->html. = "ga_img.  "? utmwv=". $this-&GT;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";  }//hash algorithm 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"])) {//Link set source $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 "and" = $UTMCCN, "utmcmd" =>$_get["utm_  Medium "]," utmctr "=>$_get[" Utm_term "]," utmcct "= = $utmcct);   } else if ($this->ga_referer!= "") {//traffic coming from the referral website $search _engine = $this->getsearchengine ();    Determine if the search engine if ($search _engine) return $search _engine;    else if (!isset ($_cookie["UTMC"])) {//If it is a new user or not a search engine, set 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" and "(referral)","Utmcmd" = "Referral", "utmctr" and "" "," Utmcct "and" = "$ref [" uri "]); }} return Array ("Utmgclid" + "", "UTMCSR" = "(direct)", "UTMCCN" and "(direct)", "Utmcmd" and "(none)", "utmctr" = "", "Utmcct" and "" ");  } 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) {$test 1 = strpos ($ref ["Referer"], "?". $this->ga_search[$ii][1]. "    ="); $test 2 = Strpos ($ref ["Referer"], "&". $this->ga_search[$ii][1]. "    ="); $i = ($test 1 >-1)?    $test 1: $test 2;     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)", "UT     Mcmd "=" (not set), "Utmctr" and "Utmcct" ("=" and "" "); ElsE return Array ("Utmgclid" + "", "UTMCSR" and "=" = $this->ga_search[$ii][0], "UTMCCN" = "(organic)", "Utmcmd"    "Organic", "utmctr" and "$k", "Utmcct" and "" "; }}} 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" and "=" = $this->ga_referer); } function Settransaction ($order _id, $amount, $shipping, $tax, $city, $region, $country) {//Transaction $this->html. = "& Lt;img src=\ "". $this->ga_img. "?  utmwv= ". $this-&GT;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-&GT;UTMZ). %3b\ "width=\" 1\ "height=\" 1\ "/>\n"; } function Settransactionitem ($order _id, $item _id, $category, $name, $price, $quantity) {//Trading commodity records $this->html. = " ga_img. "?  utmwv= ". $this-&GT;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-&GT;UTMZ). %3b\ "width=\" 1\ "height=\" 1\ "/>\n"; } function Gettrackingcode () {//Execute tracer return $this->html;}}

The above is the use of Google Analytics to count the mobile site traffic content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.