How is Matomo counting visit_total_time?

I’am digging in the code to get how is piwik calculating the Time on Page.

I think it is not a simple visit_first_action_time minus visit_last_action_time.

I can’t find the proper code for that.
I searched for “ping” and the fieldnames but i do not find anything useful.
Where is the ping treated? The PingRequestProcessor is not very helpful.
Thanks for any hint in advance.

Hallo hamburger,

your question is not so simple to answer. It is necessary to show your matomo settings / tracking code. There are many settings and modifyings to manipulate the result of the time on page.

Default, matomo tracks the time on page accuracy after a second action of the visitor and not on the last page (last page always 0 seconds). All visits with only one action are 0 seconds. A click of a (external) link is an action. The first/start time is the loading of the webpage document. 30 minutes after the last visitor action, the “visit” is ended and a new action of this visitor is tracked as a new visit.

You can manipulate all this with visit_standard_length or enableHeartBeatTimer (default 30 seconds) or own JavaScript rules with beforeunload or setInterval, setTimeout, clearInterval with _paq.push(['ping']);

Here additional tracking code for more accurace time on page tracking:

_paq.push(['enableHeartBeatTimer', 10]); // every 10 seconds
var myheartbeat = setInterval(function() {
  _paq.push(['ping']);
}, 1000); // every 1 second
setTimeout(function() {
  clearInterval(myheartbeat);
}, 11000); // disable it after 11 seconds
window.addEventListener('beforeunload', function(e) {
  _paq.push(['ping']); // send a "ping" (hidden action) on the non-action leaving of the webpage
});

thx melbao
my question was more regarding the php-code.
Where is the php-code witch calculate and update the visit_total_time.
Also where and how is the ping handling done.

OK hamburger, i don’t know it. The term visit_total_time is correct. It is used in the database table log_visit, and logical it is possible to find it in the PHP code.For this, make a grep -r -i 'visit_total_time' /path/to/matomo/* on the command line (Linux, Linux Server). You can do it with a non-used and unzipped matomo installation package in a separate folder. Good luck!

Hi @hamburger,
@melbaois is true: when there is a page view [A] then another action (event, following page view [B], etc) within the visit_standard_length time frame, Matomo set the page [A] visit duration to the delay between page view [A] and last action on page [A] (or page view [B]). On the last page, if there is no other event, page visit duration is 0. Then the visit total time is the sum of all page views durations…

There is a explicit /plugins/CoreHome/Columns/VisitTotalTime.php. Please look at that.