After I wrote about how to get a Jekyll blog up and running, I want to focus on the next important bit: Analytics.

Since I don’t want to leak the fact, that my blog was visited to Google et al. I went for Piwik.

They offer a 5 minute installation guide which took a bit more effort for me, because I am doing that for the first time.

Preparation

Because Piwik expects PHP and MySQL, prepare a database and tweak your php.ini.

Create a MySQL database

After a bit digging, you’ll find that this is quite easy:

$ mysql
mysql> CREATE DATABASE <USER>_piwik;
mysql> SHOW DATABASES;
mysql> EXIT;
$

Why <USER>_piwik? Because Uberspace requires that prefix.

Tweaking php.ini

This one is documented as well:

test -f ~/etc/php.ini || cp -a /package/host/localhost/php-$PHPVERSION/lib/php.ini ~/etc/
vim ~/etc/php.ini

As the docs explain, test -f checks, whether you already have a copied php.ini.

Download and install

Download the latest zip using wget and unpack it to ~/html as first documented by Christian Vervoorts

mkdir -p ~/tmp && cd ~/tmp
wget https://builds.piwik.org/piwik.zip
unzip piwik.zip
mv piwik/ ../html

Now visit /piwik on your Uberspace instance and follow the instructions. I had to only tweak two things:

  1. Removing something under piwik/plugins/Morpheus/
  2. Setting always_populate_raw_post_data = -1

Adding piwik.js

After you’ve completed your installation, you need to add the analytics script.

Therefore you could use jekyll-analytics but I found it has a small glitch regarding the path concatenation.

My patch made it into 0.1.5 and the following is no longer needed!

So I went a workaround:

<!-- 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="//jaenis.ch/piwik/";
    _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>
<!-- End Piwik Code -->

is saved as _includes/piwik.html.

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>How to setup Piwik on Uberspace</title>
  <meta name="description" content="After I wrote about how to get a Jekyll blog up and running, I want to focus on the next important bit: Analytics.">

  <link rel="stylesheet" href="/blog/assets/main.css">
  <link rel="canonical" href="https://jaenis.ch/blog/how-to-setup-piwik-on-uberspace/">
  <link rel="alternate" type="application/rss+xml" title="André Jaenisch&#39;s blog" href="/blog/feed.xml">
  
  { % include piwik.html % }<!-- remove the spaces there! Currently Jekyll interprets it! -->
</head>

overwrites the default _includes/head.html.

The _config.yml is adjusted as described in jekyll-analytics.

For some reasons not all visits are tracked. I have no idea, why.

Conclusion

Now you are able to get basic analytics about your visitors. This can be used to analyse, which articles went well and where you need more improvement.