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吧):

  1. <?php
  2. }else if(strpos($ua, "Baiduspider") !== false){
  3.         $browser_type = ST_BOT;
  4.         $s_id = "Baiduspider";
  5.         return "Baiduspider";
  6.     }else if(strpos($ua, "sohu-search") !== false){
  7.         $browser_type = ST_BOT;
  8.         $s_id = "SogouBot";
  9.         return "SogouBot";
  10.     }
  11. ?>

原理很简单,就是分析HTTP头中的"HTTP_USER_AGENT"信息,负责任的搜索引擎都会给出自己的标志,比如Google的"Googlebot",MSN的"msnbot"等等,对于那些把自己伪装成浏览器的搜索引擎,不统计也罢。

一个需要注意的问题是大小写不能搞错。

然后再修改一下"stattraq-install.php",主要是为了今后的升级:

  1. <?php
  2. $sqlQuery = "UPDATE {$tablestattraq} SET user_agent_type=1
  3.         WHERE browser = 'Googlebot' OR
  4.              browser = 'msnbot' OR
  5.             browser = 'Baiduspider' OR
  6.             browser = 'SogouRobot' OR
  7.             ... "
  8. ?>

2) SE Saturation

在StatTraq的SE Saturation中,可以看到对各个搜索引擎收录你文章的统计,同样,加上百度和Sogou。

在"search_engine_stat.php"中,找到相应的代码段,并添加如下信息:

  1. <?php
  2. $baidu = getPageDBResults("Baiduspider", $date_format, $time_frame, $betweenClause, $orderBy);
  3. $sogou = getPageDBResults("SogouBot", $date_format, $time_frame, $betweenClause, $orderBy);
  4.  
  5. ...
  6.  
  7. if($baidu)
  8. {
  9.     $baidu_count = $baidu->cnt;
  10. }
  11. if($sogou)
  12. {
  13.     $sogou_count = $sogou->cnt;
  14. }
  15.  
  16. ...
  17.  
  18. 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>';
  19. 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>';
  20. 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>';
  21.  
  22. ...
  23.  
  24.     <dt>Baidu.com</dt>
  25.     <dd><a href="http://www.baidu.com/search/url_submit.html">http://www.baidu.com/search/url_submit.html</a></d>
  26.     <dt>Sogou.com</dt>
  27.     <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>
  28.  
  29. ?>

显然,这里的"BaiduSpider"和"SogouRobot"就是我们刚才修改的那个函数的返回值。

3)统计用Baidu访问的关键词(Search Team)

在"stattraq.php"的函数"statTraqGetSearchPhrase"中,添加针对Baidu和Sogou的解析:

  1. <?php
  2.     }else if(strpos($referrer, "baidu.")!== false){
  3.         $key = "wd";
  4.     }else if(strpos($referrer, "sogou.")!== false){
  5.         $key = "query";
  6. ?>

原理很简单,就是分析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′)

- 解决StatTraq的乱码问题

2) Baidu, Sogou等query串的转码:

用iconv来进行转码

  1. if($gb2312 == true && extension_loaded("iconv"))
  2.     $phrase = iconv("gb2312","UTF-8",$phrase);

Popularity: 37% [?]

Related entries:

4 Responses to “Some hacking to support Baidu and Sogou in StatTraq”

  1. 数字图书馆前沿问题高级研讨班 » Blog Archive » WP插件:wp-StatTraq Says:

    [...] 一个很好用的访问统计插件:wp-Stattraq,目前的版本是1.0b。目前官方版本只支持google、msn、yahoo!。如果要添加百度、搜狗的支持,可以参考:“Some hacking to support Baidu and Sogou in StatTraq”。 安装好后的访问地址是:StatTraq @ DL-China Tags: none [...]

  2. BunnyQ Says:

    我那stattraq也是自己hack加上的百度 国内搜索引擎应该都是gb2312吧
    所以显示出的文字编码出问题 考虑iconv转换一下..

  3. Meng Yan Says:

    Thanks BunnyQ, yes, it should be “iconv”ed, :-)

  4. Nickcheng.LOG » Blog Archive » WP-StatTraq优化 Says:

    [...] 2. 添加新的搜索引擎 这个统计系统默认只是支持Google, Yahoo和MSN这三个搜索引擎, 而国内的一些搜索引擎就只有我们自己想办法了. 这个链接就给出了解决方法, 不过里面的代码有一点点问题

    1. $baidu_count = 0; $sogou_count = 0;

    http://www.mengyan.org/blog/archives/2005/08/30/59.html   [link] [...]

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI