Tag Archives: piwik

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 + document.location.search) + '/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 + document.location.search) + '/From = ' + encodeURIComponent(document.referrer)]);
  <? } ?>
  _paq.push(['enableHeartBeatTimer', 5]);
  (function() {
    var u="https://where.my.piwik.is/";
    _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="https://where.my.piwik.is/piwik.php?idsite=1" 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.

Using Piwik stats but getting ‘Page url not defined’ all the time?

For a while now I’ve been using Piwik stats instead of Google Analytics.
And it works nicely, mostly. Atleast the gathered stats are understandable now and I am much more up-to-date with trends and things on my site when it comes what you visitors do on it. This allows me to make a better site 🙂

But one thing, didn’t work… Page titles. Nobody seemed to report their page title. And as such a bunch of stats don’t work right.
If you have this annoying phenomenon, too. The fix is simple. Change your tracking code on your sites.

Tired of seeing “Page url not defined” in your page views?

After some searching I found that this may happen (and does happen a lot) on sites that do not use HTTPS while the Piwik sever is on HTTPS. That in itself is no problem. but Piwik assumes HTTP. If the HTTP request then gets redirected to HTTPS some variables are lost for some stupid reason.

So on each site you track with Piwik you simply have to replace the Agnostic link with a proper one. There are 2 links in the tracking code. Both start with //. Change that to either http:// or https://, depending on what URL your Piwik server operates on.


Save and upload your snippets and page titles and similar stats will be reported much more accurately.