& Lt ;? PhpclassGoogleAnlayticsMobile {private $ __utma ;__ utmacookie records unique identity visitors private $__utma_c_time63072000; two years (two years by default) private $ __utmb; __utmbcookie records a user's Se...
Use Google Analytics to count mobile website traffic
Ga_utmac = $ ga_utmac; $ this-> ga_utmhn = $ ga_utmhn; $ this-> ga_hash = $ this-> Hash ($ ga_utmhn); $ this-> time = time (); // Obtain the REQUEST page URI if ($ URI = NULL) $ URI = $ _ SERVER ['request _ URI ']; // Obtain the URL of the referral website $ this-> ga_referer =$ _ SERVER ['http _ referer']; // obtain the user IP address and save only the first three digits $ this-> remote_address = $ _ SERVER ['remote _ ADDR ']; // Obtain the 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 => $ value) $ ga_set_params. = "&". $ key. "= ". rawurlencode ($ value); else $ ga_set_params = ""; // check whether "utma" exists if (isset ($ _ COOKIE ["utma"]) {$ this-> utma =$ _ COOKIE ["utma"]; $ this-> utmb =$ _ COOKIE ["utmb"]; $ this-> utmz =$ _ COOKIE ["utmz"]; $ u Tmb = 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); // check whether" utmc "exists. if not, update the access times in" utma. If (! Isset ($ _ COOKIE ["utmc"]) {// increase the number of visits $ utma [5] = $ utma [5] + 1; // update the 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-> ga_utmhn); setcookie ("utmc", $ utma [0], 0 ,"/",". ". $ this-> 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, 10, $ this-> time);} else $ utmb [1] = $ utmb [1] + 1; // add the access page number in "utmb" // update the 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-> 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 (000000000,2 147483647) ^ $ 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_utmh N); 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);} // sends a page image request $ this-> html. = "ga_img. "? Utmwv = ". $ this-> ga_utmwv. "& utmn = ". rand (000000000,9999999999 ). "& utmhn = ". $ this-> ga_utmhn. "". $ ga_set_params. "& utmhid = ". rand (000000000,9999999999 ). "& utmr = ". rawurlencode ($ this-> ga_referer ). "& utmp = ". rawurlencode ($ URI ). // "& utmip = ". rawurlencode ($ this-> remote_address ). "& utmac = ". $ this-> ga_utmac. "& utmcc = utma % 3D ". $ this-> utma. "% 3B % 2 Butmz % 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) & 0 xfffffff) + $ 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"]) {// The Source $ utmccn = isset ($ _ GET ["utm_campaign"]) is set in the link. $ _ 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! = "") {// Traffic from the referral website $ search_engine = $ this-> GetSearchEngine (); // determines whether it is a 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" => "(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
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 = 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), "refe Rer "=> $ this-> ga_referer); else return array (" host "=> $ referer_tmp," uri "=> "", "referer" => $ this-> ga_referer);} function SetTransaction ($ order_id, $ amount, $ shipping, $ tax, $ city, $ region, $ country) {// transaction record $ this-> html. = "ga_img. "? Utmwv = ". $ this-> ga_utmwv. "& utmn = ". rand (000000000,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 % 2 Butmz % 3D ". rawurlencode ($ this-> utmz ). "% 3B \" width = \ "1 \" height = \ "1 \"/> \ n ";} function SetTransactionItem ($ order_id, $ item_id, $ category, $ name, $ price, $ quantity) {// transaction item record $ this-> html. = "ga_img. "? Utmwv = ". $ this-> ga_utmwv. "& utmn = ". rand (000000000,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 % 2 Butmz % 3D ". rawurlencode ($ this-> utmz ). "% 3B \" width = \ "1 \" height = \ "1 \"/> \ n ";} function GetTrackingCode () {// execute the tracking program return $ this-> html ;}}
The above section uses Google Analytics to measure the traffic of mobile websites. For more information, see PHP Chinese website (www.php1.cn )!