Heartbeat Timer tracks when tab is minimized

Hi there!

I’ve enabled the Heartbeat Timer for one of our websites so it pings every 15 seconds. I installed the following code via the Matomo Tag Manager with the custom html feature.

<script type="text/javascript">

 setInterval(function () {
 window._paq = window._paq || [];
  }, 15000);


When I tested it in the realtime dashboard it worked fine. The only problem is the pinging continues even if the tab is not in focus or the whole browser is minimized which leads to highly inaccurate visit durations.
As far as I understand, the heartbeat timer is only supposed to ping when the tab is active and in focus. Did I get that wrong or is my implementation somehow wrong? Does anyone have the same problem?

Thanks in Advance!

Hello, the function of the heartbeat was changed in the year 2020. It’s not longer a setInterval ping. It detect page leave and similar. The number is time in seconds in a setTimeout, also not before [seconds] the function works. 5 seconds are the minimum.

Edit: sorry…
_paq.push(['enableHeartBeatTimer']); has changed shes function.

Your script is not optimal.

 window._paq = window._paq || [];
// here the matomo tracking code

var matomoheartbeat = setInterval(function () {
}, 15000);

window.addEventListener('focusout', function(e) {

Thank you very much! I tried using your code but I still have the same problem as before. Even if the tab is minimized or when I tested it on my phone, when I locked the phone, the timer continues to track in the given interval. I also made sure that there wasn’t the old code in the cache of the browser. I installed the code like this via the custom html feature in the matomo tag manager:


I wasn’t sure what exactly to put in “here the matomo tracking code” since the regular matomo tracking code is included with a matomo config variable, which is loaded through another tag in the tag manager.
Do you see another mistake?

The matomo tag manager is not my field. I don’t have any knowledge over there and I’m not involved in this. Sorry. Maybe this works:

var _paq = window._paq = window._paq || [];
var u="//www.example.com/";
_paq.push(['setTrackerUrl', u+'matomo.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+'matomo.js'; s.parentNode.insertBefore(g,s);
// ↓ HeartBeat
var matomoheartbeat = setInterval(function () {
}, 15000);
// ↓ Clear HeartBeat on focusout
window.addEventListener('focusout', function(e) {


  • set the URL of your matomo installation.
  • set the Site-ID.
  • the “focusout” event works eventually not on all devices.

Perhaps there are other events that are triggered when the display is turned off.

1 Like

Unfortunately this still doesn’t work for me. But thank you very much anyways!

Have you tested the


This is (since 2020) not a interval, it is a leave page detector.

Hi @Florian_Rebmann
Did you achieve to detect the tab leave?
A little question: Which behavior do you expect when a user leaves a table, then comes back after 10 minutes or 1 hour?..

@heurteph-ei , the first problem is here a solution for Tag Manager. For this i have no knowledge. The second is to find a detector for “when I locked the phone”. Another problem perhaps, that the focusout are not working.

@heurteph-ei Unfortunately I didn’t find a solution for this yet. Ideally if the user leaves a site (minimizes the browser or changes the tab) and then comes back after 10 minutes the time on site should start again and should be inactive while the tab is not in focus. So for example I’m 1 minute on the site and then change the tab, the timer should stop until the site is in focus again. Otherwise I get way too high times on site that don’t reflect the user engagement and destort the average time on site.

@melbao I also tried to implement the heartbeattimer with your latest suggestion. I included it like this via Matomo Tag Manager with a custom HTML tag:

Unfortunately this way, I don’t get any pings that influence the time on site.

The matomo HeartBeatTimer is since year 2020 no longer a setInterval. It is a focusout detector and similar. It has a setTimeout in seconds. Default 15 seconds, minimum 5 seconds.
_paq.push(['enableHeartBeatTimer', 30]);
The HeartBeatTimer send a ping at:

  • switching to another browser tab after the current tab was active for at least 15 seconds (can be configured see below).
  • navigating to another page within the same tab.
  • closing the tab.

Note: When testing the heart beat timer, remember to make sure the browser tab has focus and not e.g. dev tools. = consider: when the browser console is in focus, the browser tab is out of focus.



1 Like

Thank you so much for clearing it up! I’m sorry, I didn’t get it the first time. I now tested it the way it’s supposed to work and it works! Everytime I change the tab or minimize the browser a ping is sended.

The only issue I’m still having with the way the time on site works is that the time the tab is not in focus still counts into the whole time on site as soon as I revisit the page and trigger another ping or event. But I guess there’s no solution for that right now and maybe belongs to another topic.

Thanks again!

Please make a look here: Visits per number of pages mistake

Hi @Florian_Rebmann

For this interesting new feature, I suggest you create a new feature request in Matomo GitHub repo: