Avoid ad blockers with Piwik Analytics

For many reasons you may want to circumvent an Ad Blocker that's blocking your analytics script. Ad Blockers may block these scripts to curb invasive marketing tracking from third party advertisers i.e. Google. If you're using Piwik Analytics (or most other self-hosted systems) then you're in luck, you can rewrite the url to avoid the Ad Blocker.

Image: Google Analytics & Piwik Analytics requests blocked
pw-blocked.png#asset:66

We'll rewrite the url in .htaccess, you can change the path to anything you want. (Make sure you set the "analytics" text sections to your actual path where Piwik is installed).

RewriteEngine On
RewriteRule ^proxy-analytics/(.*) /analytics/piwik.$1 [L]

Then we'll need to update the tracking code for Piwik to work with the rewrite, here's what your code should look like before the change.

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//www.example.com/analytics/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', 2]);
    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="//www.example.com/analytics/piwik.php?idsite=2" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

And here's the lines to change: (Replace "analytics" text sections to your actual path where Piwik is installed).

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//www.example.com/proxy-analytics/";
    _paq.push(['setTrackerUrl', u+'php']);
    _paq.push(['setSiteId', 2]);
    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="//www.example.com/proxy-analytics/php?idsite=2" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

Line 7: You'll need to update the url to the path that is your directory rewrite from the htaccess file.
Line 8 + 11: Remove piwik. from these lines, ad blockers may target piwik.[php/js] or like ABP they may target just the word piwik.
Line 14: Lastly, you'll need to update the path like on line 7, and again remove piwik. from the image source. The htaccess rewrite prefixes the path with piwik. so that everything still works.

Once you've made this change you should immediately be able to collect Piwik Analytics data through most Ad Blockers.

Image: Google Analytics blocked & Piwik Analytics request not blocked
pw-unblocked.png#asset:67

This method has been tested and verified for use with 'Adblock Plus' but should work with most other Ad blockers. If you have any questions about Piwik with Ad Blockers, leave a comment or tweet me @WilliamIsted

Update: Interestingly I've just noticed that ABP is actually stopping all of the screenshot images with piwik in the name from displaying. That seems like rather aggressive filtering.