matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.63k stars 2.62k forks source link

PagePerformance Plugin: update already tracked visit to set time_on_load and introduce time_fully_loaded #20726

Open rr-it opened 1 year ago

rr-it commented 1 year ago

Summary

The PagePerformance Plugin can already track time_on_load via JavaScript PerformanceTiming.loadEventStart/PerformanceTiming.loadEventEnd.

In case trackPageView is fired on event DOMContentLoaded and thereby before the event onload: time_on_load is always 0.

Can we implement a solution to still track the pageview on event DOMContentLoaded and update time_on_load later on?

Generally tracking the pageview after the event onload might not work, as we are losing tracked visits which did not wait for onload to finish.

This feature would open further performance tracking options. E.g. track a new metric time_fully_loaded - after all ressources on page are fully loaded:

var performanceEntries = window.performance.getEntries();
var timeFullyLoaded = 0
for(var i=0; i<performanceEntries.length;i++){
  var responseEnd = performanceEntries[i].responseEnd ? performanceEntries[i].responseEnd : 0;
  timeFullyLoaded = Math.max(responseEnd,timeFullyLoaded);
}
console.log(timeFullyLoaded);

Update the data once before page is closed: See mdn web docs: Navigator: sendBeacon() method - Sending analytics at the end of a session

Websites often want to send analytics or diagnostics to the server when the user has finished with the page. The most reliable way to do this is to send the data on the visibilitychange event:

document.addEventListener("visibilitychange", function logData() {
  if (document.visibilityState === "hidden") {
    navigator.sendBeacon("/log", analyticsData);
  }
});
michalkleiner commented 1 year ago

Hi @rr-it, thank you for the creating the issue and explaining your idea for improvement, it definitely sounds interesting. As I'm not personally sure how this could be implemented I'm assigning the issue to the product team to have a look and prioritise it.

rr-it commented 2 months ago

Related to https://github.com/matomo-org/matomo/issues/22339