Hosting Piwik.js on a CDN

ShareTweet about this on TwitterShare on Google+Share on FacebookShare on LinkedInShare on StumbleUponEmail this to someone

piwik-logoI use Piwik to monitor a couple of sites. If you aren’t familiar with it, it’s an alternative to Google Analytics which you host yourself. I installed it as an experiment and fell in love with it. The thing is, most of my sites are hosted in a shared environment, yet I’m a bit obsessive compulsive about page load times. I host all of my static content (images, CSS & JS) on a CDN.

When you install Piwik it will prompt you to use the piwik.js file on the same domain where Piwik is running. Nothing wrong with that, but if you’re using a shared webhost like me that can add seconds to your page load times. Unacceptable. The difference is in the g.src variable of the tracking code.

Change the g.src to point to the piwik.js file on your CDN and you’re set. That’s it! Here’s the complete code:

The code still loads asynchronously (notice that g.async is set to true), so the only difference is that the piwik.js file will load much, much faster.