Track Adblock and uBlock Users with Piwik

I’ve personally run into the issue of not being able to track users who have Adblock, uBlock or similar add-ons and I’ve come up with a solution that is guaranteed to work across them all. This is only possible thanks to the fact that you can install and configure Piwik however you like because it is available under the GPLv3 license, unlike third party providers like Google Analytics or Yahoo Analytics (Flurry) which you have little to no control over.

Let us take a look at how we can use Piwik to track users with ad blockers and other privacy plugins.

How Ad Blockers Work

Simply put, ad blockers have a blacklist which contain patterns for ad or tracking URLs which they match to and then remove from the page or even block the browser from sending a request to that URL.

For example, the Easy Privacy list – which ensures the users privacy in add-ons like Adblock or uBlock – contains the following patterns for Piwik:

/piwik-$domain=~github.com|~piwik.org
/piwik.$script,domain=~piwik.org
/piwik.php
/piwik/*$domain=~github.com|~piwik.org
/piwik1.
/piwik2.js
/piwik_
/piwikapi.js
/piwikC_
/piwikTracker.

Basically, all we have to do is remove all links with Piwik in the tracking code and we’ve effectively bypassed any of these and also patterns from other privacy lists.

Modifying the Piwik Tracking Code

Let’s take a look at the HTML code generated by Piwik for embedding in your website. The following example is generated with only the checkbox Track users with JavaScript disabled checked, all other options were left unchecked.

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//piwik.szibele.com/";
    _paq.push(['setTrackerUrl', u+'piwik.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+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<noscript><p><img src="//piwik.szibele.com/piwik.php?idsite=1&rec=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

As we can see from the above code, the name Piwik is included in the tracking links. We will need to remove every occurrence of Piwik from those links or else they will get blocked. I run my Piwik instance on the subdomain piwik.szibele.com, so the first logical step is to set up another subdomain without Piwik in it. In my case I’ve set up a second subdomain p.szibele.com and only use that for tracking.

If you have Piwik set up in a subdirectory on your website, your best bet is to use mod_rewrite on Apache or rename the piwik subdirectory to /p/ or anything else unrelated to web analytics or Piwik.

The next step that we must take, is to change the files piwik.js and piwik.php to js/. The js/index.php is used for compressing the Piwik Javascript file and is also used for GZIP compression and caching. However, it can also be used for avoiding those filters. We also want to modify the tracking pixel and remove index.php from it.

So, lines 9 and 12 above become:

_paq.push(['setTrackerUrl', u+'js/']);

and

.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'js/'; s.parentNode.insertBefore(g,s);

respectively.

The tracking pixel code on line 15 becomes:

<noscript><p><img src="//p.szibele.com/js/?idsite=1&rec=1" style="border:0;" alt="" /></p></noscript>

The Resulting Code

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
        var u="//p.szibele.com/";
        _paq.push(['setTrackerUrl', u+'js/']);
        _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+'js/'; s.parentNode.insertBefore(g,s);
  })();
</script>
<noscript><p><img src="//p.szibele.com/js/?idsite=1&rec=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

As you can see from the Javascript code above, the links no longer contain the name Piwik, and therefore the ad blockers and privacy add-ons can’t bypass our tracking. If you wanted to go the extra mile, you could even go ahead and do some more trickery to conceal your tracking even further, but as this already bypasses all the ad blockers and privacy add-ons, I wouldn’t see why you would want to.

If the developers for the ad blockers and privacy add-ons do find another, more sophisticated approach to blocking, then I’ll be sure to post a follow-up going the extra mile.

One Reply to “Track Adblock and uBlock Users with Piwik”

Leave a Reply

Your email address will not be published. Required fields are marked *