New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better handling of POST tracking requests when page is unloaded (using sendBeacon) #12538
Conversation
@mattab I tested it again and also checked in the logs and it worked. There is one thing that is a bit annoying but it always has been like this already: I noticed sometimes 2 POST requests. Eg a user submits a form, a POST request is sent, a few ms later the "unload" event occurs and the browser cancels these requests, then we retry them and send the second request. In the callback where we usually fallback to get request, we cannot know if the initial POST was already successfully transferred to the server or not (eg maybe request completed but response wasn't sent yet) and in the worst case this could cause the same data to be tracked twice. This affects all POST requests that are made shortly before unload. I wonder if we should always execute such requests with like a 50ms timeout for example? In my tests this prevent this problem like this:
|
FYI: I believe this timeout I just explained is the way to go and helpful... therefore pushed the change. |
js/piwik.js
Outdated
|
||
try { | ||
var blob = new Blob([request], headers); | ||
success = navigator.sendBeacon(configTrackerUrl, blob); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When sending the beacon, currently we don't get the data in the GET, but as POST. since it's useful to get GET requests so we can replay the logs, wondering if we should maybe do the same as in getImage
and write eg. navigator.sendBeacon( configTrackerUrl + (configTrackerUrl.indexOf('?') < 0 ? '?' : '&') + request, blob)
and send the data as GET params instead of POST?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not possible to send a GET request but mentioned in #6641 we need to check if log importer can process POST request when URL parameters are used
Great find with the new setTimeout and 50 ms which will help track more accurate data 👍 |
This reverts commit 51addf1
# Conflicts: # js/piwik.min.js # piwik.js
…g sendBeacon) (matomo-org#12538) * use sendBeacon if available * better sendBeacon implementation * execute post request with a little delay * make sure to use alias so variable will be minimized * trigger build * Merged 3.x-dev * Revert "Merged 3.x-dev" This reverts commit 51addf1 * Minified files up to date
refs #6641
Anyone keen on doing some more testing with this?
Basically, what I experienced is, that we eg send a POST request on a form submit, then an unload happens and all pending requests are directly cancelled without hitting the Matomo tracking server, then we fall back to GET and those GET requests might fail because the URL is too long etc.
What would now happen here is when such a POST request is cancelled, and the unload was triggered, we would send them again as POST request which makes it more likely to succeed vs GET but this time using sendBeacon.
Also if any tracker plugin sends any tracking requests via
unload
plugin event, those tracking requests would be directly sent viasentBeacon
as well vs before they likely did not finish on time before the next page was loaded.