Some hacking to support Baidu and Sogou in StatTraq
一直用Stattraq插件来统计Blog的访问信息,使用起来还不错。后来发现,我的Blog上很多关键词都是来自Baidu,而Stattraq根本没有对Baidu的特殊处理,于是Hack一下,让Stattraq也能统计百度的信息。
1)BaiduRobot
在"stattraq.php"的函数"statTraqGetBrowser"中,添加对Baidu Robot的识别(顺便也识别一下Sogou的Robot吧):
- <?php
- }else if(strpos($ua, "Baiduspider") !== false){
- $browser_type = ST_BOT;
- $s_id = "Baiduspider";
- return "Baiduspider";
- }else if(strpos($ua, "sohu-search") !== false){
- $browser_type = ST_BOT;
- $s_id = "SogouBot";
- return "SogouBot";
- }
- ?>
原理很简单,就是分析HTTP头中的"HTTP_USER_AGENT"信息,负责任的搜索引擎都会给出自己的标志,比如Google的"Googlebot",MSN的"msnbot"等等,对于那些把自己伪装成浏览器的搜索引擎,不统计也罢。
一个需要注意的问题是大小写不能搞错。
然后再修改一下"stattraq-install.php",主要是为了今后的升级:
- <?php
- $sqlQuery = "UPDATE {$tablestattraq} SET user_agent_type=1
- WHERE browser = 'Googlebot' OR
- browser = 'msnbot' OR
- browser = 'Baiduspider' OR
- browser = 'SogouRobot' OR
- ... "
- ?>
2) SE Saturation
在StatTraq的SE Saturation中,可以看到对各个搜索引擎收录你文章的统计,同样,加上百度和Sogou。
在"search_engine_stat.php"中,找到相应的代码段,并添加如下信息:
- <?php
- $baidu = getPageDBResults("Baiduspider", $date_format, $time_frame, $betweenClause, $orderBy);
- $sogou = getPageDBResults("SogouBot", $date_format, $time_frame, $betweenClause, $orderBy);
- ...
- if($baidu)
- {
- $baidu_count = $baidu->cnt;
- }
- if($sogou)
- {
- $sogou_count = $sogou->cnt;
- }
- ...
- echo '<tr><td>Number of Pages Indexed</td><td class="right">' . $google_count . '</td><td class="right">' . $yahoo_count . '</td><td class="right">' . $msn_count . '</td><td class="right">' . $baidu_count . '</td><td class="right">' . $sogou_count . '</td></tr>';
- echo '<tr><td>Number of Pages <em>Not</em> Indexed</td><td class="right">' . ($total - $google_count) . '</td><td class="right">' . ($total - $yahoo_count) . '</td><td class="right">' . ($total - $msn_count) . '</td><td class="right">' . ($total - $baidu_count) . '</td><td class="right">' . ($total - $sogou_count) . '</td></tr>';
- echo '<tr><td>Percent Saturation</td><td class="right">' . floor(($google_count/$total)*100) . '%</td><td class="right">' . floor(($yahoo_count/$total)*100) . '%</td><td class="right">' . floor(($msn_count/$total)*100) . '%</td><td class="right">' . floor(($baidu_count/$total)*100) . '%</td><td class="right">' . floor(($sogou_count/$total)*100) . '%</td></tr>';
- ...
- <dt>Baidu.com</dt>
- <dd><a href="http://www.baidu.com/search/url_submit.html">http://www.baidu.com/search/url_submit.html</a></d>
- <dt>Sogou.com</dt>
- <dd><a href="http://db.sohu.com/regurl/regform.asp?Step=REGFORM&class=">http://db.sohu.com/regurl/regform.asp?Step=REGFORM&class=</a></d>
- ?>
显然,这里的"BaiduSpider"和"SogouRobot"就是我们刚才修改的那个函数的返回值。
3)统计用Baidu访问的关键词(Search Team)
在"stattraq.php"的函数"statTraqGetSearchPhrase"中,添加针对Baidu和Sogou的解析:
- <?php
- }else if(strpos($referrer, "baidu.")!== false){
- $key = "wd";
- }else if(strpos($referrer, "sogou.")!== false){
- $key = "query";
- ?>
原理很简单,就是分析referrer中的Query串(Baidu默认是采用GB2312来传递,这个比较烦人)。
比如:
http://www.google.com/search?hl=zh-CN&q=xerdoc&…
"q"就是Key。
Update (2005.11.04)
修改一下StatTraq中文乱码的错误:
1) Summary页面中的乱码:
1.打开/wp-stattraq/reporter/summary.php,找到第140行
<?php echo htmlentities(stripslashes($row->post_title));?> ,修改为
<?php echo htmlentities(stripslashes($row->post_title),ENT_NOQUOTES,’utf-8′);?>2.打开/wp-stattraq/reporter/query_strings.php,找到第88行
htmlentities($row->search_phrase) ,修改为
htmlentities($row->search_phrase,ENT_NOQUOTES,’utf-8′)
2) Baidu, Sogou等query串的转码:
用iconv来进行转码
- if($gb2312 == true && extension_loaded("iconv"))
- $phrase = iconv("gb2312","UTF-8",$phrase);
Popularity: 37% [?]
Related entries:
September 28th, 2005 at 12:08 am
[...] 一个很好用的访问统计插件:wp-Stattraq,目前的版本是1.0b。目前官方版本只支持google、msn、yahoo!。如果要添加百度、搜狗的支持,可以参考:“Some hacking to support Baidu and Sogou in StatTraq”。 安装好后的访问地址是:StatTraq @ DL-China Tags: none [...]
September 30th, 2005 at 2:15 pm
我那stattraq也是自己hack加上的百度 国内搜索引擎应该都是gb2312吧
所以显示出的文字编码出问题 考虑iconv转换一下..
October 2nd, 2005 at 3:12 pm
Thanks BunnyQ, yes, it should be “iconv”ed,
October 28th, 2005 at 6:25 pm
[...] 2. 添加新的搜索引擎 这个统计系统默认只是支持Google, Yahoo和MSN这三个搜索引擎, 而国内的一些搜索引擎就只有我们自己想办法了. 这个链接就给出了解决方法, 不过里面的代码有一点点问题
http://www.mengyan.org/blog/archives/2005/08/30/59.html [link] [...]