Advertisement
  1. Code
  2. Coding Fundamentals

Self-Hosting Your Own Analytics Server With Piwik

Scroll to top
4 min read
Final product imageFinal product imageFinal product image
What You'll Be Creating

Piwik may be the most polished open source analytics tool you've never heard of. Not only has it had more than two million downloads, it has a rich set of features and excellent documentation. This tutorial will describe how to install and use it to track web traffic to your own sites.

If you prefer to pay to have someone else run it for you, they offer several cloud-based options. Piwik offers a 30 day free trial with $65 monthly pricing afterwards. There's also a community of paid consultants you can hire to assist you.

Benefits to Hosting Your Own Analytics

I've been a long-time user of freemium, cloud-service analytics provider Statcounter. I like being able to see specific user activity and IP address traffic. While Google Analytics doesn't provide this information, Statcounter lacks many of the features that Google Analytics provides for free.

Enter Piwik, a mature, well-documented, full-featured open source analytics product written in PHP; it even offers free mobile applications for iOS and Android. 

Piwik Offers a Mobile AppPiwik Offers a Mobile AppPiwik Offers a Mobile App

The team has upcoming plans to offer a tool to migrate Google Analytics data to Piwik in 2015.

Installing Piwik on Your Own Server

Piwik runs on any modern LAMP-based server configuration. If you'd like, you can begin with my generic Ubuntu LAMP installation here.

Then, follow my installation instructions below and/or watch the installation video:

First, let's download the open source Piwik code and create a web directory for your dashboard:

1
mkdir /var/www/piwik
2
cd /var/www/piwik
3
sudo wget http://builds.piwik.org/piwik.zip
4
sudo unzip piwik.zip 
5
sudo rm piwik.zip
6
sudo rm How\ to\ install\ Piwik.html 

Change the ownership and permissions appropriate to your web server:

1
chown -R www-data:www-data /var/www/piwik
2
sudo mkdir /var/www/piwik/tmp
3
sudo mkdir /var/www/piwik/tmp/assets/
4
sudo mkdir /var/www/piwik/tmp/cache/
5
sudo mkdir /var/www/piwik/tmp/logs/
6
sudo mkdir /var/www/piwik/tmp/tcpdf/
7
sudo mkdir /var/www/piwik/tmp/templates_c/
8
sudo chmod  -R 0755 /var/www/piwik/tmp
9
sudo chmod  -R 0755 /var/www/piwik/tmp/assets/
10
sudo chmod  -R 0755 /var/www/piwik/tmp/cache/
11
sudo chmod  -R 0755 /var/www/piwik/tmp/logs/
12
sudo chmod  -R 0755 /var/www/piwik/tmp/tcpdf/
13
sudo chmod  -R 0755 /var/www/piwik/tmp/templates_c/

Create an Apache site configuration file and paste the Virtual Host definition below into it. Then, enable the site.

1
cd /etc/apache2/sites-available/
2
sudo nano stats.conf
3
sudo a2ensite stats.conf

Paste in and customize the virtual host definition for your Piwik site as shown below:

1
<VirtualHost *:80>
2
   ServerName piwik.yourdomain.com
3
   DocumentRoot /var/www/piwik 
4
   DirectoryIndex index.php
5
   <Directory /var/www/piwik/>
6
      AllowOverride All
7
      Order Deny,Allow
8
      Allow from all
9
   </Directory>
10
</VirtualHost>

Setting Up Piwik

You can follow the Piwik five minute installation guide if you'd like more details. The installation wizard you should see on your site is quite simple:

Piwik Installation Wizard WelcomePiwik Installation Wizard WelcomePiwik Installation Wizard Welcome

It performs a system check, which should pass if you followed my LAMP configuration guide:

Piwik Installation Wizard System CheckPiwik Installation Wizard System CheckPiwik Installation Wizard System Check

Enter in the database permissions that you created from the configuration guide—when you created your MySQL database:

Piwik Installation Wizard MySQL Database SetupPiwik Installation Wizard MySQL Database SetupPiwik Installation Wizard MySQL Database Setup

You should see this:

Piwik Installation Wizard Database CreatedPiwik Installation Wizard Database CreatedPiwik Installation Wizard Database Created

Specify your administrator or superuser login credentials:

