How to add Piwik Tracking to Discourse forum?

  1. Go to Discourse / Admin / Customize / CSS/HTML / </body> and insert:

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//PIWIK_URL_HERE/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', PIWIK_ID_SITE_HERE]);
    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>
  1. Replace PIWIK_URL_HERE by your Piwik Domain name
    and Replace PIWIK_ID_SITE_HERE by your Piwik Website ID

  2. Save, and click checkbox “Enable”.

  3. Go to Discourse / Admin / Customize / CSS/HTML / </head> and insert:

<script type="text/discourse-plugin" version="0.2">
    // Track page views 
    api.onPageChange((url, title) => {
        if (_paq) {
            try {
                var currentUser = PreloadStore.get('currentUser');
                _paq.push(['setCustomVariable', 3, 'Forum status', !currentUser ? 'Anonymous' : 'LoggedIn user', 'visit']);
            } catch(e) {} 
              
            _paq.push(["setCustomUrl", url]);
            _paq.push(["setDocumentTitle", title]);
            _paq.push(["trackPageView"]);
        }
    });
</script>
  1. Save, and click checkbox “Enable”.

  2. Your discourse forums should now be tracked with Piwik! Congratulations.

1 Like

@matthieu I’m using the proxy script, is there something I should change except g.src=u+'piwik.php'? And why is the tracking script different here on forum.piwik.org?

All pageviews are counted twice in my discourse instance. :frowning:

@analytical follow the same instructions as in: GitHub - matomo-org/tracker-proxy: HTTP proxy for Piwik's tracker API. This script allows to track websites with Piwik without revealing to your visitors the secret Piwik server URL. and discourse piwik tracking will work also with the tracker script.

Thanks, it works now with your updated instructions above (btw. the closing script-tag is missing). As far as I can tell the only change necessary is replacing the URL and g.src=u+'piwik.js' for the proxy script.

This doesn’t seem to work fully anymore, specifically this part:

try {
    var currentUser = PreloadStore.get('currentUser');
    _paq.push(['setCustomVariable', 3, 'Forum status', !currentUser ? 'Anonymous' : 'LoggedIn user', 'visit']);
} catch(e) {}

Any idea what the current configuration should be? I never see the custom variable sent when monitoring the traffic.

Getting piwik work in an Discourse site is one thing, complying with european law may be even more difficult :disappointed_relieved:

Piwik’s cookie opt-out switch should be implemented by including an <iframe . Unfortunately, Discourse doesn’t allow iframe tags in posts… :grimacing:

Does anyone see a way to include this piwik opt-out iframe in Discourse TOS ?
Thanks in advance!