Sunday, September 25, 2011

Tips12: Modifying Add Hits on Article Object




Each time an object is opened, TikiWiki can calculate, how many times the object was opened. It can be seen from the page counter of that object. For example, for object article, page counters is performed by the field nbreads of tiki_articles table that will count every visitor who opens an article page. The counter will never decrease, it will even continue to increase when a visitor opens that article page. In the object file galleries, page counter is calculated through the field hits.

Increment of the page counter would be a problem if the visitor is not really a person who uses his hand to click a link, or write the page address manually. Many of the webmaster who struggle to solve this because they get the hits/counters that are high but does not reflect that the hits were coming from human origin. And, indeed, in fact so. To know if visitors really a human being is not easy. However, we could manage. The trick is to insert a php command that identifies the user agent of the visitor referrers, just before the execution the number of counters done.

If you have a kind of logbook or log stats that contains data like IP Address, referral, keyword, and user agent, then you will see the user agent that contains keywords like Google Bot, MSN Bot, Bing Bot, Yahoo Slurp, facebookexternalhit, Twitter Bot, and Baidu crawl in which they are a direct referral. Their job is to index any web page if there is any new or renewed page. Often, the bots help fuel the page counter of the web page. The purpose of creating the following  PHP script is to bypass bots from the counter so that when the bots indexing a web page, the counter is not increased.
  1. Open file /lib/articles/artlib.php
  2. Go to function add_article_hit. The original function is as follow:
    function add_article_hit($articleId)
    {
       global $prefs, $user;

       if ($prefs['count_admin_pvs'] == 'y' || $user != 'admin') {
          $query = "update `tiki_articles` set `nbreads`=`nbreads`+1 where `articleId`=?";

          $result = $this->query($query, array($articleId));
       }

       return true;
    }
  3. Now, modify the above function with the script below:
    function add_article_hit($articleId)
    {
    global $prefs, $user;
    //Identify the name of user agents begin
    if (empty($_SERVER['HTTP_USER_AGENT'])) {
       $client = 'NO USER AGENT';
    } else {
       $client = $_SERVER['HTTP_USER_AGENT'];
    }
    //Identify the name of user agents end
    //Bypass the following list of client sor user agents begin
    if ($client != 'Moreoverbot/5.1 (+http://w.moreover.com; webmaster@moreover.com) Mozilla/5.0'
        && $client != 'Googlebot-Image/1.0'
        && !strpos(mb_strtoupper($client),mb_strtoupper("craw"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("spider"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("bot"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("slurp"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("facebookexternalhit"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("Google Web Preview"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("Butterfly"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("YahooYSMcm"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("SocialSearcher"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("SiteChecker"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("PycURL"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("Voyager"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("Resolver"))
        && !strpos(mb_strtoupper($client),mb_strtoupper("Feed"))
    /* you may add another here */
    ) {
    //add article hit if the user agent is not from the above list begin
        if ($prefs['count_admin_pvs'] == 'y' || $user != 'admin') {
            $query = "update `tiki_articles` set `nbreads`=`nbreads`+1 where `articleId`=?";

            $result = $this->query($query, array($articleId));
        }
    //add article hit if the user agent is not from the above list end
    }
    //Bypass the  following list of client or user agents end
    return true;
    }
  4. Finish. Save and close /lib/articles/artlib.php. When and article page is opened by a visiotor, TikiWiki will identify from what user agent the visitor is coming from. If it is on the list of clients or user agents, article hit should not be calculated.

No comments:

Post a Comment