ga_utmac = $ga_utmac; $this->ga_utmhn = $ga_utmhn; $this->ga_hash = $this->Hash($ga_utmhn); if(isset($gclid) and !empty($gclid)) $_GET['gclid'] = $gclid; $this->tracking_codes[] = 'gclid : '.$_GET['gclid']; // Set the time for the request $this->time = time(); // Set the page URI that is request if($URI==NULL) $URI = $_SERVER['REQUEST_URI']; // Set the referer page $this->ga_referer = $referer; // Set the visitor source $source = $this->GetTrafficSource(); // Set the new traffic source 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 if Google Analytics cookie "__utma" already exists if(isset($_COOKIE["__utma"])) { // Save cookies to local variable $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); // Check if Google Analytics "session" cookie "__utmc" exists, if not create one and update the number of visits in cookie: "__utma" if(!isset($_COOKIE["__utmc"])) { // Increase the number of visits $__utma[5] = $__utma[5]+1; // Update the time of the visit $__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); // Update "__utmb" cookie with the number of pageviews or create a new cookie if(isset($_COOKIE["__utmb"])) $__utmb[1] = 1; else $__utmb = array($__utma[0], 1, 10, $this->time); } else $__utmb[1] = $__utmb[1]+1; // Increase the number of pageviews in "__utmb" cookie // 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 cookies "__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 { // No Google Analytics cookies exists, create new ones and save them i local variables $cookieRandom = rand(1000000000,2147483647); $this->__utma = $this->ga_hash.".".$cookieRandom.".".$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->__utmz = $this->ga_hash.".".$this->time.".1.1.".$source_str; // Create the pageview request to Google Analytics image $this->tracking_codes[] = $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)."&utmac=".$this->ga_utmac."&utmcc=__utma%3D".$this->__utma."%3B%2B__utmz%3D".rawurlencode($this->__utmz)."%3B"; } 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"])) { // The traffic source i set in the URL $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"=>"medium : ".$_GET["utm_medium"], "utmctr"=>$_GET["utm_term"], "utmcct"=>$utmcct); } else if($this->ga_referer!="") { // The treffic source is from a referral site $search_engine = $this->GetSearchEngine(); // Check if it's a search engine if($search_engine) return $search_engine; else if(!isset($_COOKIE["__utmc"])) { // It's not a search engine and it's a new visit. Set the 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;$iiga_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 gclid)", "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){ // Generate code to set a new transaction in Google Analytics $this->tracking_codes[] = $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"; } function SetTransactionItem($order_id, $item_id, $category, $name, $price, $quantity){ // Generate code to set a new transaction item in Google Analytics, you must call the function SetTransaction before you call this one. $this->tracking_codes[] = $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"; } function SetTrackPageview($page){ // Generate code to set a new transaction item in Google Analytics, you must call the function SetTransaction before you call this one. $this->tracking_codes[] = $this->ga_img."?utmwv=".$this->ga_utmwv."&utmn=".rand(1000000000,9999999999)."&utmhn=".$this->ga_utmhn."&utmp=".rawurlencode($page)."&utmac=".$this->ga_utmac."&utmcc=__utma%3D".$this->__utma."%3B%2B__utmz%3D".rawurlencode($this->__utmz)."%3B"; //$urchinUrl='http://www.google-analytics.com/__utm.gif?utmwv=1&utmn='.$var_utmn.'&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn='.$var_utmhn.'&utmr='.$var_referer.'&utmp='.$var_utmp.'&utmac='.$var_utmac.'&utmcc=__utma%3D'.$var_cookie.'.'.$var_random.'.'.$var_today.'.'.$var_today.'.'.$var_today.'.2%3B%2B__utmb%3D'.$var_cookie.'%3B%2B__utmc%3D'.$var_cookie.'%3B%2B__utmz%3D'.$var_cookie.'.'.$var_today.'.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D'.$var_cookie.'.'.$var_uservar.'%3B'; } function SetTrackEvent($event){ // Generate code to set a new transaction item in Google Analytics, you must call the function SetTransaction before you call this one. $this->tracking_codes[] = $this->ga_img."?utmwv=".$this->ga_utmwv."&utmn=".rand(1000000000,9999999999)."&utmhn=".$this->ga_utmhn."&utmt=event&=utme=".rawurlencode($event)."&utmac=".$this->ga_utmac."&utmcc=__utma%3D".$this->__utma."%3B%2B__utmz%3D".rawurlencode($this->__utmz)."%3B"; //$urchinUrl='http://www.google-analytics.com/__utm.gif?utmwv=1&utmn='.$var_utmn.'&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn='.$var_utmhn.'&utmr='.$var_referer.'&utmp='.$var_utmp.'&utmac='.$var_utmac.'&utmcc=__utma%3D'.$var_cookie.'.'.$var_random.'.'.$var_today.'.'.$var_today.'.'.$var_today.'.2%3B%2B__utmb%3D'.$var_cookie.'%3B%2B__utmc%3D'.$var_cookie.'%3B%2B__utmz%3D'.$var_cookie.'.'.$var_today.'.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D'.$var_cookie.'.'.$var_uservar.'%3B'; } function GetTrackingCode() { // Return the Google Analytics code for this request return $this->tracking_codes; } } ?>