Using Piwik PRO Tag Manager to track accurate time spent in Piwik

Published: June 14, 2016 Updated: August 24, 2018 Author Category Tag Manager, Use cases

Why would you want to implement this?

By default, when a visitor only visits one page, Piwik (and Google Analytics as well) tracks the entire session as 0 seconds. This happens because of the way Piwik calculates the time spent on a page – comparing the time of the first pageview to the time of the next one.

Happily, it is possible to configure Piwik so that it more accurately measures the time spent on the last page of a single visit. Unfortunately, we need to add a special line into the Piwik tracking code.

Fortunately, however, we can do this by firing another tag using the Piwik PRO Tag Manager.

Free Comparison of 6 Enterprise-Ready Tag Management Systems

Get to know the 40 key differences between Google Tag Manager, Tealium, Segment, Adobe Marketing Cloud, Ensighten & Piwik PRO:

Download FREE Ebook

Ready to find out more? Continue reading…

Piwik PRO Tag Manager Implementation

We are assuming that you have your instance of Piwik PRO Tag Manager installed. And, hopefully, you have used some tags already because the first step to implementing accurate time spent is, of course, to + Add a tag (for more information on this topic check the Tags User Guide):

Piwik PRO Tag Manager - adding a tag
Piwik PRO Tag Manager - adding a tag

Next we select an appropriate Tag name (1) and choose Asynchronous custom HTML tag (2) from the templates section:

New tag configuration
New tag configuration

Now we have to insert this special line of code we mentioned earlier into the Tag HTML field. To make this work correctly, we need to put this bit of code into the <script></script> tags:

Inserting additional Piwik code
Inserting additional Piwik code

Here you can copy the code:

<script>
_paq.push(['enableHeartBeatTimer']);
</script>

This is a good place to stop and explain a bit more about this code. In its standard form, it tells Piwik to send requests to count the actual time spent on the page, in other words, when the user is actively viewing the page (i.e. when the tab is active and in focus). These heartbeat requests will not track additional actions or pageviews.

By default, Piwik will send a heartbeat request every 15 seconds. But if you want to change it, for example to send a request every 30 seconds, the code will looks like this:

<script>
_paq.push(['enableHeartBeatTimer', 30]);
</script>

Now we have to configure the trigger. The best option will be to fire this tag across the entire website, unless you have some good reasons to limit the firing to specific URLs. We are going to fire this on all pages. See how easy it is:

  1. Set an easily recognizable Trigger name – you can reuse it later.
  2. Choose Page View here and…
  3. … here.
  4. Leave this as is – look at the text: Fire tag on all pages – exactly what we want to do.
  5. Accept by clicking Add.
Creating trigger for all pages of all websites
Creating trigger for all pages of all websites

Now just Save the whole configuration and publish your tag by clicking Publish:

Publishing last changes
Publishing last changes

Free Comparison of 6 Enterprise-Ready Tag Management Systems

Get to know the 40 key differences between Google Tag Manager, Tealium, Segment, Adobe Marketing Cloud, Ensighten & Piwik PRO:

Download FREE Ebook

Tagged under

Author:

Szymon Grzesiak, Marketing Tech Lead

Tech savvy Performance Marketing Specialist. LinkedIn Profile

See more posts of this author
Free Comparison of 5 Enterprise-Ready Tag Management Systems

Share