summaryrefslogtreecommitdiffstats
path: root/content/posts/piwik-in-docker-for-pelican.rst
blob: 1bf5df0310fce9a24469d51005c46f138897041c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Piwik for this blog
####################
:date: 2014-02-18 08:00
:tags: piwik, docker, pelican

I was looking for a simple analytics tool for this blog. As everyone seems to
be using **piwik** and everyone loves **docker** for rapid deployment. As i am
always short on resources on all my machines i decided to deploy piwik on
another of my VMs which is ``mediengewitter.krebsco.de`` instead of directly on
``euer.krebsco.de`` Again it was a case of 'how-hard-can-it-be?'. 
Turns out everything is much harder than expected. 
As always this this is the digest of some hours work.

Installing piwik in docker
==========================
After testing out all kinds of non-working Dockerfiles, this is what worked for
me:

.. code-block:: bash

    git clone https://github.com/makefu/docker-piwik.git piwik
    cd piwik
    ./build

    # if you want persistence, see:
    mkdir -p /media/ext/piwik/{www,mysql}
    cat README

Running piwik
=============
Because docker handles it's own network, the docker image port 80 must be
forwarded to the host (i use 10000). Also i want data persistence via exported filesystems.

.. code-block:: bash
    
    docker run -p=10000:80 -d
      \ -v /media/ext/piwik/www:/var/www 
      \ -v /media/ext/piwik/mysql:/var/lib/mysql piwik 
    # or ./run

Adding piwik to pelican
=======================
Integration of piwik tracking to pelican **should** be straight forward, just
add the following to your ``pelicanconf.py``:

.. code-block:: python
    
    PIWIK_URL='mediengewitter.krebsco.de:10000'
    # first piwik site is always id 1
    PIWIK_SITE_ID=1

Bit nothing seemed to be happening, turns out my theme of choice does not
support piwik so i had to add this feature explicitly to the skin (by stealing
the code from another theme). I created a pull request for my code:
https://github.com/getpelican/pelican-themes/pull/195 .
If it will never be merged, use my repository for themes:

.. code-block:: bash

    cd <pelican-dir>
    git clone git@github.com:makefu/pelican-themes.git -b add-piwik-to-gum themes

Add piwik to another skin
=========================
If you are using another skin without piwik integration, this is basically what you
need to do:
   
1. Add piwik.html to '``<skin>/templates``'

.. code-block:: html

    {% if PIWIK_URL and PIWIK_SITE_ID %}
    <script type="text/javascript">
    {% if PIWIK_SSL_URL %}
        var pkBaseURL = (("https:" == document.location.protocol) ? "https://{{ PIWIK_SSL_URL }}/" : "http://{{ PIWIK_URL }}/");
    {% else %}
        var pkBaseURL = (("https:" == document.location.protocol) ? "https://{{ PIWIK_URL }}/" : "http://{{ PIWIK_URL }}/");
    {% endif %}
    document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
    </script><script type="text/javascript">
    try {
    var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", {{ PIWIK_SITE_ID }});
    piwikTracker.trackPageView();
    piwikTracker.enableLinkTracking();
    } catch( err ) {}
    </script><noscript><p><img src="http://{{ PIWIK_URL }}/piwik.php?idsite={{ PIWIK_SITE_ID }}" style="border:0" alt="" /></p></noscript>
    {% endif %}

2. Add this line somewhere near the end but before </body></html> to '``<skin>/templates/base.html``':

.. code-block:: html

    ...
    {% include 'piwik.html' %}
    ...