Piwik Installation Wizard SuperuserPiwik Installation Wizard SuperuserPiwik Installation Wizard Superuser

Now, you can add your first website to track:

Piwik Installation Wizard Add Your SitePiwik Installation Wizard Add Your SitePiwik Installation Wizard Add Your Site

Keep in mind that the site isn't actively tracking yet. We'll do that below.

If you log out, you'll be greeted with the Piwik sign-in. Enter your administrative credentials chosen above:

Piwik Sign InPiwik Sign InPiwik Sign In

Enabling Tracking From Your Sites

There are a couple of ways to enable tracking on your website.

Embedding JavaScript

Just like with Google Analytics, you can paste the Piwik JavaScript into your website code:

Piwik Tracking Code in JavascriptPiwik Tracking Code in JavascriptPiwik Tracking Code in Javascript

WordPress Plugin

Or, you can install the WP-Piwik plugin:

WP-Piwik Plugin for WordPressWP-Piwik Plugin for WordPressWP-Piwik Plugin for WordPress

Next, visit your Piwik API settings and copy your authentication token—just the parameter value (mine is blacked out below).

Piwik API KeyPiwik API KeyPiwik API Key

Visit the WordPress WP-Piwik Settings page and provide your Piwik URL, the domain you set up to host Piwik, and the auth token. 

WP-Piwik Plugin for WordPress SettingsWP-Piwik Plugin for WordPress SettingsWP-Piwik Plugin for WordPress Settings

Don't forget to visit the Tracking tab under settings to turn on the tracking code on your WordPress site:

WP-Piwik Plugin for WordPress TrackingWP-Piwik Plugin for WordPress TrackingWP-Piwik Plugin for WordPress Tracking

The Piwik Dashboard

You should be able to see data coming in immediately from the real-time dashboard panel:

Piwik Real Time Visitor PanelPiwik Real Time Visitor PanelPiwik Real Time Visitor Panel

Once Piwik's been collecting data for a few days, you should see something like this in the Piwik Dashboard when you log in:

Piwik DashboardPiwik DashboardPiwik Dashboard

All the sites you've added can be viewed at a glance:

Piwik All SitesPiwik All SitesPiwik All Sites

Here's what a summary of traffic by page title looks like:

Piwik Visitor Traffic by Page TitlePiwik Visitor Traffic by Page TitlePiwik Visitor Traffic by Page Title

Here's a visitor log for all traffic:

Piwik Visitor LogPiwik Visitor LogPiwik Visitor Log

And Piwik has a variety of other features such as device, operating system and browser statistics:

Piwik Devices and Operating SystemsPiwik Devices and Operating SystemsPiwik Devices and Operating Systems

The Piwik administration settings are well designed and very thorough. It's unusually good for an open source project:

Piwik SettingsPiwik SettingsPiwik Settings

Piwik Mobile Applications

There are also free Piwik mobile apps available at the AppStore and Google Play, so you can keep up with your site traffic on the go more easily:

Piwik iOS and Android Mobile AppsPiwik iOS and Android Mobile AppsPiwik iOS and Android Mobile Apps

Special Considerations

Piwik has a wide variety of capabilities. Here are a few things you may wish to update in your own system.

Anonymizing IP Addresses

You can change how much of IP addresses Piwik collects—this affects the relative privacy of your users:

Piwik Anonymize IP AddressesPiwik Anonymize IP AddressesPiwik Anonymize IP Addresses

Excluding Your Visits

You can set a cookie on your browser to prevent Piwik from adding traffic of your own editing and browsing from being counted:

Piwik Exclude Your VisitsPiwik Exclude Your VisitsPiwik Exclude Your Visits

Rolling Up Database Logs

You can also set up Piwik to automatically roll up its logs and reports, to prevent the database from growing too large over time:

Piwik Rollup LogsPiwik Rollup LogsPiwik Rollup Logs

Where to Go From Here

Hopefully you've found Piwik useful and interesting. The team has a variety of ways for you to learn more and engage with its community:

I've been impressed with the clean design and relatively robust feature set provided by this free, open source project.

If you have any questions or suggestions, please post them in the comments. If you'd like to keep up on my future Tuts+ tutorials and other series, please visit my instructor page or follow @reifman.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.