Track time spent on a single page visit

Hello everyone,

I’ve been using Matomo since June and I’m having a problem with tracking when a user visits a single page. I’m in a situation where I use a lot of landing pages that don’t aim to generate more than a single page view.

I’m on Matomo On premise and via GTM I’ve installed the following push:

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

Initially I had set it to 1 second but it seems that the minimum is 5 (I can accept losing a little detail, it’s not serious).

Despite this, I’m still getting a 0-second timeout on all visits with a single page view, which is starting to get really annoying.

Do you have any idea how to solve this problem?

Hi @aksoum

I don’t understand clearly what you mean… :thinking:

In order to simplify your implementation (and user privacy), I suggest you use MTM instead of GTM; page views and heartbeat timer are indeed better integrated.

Can you share the full Matomo tracking code?

Hello,

Unfortunately for many reasons I can’t use MTM in the media.

Here is my code

<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(["setExcludedQueryParams", ["xxxxxxxxxx"]]);
  _paq.push(["setExcludedReferrers", ["xxxxxxx\/"]]);


 
  _paq.push(['setCustomDimension', customDimensionId = 1, customDimensionValue = '{{xxxxxx}}']);
_paq.push(['setCustomDimension', customDimensionId = 2, customDimensionValue = '{{xxxxxx}}']);
_paq.push(['setCustomDimension', customDimensionId = 3, customDimensionValue = '{{xxxxxx}}']);
_paq.push(['setCustomDimension', customDimensionId = 4, customDimensionValue = '{{xxxxxx}}']);
_paq.push(['setCustomDimension', customDimensionId = 5, customDimensionValue = '{{xxxxxx}}']);
_paq.push(['setCustomDimension', customDimensionId = 6, customDimensionValue = '{{xxxxxx}}']);
_paq.push(['setCustomDimension', customDimensionId = 7, customDimensionValue = '{{xxxxxx}}']);
_paq.push(['setCustomDimension', customDimensionId = 8, customDimensionValue = '{{xxxxxx}}']);
_paq.push(['setCustomDimension', customDimensionId = 9, customDimensionValue = '{{xxxxxx}}' ]);

  
    _paq.push(['disableCookies']);

  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  _paq.push(['enableHeartBeatTimer', 5]);
  (function() {
    var u="https://xxxxxxxxxxxxxxxx.matomo.cloud/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '{{xxxxxxxxxx}}']);
    
    
    
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src='//cdn.matomo.cloud/xxxxxxxxxx.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g,s);
  })();
  
  
  var my_matomo_heartbeat = setInterval(function() {
  _paq.push(['ping']);
}, 10 * 1000); // a Ping will be send at every 10 seconds
setTimeout(function() {
  clearInterval(my_matomo_heartbeat);
}, 5 * (60 * 1000)); // the HeartBeatTimer will be deactivated after 5 Minutes.
// │     │      └ Milliseconds multiplicator (1000 milliseconds = 1 second)
// │     └─────── Seconds (60 seconds = 1 minute)
// └───────────── Minutes
  
</script>
<!-- End Matomo Code -->```

In the meantime, I've added a ping every 10 seconds.

The
_paq.push(['enableHeartBeatTimer', 5]);
is not a ping intervall. Its a “leave page” detector.

The my_matomo_heartbeat can modified by you. Use a 1 minute intervall. Delete the clearInterval. Or make a addition own my_matomo_heartbeat_2.

var my_matomo_heartbeat_2 = setInterval(function() {
  _paq.push(['ping']);
}, 1 * (60 * 1000)); // a Ping will be send at every 1 minute
1 Like