Use Piwik for more Reliable Visitor Statistics (avoid referral spam and ad blockers)

Jun 11, 2016 03:06 · 320 words · 2 minute read

  1. Ad Blockers: A rising number of users blocks ads with these browser plugins. All of them will prevent GA from loading. On tech-heavy sites this will lose you around 13 of visitors. Even iOS supports Ad Blockers.

    Google Analytics blocked by Ad Blocker.
    Google Analytics blocked by Ad Blocker.
    • Referral Spam: Due to the client-side nature of Java Script and thus Google Analytics, it’s easy to fake website visits and just poison your statistics with any data. Spammers know that webmasters are interested in sites referring to them. So they place spam links to lure them to e-commerce or malware sites.


Piwik allows you to self-host a statistics system very similar to GA with most of the features and none of the issues described above. By default Ad Blockers will block piwik as well, but since it’s under your control, you can simply redirect your endpoints. For Nginx these lines are enough:

# Avoid AdBlockers
rewrite ^/pk.php /piwik.php;
rewrite ^/pk.js /piwik.js;

Then your tracking code should point to the new endpoints:

<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
(function() {
var u="//MYHOST.COM/";
_paq.push(['setTrackerUrl', u+'pk.php']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'pk.js'; s.parentNode.insertBefore(g,s);
<noscript><p><img src="//MYHOST.COM/pk.php?idsite=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

When visiting this site with an Ad Blocker, you should still see your stats coming in.

As an added benefit there are no cookies involved. So you don’t need one of these annoying notices.


Is it ethical to track people who are using Ad Blockers? Most users use these plugins to avoid downloading ads and thus get a faster browsing experience. Their next-highest concern is probably being tracked across all sites to serve them personalized ads. The data your one site is gathering won’t be correlated with their Facebook and Google profile. So I’d say it’s OK to use this technique. The same data can be obtained from web logs.