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.92k stars 2.66k forks source link

[Bug] `setLinkTrackingTimer` does not influence delay if sendBeacon is used #22770

Open LeoniePhiline opened 1 day ago

LeoniePhiline commented 1 day ago

What happened?

Matomo caused our Interaction Next Paint (INP) timing statistic to be longer than the 200ms deemed acceptable by Google, which resulted in downranking in Google search.

Trying to counter #21297, we set setLinkTrackingTimer to 0. It is described as:

When a user clicks to download a file, or clicks on an outbound link, Matomo records it. In order to do so, it adds a small delay before the user is redirected to the requested file or link. The default value is 500ms, but you can set it to a shorter length of time. It should be noted, however, that doing so results in the risk that this period of time is not long enough for the data to be recorded in Matomo.

Source: https://developer.matomo.org/guides/tracking-javascript-guide#changing-the-pause-timer

However, the delay when the browser’s “send beacon” is used is hard coded to 100ms and not lowered by setting the link tracking timer.

What should happen?

The 100ms hard coded delay should be reduced to the value of configTrackerPause, if the latter is set shorter. I.e. use delay for legacy tracking requests and Math.min(delay, 100) when using send beacon.

Alternatively, the 100ms delay should be removed entirely, since “send beacon” exists precisely for reliably sending beacons at unload, without requiring any artificial delay.

How can this be reproduced?

  1. Configure:
window._paq.push(['setLinkTrackingTimer', 0]);
window._paq.push(['enableLinkTracking']);
  1. Find that the 100ms delay still applies. This means that the configuration is not effective. This is the bug.

  2. Optionally inspect the JS source code and find that the delay argument to the send request and send batch request functions is not used when use of send beacon is enabled.

Matomo version

5.1.2

PHP version

8.3

Server operating system

Debian 12

What browsers are you seeing the problem on?

Firefox, Chrome, Safari, Microsoft Edge

Computer operating system

All

Relevant log output

No response

Validations

LeoniePhiline commented 1 day ago

I have been playing with Chrome's dev tools script content override feature to mimic a patched matomo.js.

Changing the existing minified cv(100), which corresponds to the unminified setExpireDateTime(100), to cv(0), then sending the beacon is not entirely reliable.

With cv(0), sometimes profiles only show the page load, but not the beacon being sent:

image

However, setting cv(10) (= setExpireDateTime(10)) was enough to make sure the beacon is sent reliably:

image

Thus, while some delay seems required for a reliable beacon, 100ms might be more than Matomo users are willing to pay for it.

Therefore, I believe that applying Math.min(configTrackerPause, 100) to setExpireDateTime (minified: cv(Math.min(bW,100))) in the context of sendBeacon-enabled browsers is the right way to go, as it allows users to configure for shorter than 100ms delays.