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
Use navigator.sendBeacon by default and minimise the tracker pause from 500ms to 100ms #6641
Comments
This is nice. Also Firefox implemented it etc 👍 for giving it a try |
Never heard of it until now! This looks very useful 👍 |
Tried implementing this into the JS tracker and my browser does fire the events correctly. However it seems the server side is not logging the requests because nothing shows up in the visitor logs. sendBeacon is POST only and if sending the request as a string it will be transmitted as "text/plain;charset=UTF-8". I saw that piwik POST requests are transmitted as "application/x-www-form-urlencoded; charset=UTF-8". It is possible to use FormData with sendBeacon but it will be transmitted as "multipart/form-data", I did not try this approach. Is there a check or limitation on which content-type the server expects for POST tracking? |
Hi there, you can enable tracker debug see: |
Hi, turned on debug tracking and this is what a sendBeacon post request looks like from Chrome (copied as cURL to show results) curl 'http://piwik.elysian.se/piwik.php' -H 'Cookie: OVHCDN=R2163973028' -H 'Origin: http://soniccharge.com' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2239.2 Safari/537.36' -H 'Content-Type: text/plain;charset=UTF-8' -H 'Accept: /' -H 'Cache-Control: max-age=0' -H 'Referer: http://soniccharge.com/download' -H 'Connection: keep-alive' --data-binary 'action_name=Sonic%20Charge%20-%20download&idsite=2&rec=1&r=289293&h=12&m=53&s=13&url=http%3A%2F%2Fsoniccharge.com%2Fdownload&_id=75819894e2914cd2&_idts=1417780393&_idvc=1&_idn=1&_refts=0&_viewts=1417780393&send_image=0&pdf=1&qt=0&realp=0&wma=0&dir=0&fla=1&java=1&gears=0&ag=0&cookie=1&res=2880x1800>_ms=585' --compressed Response.
Looks like the tracker side does not read the posted data only the cookie in the header. |
Implemented sendBeacon as primary tracking method with XmlHttpRequest and getImage as fallbacks. matomo-org#6641
Using sendBeacon with FormData was recognized by the tracker. Now unfortunately it generates a lot of unnecessary data as FormData is transmitted as "multipart/form-data". It also required a small URI to FormData converter but it is still code that we can get rid of if the server side tracker is taught to parse "Content-Type: text/plain;charset=UTF-8". At least the implementation is now working. =) Form data example
as compared to text/plain version
|
Started looking into adding the text/plain support on the server side implementing php://input just to realize that is exactly what bulk requests already does. Why re-invent the wheel... I'll make a new sendBeacon implementation that uses the bulk-request format instead. |
Just FYI: The bulk tracker does currently not check whether there are scheduled tasks to run etc. We might want to enable this at some point in case we merge this (at the latest once |
Ah I see. As long as I have visitors without Firefox or Chrome, normal tracking requests till be made as well. I guess this is good enough for now? I have implemented this for all sites I track, mainly because the sendBeacon method (if available) queues the request and returns instantly which avoids round-trip delays on downloads and outlinks. I do have one concern regarding the server side implementation. When using sendBeacon you do not know exactly when the request is made, it is up to the browser to queue and dispatch these at its earliest convenience. Technically I suspect that multiple requests can reach the server at the same time. I noticed there was a delay in the code to avoid this because multiple visits could be created by mistake. If it is still needed, I am guessing there should be a better solution implemented for this as an arbitrary delay is far from reliable. This should be handled on the server side imho (if it is not done already). |
Another thing that comes to my mind: When using Content Tracking we might use the |
Good news: We should 100% give this a try and if the feature available in the browser, use it (if tests are good). This would minimize failed requests that don't make it within 500ms while at the same time not delaying the next page load by 500ms. More information: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon |
We could move it into 3.4 or at least 4.0? Basically, the idea be to set a variable in the We could likely send also GET requests using sendBeacon as long as the URL parameters are fully in the URL so it can be replayed by log analytics if needed. |
A first implementation is done in #12538 However, we should also fully support this and allow users to opt in for always using
When enabled, users also need to make sure POST requests are handled correctly by the webserver. This should not be a problem. Drawback: sendBeacon has no callback to check whether the request was successful or not. This means the Universal GA supports sendBeacon since 2014 and users can opt in to it: https://www.thyngster.com/google-analytics-added-sendbeacon-functionality-universal-analytics-javascript-api/ |
Currently Piwik adds a delay on clicks in order to ensure the links are tracked properly. As Chrome has now shipped navigator.sendBeacon can we get it to use this instead of the delay if the browser supports it?
The text was updated successfully, but these errors were encountered: