Adjusted bounce rate and track 404 page requests in Piwik Analytics

piwikOver the last few days I’ve been doing some research about how Piwik Analytics reports bounce rates – The same way as Google Analytics – and what to do about it.

Recently I launched a satellite site as a sort of landingpage for AdRotate Pro to drop the bounce rate on my primary site. But then, since the landingpage has no other pages the bounce rate there was 100%. Just because there are no more pages than the front-page.

Heartbeat timer

To try and counter that Piwik Analytics has introduced a heartbeat timer in their latest version of Piwik. I found this in their API documentation and was recommended to me on the forums when asking about it.

Starting with Piwik version 2.14 you can use a heartbeat timer. And it looks like this:


You can pass a variable to it to make the heartbeat faster or slower. The default value is 15 seconds. For my sites I set it to 5 seconds. Mainly because a bunch of pages and stuff are small and easily interpreted. A visitor may stay shorter than 15 seconds and leave, the heartbeat timer helps measure the time spent on websites more accurately. Which in turn will tell you if the bounce rate is actually worthwhile to look at.

If you want a different heartbeat time, too. You edit the line to be something like this:

_paq.push(['enableHeartBeatTimer', 5]);

The number is in seconds.

404 page requests

To better understand why people end up on 404 pages on your site you can add the following to the tracking code, too. This works only for WordPress:

<?php if(is_404()) { ?>
_paq.push(['setDocumentTitle', '404/URL = ' + encodeURIComponent(document.location.pathname + + '/From = ' + encodeURIComponent(document.referrer)]);
<? } ?>

Basically what this does is if the page is a 404 error page it will pass an extra set of variables to Piwik so you get a clear insight of which page was requested and where that request came from. As opposed to “there have been 20 people on a 404 page”. Which is the default Piwik behavior.

With this addition your page titles in Actions > Page Titles will look like this:

Fold out each entry to see where it came from. So you know what to do about it.
This is useful for tracking broken internal links for example. But also to fix links from 3rd parties if that’s in your control.
I actually found this on the Piwik forum somewhere and tailored it a bit to my needs.

Tracking code

With these 2 additions my tracking code for this website now looks something like this:

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  <?php if(is_404()) { ?>
  _paq.push(['setDocumentTitle', '404/URL = ' + encodeURIComponent(document.location.pathname + + '/From = ' + encodeURIComponent(document.referrer)]);
  <? } ?>
  _paq.push(['enableHeartBeatTimer', 5]);
  (function() {
    var u="";
    _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);
<noscript><img src="" style="border:0;" alt="" /></noscript>
<!-- End Piwik Code -->

I found these useful for me, and use them on all my sites now to get an even better insight in what’s going on with my pages.
Note: Both these methods are available for Google Analytics, too. If you prefer that. Just look for the methods on Google.