Meng Yan ( 孟岩 )’s Weblog

Some hacking to support Baidu and Sogou in StatTraq

by Meng Yan on Aug.30, 2005, under Blog

一直用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: 43%


2 Comments for this entry

2 Trackbacks / Pingbacks for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!