Last active
October 13, 2015 14:57
-
-
Save firedfox/4212865 to your computer and use it in GitHub Desktop.
holmes-for-tc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class _HMT { | |
// 版本号 | |
private $VERSION = "tc-1.0"; | |
// 访次长度(秒) | |
private $VISIT_DURATION = 1800; | |
// 访客有效期(秒) | |
private $VISITOR_MAX_AGE = 31536000; | |
// 站点id | |
private $siteId = ""; | |
// 域名列表 | |
private $domainList = array(); | |
// 受访页面 | |
private $visitURL = NULL; | |
// 来源页面 | |
private $refererURL = NULL; | |
// 事件类型 | |
private $eventType = 0; | |
// 事件属性 | |
private $eventProperty = ""; | |
// 从url中获取指定参数的值 | |
private function getQueryValue($url, $key) { | |
preg_match("/(^|&|\\?|#)(" . $key . ")=([^&#]*)(&|$|#)/", $url, $matches); | |
return count($matches) > 0 ? $matches[3] : NULL; | |
} | |
// 判断某个域名是否为另一个域名的子域名 | |
private function isSubDomain($subDomain, $domain) { | |
$subDomain = "." . preg_replace("/:\d+/", "", $subDomain); | |
$domain = "." . preg_replace("/:\d+/", "", $domain); | |
$index = strpos($subDomain, $domain); | |
return ($index !== FALSE && $index + strlen($domain) === strlen($subDomain)); | |
} | |
// 判断某个域名是否被包含在域名列表中 | |
// 是则返回列表中匹配的域名,否则返回FALSE | |
private function matchDomainList($hostName) { | |
foreach ($this->domainList as $domain) { | |
if ($this->isSubDomain($hostName, $domain)) { | |
return $domain; | |
} | |
} | |
return FALSE; | |
} | |
// 获取来源类型 | |
private function getSourceType( | |
$currentHost, | |
$refererHost, | |
$currentPageVisitTime, | |
$lastPageVisitTime | |
) { | |
if (is_null($refererHost) // 来源页面为空 | |
// 当前页面和来源页面都在域名列表内 | |
|| ($this->matchDomainList($currentHost) !== FALSE | |
&& $this->matchDomainList($refererHost) !== FALSE) | |
|| $currentHost === $refererHost // 来源和当前页面同域 | |
) { | |
return | |
($currentPageVisitTime - $lastPageVisitTime > $this->VISIT_DURATION) | |
? 1 : 4; // 直达/站内 | |
} | |
else { | |
return 3; // 外链 | |
} | |
} | |
// 获取统计请求url | |
private function getPixelURL() { | |
// 当前页面url | |
$currentURL = $this->visitURL; | |
$parsedCurrentURL = parse_url($currentURL); | |
$currentHost = $parsedCurrentURL["host"]; | |
// 来源页面url | |
$refererURL = $this->refererURL; | |
$parsedRefererURL = parse_url($refererURL); | |
$refererHost = $parsedRefererURL["host"]; | |
// 当前页面的访问时间 | |
$currentPageVisitTime = time(); | |
// 前一页面的访问时间 | |
$lastPageVisitTime = (int)$_COOKIE["Hm_lpvt_" . $this->siteId]; | |
// 前一访次或当前访次的起始时间 | |
$lastVisitTime = $_COOKIE["Hm_lvt_" . $this->siteId]; | |
// 来源类型 | |
$sourceType = $this->getSourceType($currentHost, $refererHost, $currentPageVisitTime, $lastPageVisitTime); | |
// 是否为新访次 | |
$isNewVisit = ($sourceType == 4) ? 0 : 1; | |
// 写cookie要使用的域名 | |
$cookieDomain = ""; | |
// 每次都要更新lpvt | |
setCookie( | |
"Hm_lpvt_" . $this->siteId, | |
$currentPageVisitTime, | |
0, | |
"/", | |
$cookieDomain | |
); | |
// 在访次开始时要更新lvt | |
if ($isNewVisit === 1) { | |
setCookie( | |
"Hm_lvt_" . $this->siteId, | |
$currentPageVisitTime, | |
time() + $this->VISITOR_MAX_AGE, | |
"/", | |
$cookieDomain | |
); | |
} | |
$pixelURL = "http://hm.baidu.com/hm.gif" . | |
"?si=" . $this->siteId . | |
"&et=" . $this->eventType . | |
($this->eventProperty !== "" ? "&ep=" . str_replace("%27", "'", urlencode($this->eventProperty)) : "") . | |
"&nv=" . $isNewVisit . | |
"&st=" . $sourceType . | |
(!is_null($lastVisitTime) ? "<=" . $lastVisitTime : "") . | |
(!is_null($refererURL) ? "&su=" . urlencode($refererURL) : "") . | |
($this->visitURL !== "" ? "&u=" . urlencode($this->visitURL) : "") . | |
"&v=" . $this->VERSION . | |
"&rnd=" . rand(10e8, 10e9); | |
// 指定广告跟踪 | |
$adTags = array("cm", "cp", "cw", "ci", "cf"); | |
$adKeys = array("hmmd", "hmpl", "hmkw", "hmci", "hmsr"); | |
for ($i = 0; $i < count($adKeys); $i++) { | |
$key = $adKeys[$i]; | |
$value = $this->getQueryValue($currentURL, $key); | |
if (!is_null($value)) { | |
$pixelURL .= "&" . $adTags[$i] . "=" . urlencode($value); | |
} | |
} | |
// 因为url要显示在html中,这里需要做html转义 | |
return htmlspecialchars($pixelURL); | |
} | |
// 构造函数 | |
public function __construct($siteId) { | |
$this->siteId = $siteId; | |
} | |
// 设置站点id | |
public function setAccount($siteId) { | |
$this->siteId = $siteId; | |
} | |
// 设置域名列表 | |
// 变长参数 | |
public function setDomainName($domainList) { | |
$this->domainList = func_get_args(); | |
} | |
// 设置来源页面 | |
public function setReferrerOverride($url) { | |
$this->refererURL = $url; | |
} | |
// 发送pv统计请求 | |
public function trackPageView($url) { | |
$this->eventType = 0; | |
$this->eventProperty = ""; | |
$this->visitURL = $url; | |
return $this->getPixelURL(); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 这几行代码请添加到页面的最顶部 | |
// 请把"站点id"替换成您站点的id(js统计代码的 hm.baidu.com/h.js%3F 之后的部分) | |
require("hm.php"); | |
$_hmt = new _HMT("dbe397b0a5ed652d2c6be5c367251663"); | |
$_hmt->setDomainName("126.com", "21cn.com"); | |
$_hmt->setReferrerOverride("http://21cn.com/"); | |
$_hmtPixel = $_hmt->trackPageView("http://126.com/"); | |
?> | |
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN" "http://www.wapforum.org/DTD/wml13.dtd"> | |
<wml> | |
<card title="百度统计WAP页面监控演示"> | |
<img src="<?php echo $_hmtPixel; ?>" width="0" height="0" /> | |
<p>这里是用户页面中的原有内容</p> | |
</card> | |
</wml> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment