• How To Avoid Bots When Using Image Tracking in Piwik Analytics

    by  • Last modified:March 16, 2013 • Piwik

    Javascript Free Tracking

    There are a couple of ways to track your visitors, without javascript, for example using the Piwik PHP API. But sometimes, you just need a quick fix or a simple solution. In case you were not aware, by default the standard Tracking Tag for Piwik uses Javascript. This means, Piwik will only track visitors with Javascript enabled in their web browser. If you happen to be in a niche, where lots of users have noscript enabled, you will lose a percentage of your visitors. Out of the box Piwik offers a way to track users with Javascript disabled. The code looks like below.

    <noscript><p><img src="http://localhost/piwik.php?idsite=1&rec=1" style="border:0" alt="" /></p></noscript>

    With this code, you will get only basic information of a hit to your website. You will not get custom variables, you will not get technical information about the user (i.e. Browser, Screen Size ect). And you will also face a bigger issue…

    Rise of the Bots
    Bots. Yes, those lovely creatures. Well some are not bad, such as Google or Bing who crawl our site and help with getting you traffic. Nevertheless, they can also cause your trouble, by triggering your image tracker and in a really bad case cause server slowdown.

    Solution
    Thankfully there is a simple solution, you can modify the code. All you need to do is replace the standard image tracking code with the below:

    <noscript>
    <?php
    if (preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
    //do not fire tracker  
    }
      else {?>
    <!-- Piwik Image Tracker -->
    <img src="http://localhost/piwik/piwik.php?&idsite=1&amp;rec=1&urlref&_cvar={&quot;5&quot;:[&quot;Non Bot&quot;,&quot;No Javascript&quot;]}" style="border:0" alt="" />
    <!-- End Piwik -->
     <?php }?>
    </noscript>
    

    All you need to do is change the url and the siteid to the appropriate information. After that, this code will start tracking only when a user has noscript enabled and their user agent does not meet one of the patterns listed (i.e bot). It will also create a custom variable in slot 5 with information that the visit was a Javascript disabled/non-bot visit. If there are things you want to add or delete to the image tracking, you can read all the options at the Piwik wesbite and learn more about customizing the image tracker.

    About

    About The Author: Adrian has over 10 years experience in Web Marketing and Analytics. He currently works for one of the world's largest electronic component distributors as a Web Analytics Manager, handling their analytics needs worldwide. Adrian is also certified in Google Analytics and you can find him working with the team at Piwik.org in his spare time.