Awesome Piwik setup – CDN, evading adblockers, GeoIP and more

By | 17th October 2016

My setup guide heavily depends on Nginx so you may have to change a few things if you are using some other web server. But this should be helpful never the less. πŸ™‚

I was looking for a nice Google Analytics alternative since I did not want to trust my user’s data with Google. I stumbled upon Piwik and simply was blown away by right from how nice the UI looked to the customisation to the fact that it was completely open source and you could just install it on your own server. Because of this you retain 100% ownership of your data.

How to install Piwik

I simply used this guide to get Piwik running. Installation was a breeze and it didn’t mess up even with a SSL enabled reverse proxy, like WordPress likes to do. Make sure you get the nginx-custom package from the RTcamp repo. If you are using Debian Dotdeb should also work. Make sure you use PHP 7.0 instead of 5.6 mentioned in the article. Choose a nice domain/subdomain and make sure you enable SSL if any of your websites use it and if you choose to do so make sure it is enabled before running the installation. Should make your life easier. I also had to install php-mbstring and a few other PHP extensions which I can’t remember.

Making it work with WordPress

Look at this list for a list of integrations that Piwik has to offer. I use WordPress for my blog so I went with WP-Piwik but it just refused to work, throwing a 5000 ms timeout at me. After trying to make it work for hours, I simply gave up and just put in the analytics code that Piwik gave me just before closing the head section of the header.php file. Do note that you will lose changes on theme updates so it would be a good idea to look at creating a WordPress child theme.

screenshot-from-2016-10-17-08-14-27-1

The Piwik dashboard looks breathtakingly good

After you get it running, the Piwik dashboard should start showing user visits. The level of detail is simply amazing. You can know the IP address of a person which can be used to auto detect country, city, ISP and ASN(we’ll talk about this later), user resolution, browser, OS, time on each link he/she clicked, referrer etc. The amount of detail is simply astounding.

Moving JS to CDN and evading adblockers

I use Cloudfront for images and that helps massively with the load times. I simply moved https://domainforanalytics.com/piwik.js file over to my S3 bucket and it was accessible right away from my Cloudfront enabled domain. Make sure you rename the file without the word piwik in it. This is because adblockers simply reject. Link to my piwik.js file is https://cdn.varunpriolkar.com/varunpriolkar.js. You can add cache control headers in S3 itself. If you do not want to use a CDN just renaming it to whatever you like should work.

That is not enough. The adblockers still manage to block out piwik.php file. So just rename it from your web root folder. I named mine varunpriolkar.php. Don’t forget to set proper permissions. Next open up /etc/nginx/apps/piwik/piwik.conf. Locate location = /piwik.php and rename it to location = /varunpriolkar.php . Replace varunpriolkar.php with whatever you had renamed the file to. Do note that varunpriolkar.php can become stale or can be overwritten on updates so be careful!

EDIT 17/10/2016: My friend Sunit did point out that you can use URL rewrites to achieve this but I prefer to do it this way. I find rewrites to be a little messy.

Next we will have to modify the tracking code we had put in the document head. I had to change g.src=u+’piwik.js’ to g.src=u+’https://cdn.varunpriolkar.com/varunpriolkar.js’ , <noscript><p><img src=”//analytics.varunpriolkar.com/piwik.php?idsite=1″ style=”border:0;” alt=”” /></p></noscript> to <noscript><p><img src=”//analytics.varunpriolkar.com/varunpriolkar.php?idsite=1″ style=”border:0;” alt=”” /></p></noscript> andΒ _paq.push([‘setTrackerUrl’, u+’piwik.php’]); to _paq.push([‘setTrackerUrl’, u+’varunpriolkar.php’]); . My final analytics code looks something like this.

Leave a Reply