Fiverr Tech
Published in

Fiverr Tech

The benefits of sending analytical information with sendBeacon

What changed when we replaced XHR calls with sendBeacon for sending analytical information.

A brief introduction to sendBeacon function.

sendBeacon was created as a way to send [analytical] information back to the server, without expecting a response. This intention alone makes a substantial difference in considerations of behaviour, security, performance and prioritisation, and I’ll elaborate on this later on, but first I want to emphasise the significance of not expecting a response. When a browser expects a response from a web server, it assumes that the application will act upon that information. That’s why vendors have implemented some defence mechanisms in place, to protect the user. The sendBeacon API is not limited by the same constraints.

The Network Tab

Cross-Origin Resource Sharing (CORS)

CORS dictates an elaborate protocol for sending data between domains (or subdomains). This includes a preflight OPTIONS request and omitting the cookie header.

Prioritisation

Understanding that these requests do not affect user experience, the browser can now prioritise accordingly, allowing experience related requests to be executed and completed first.

Behaviour

Since these requests require no response, the browser will not abort them upon page unload, as it would for XHR or fetch requests. The Network tab includes one request with the status (unknown) and the time (unknown). This request was executed before navigation but was not aborted. The browser completed it in the background while the next page request was already being processed.

Amount of cumulative metrics received

Limitations

Since this request function does not accept a response, we have no way for the browser to know whether the request has failed on our servers. This is something you simply have to tolerate when switching to beacons. We will be able to tell if the transmission itself has succeeded by the function’s return value.

Send post requests as a listenter on a navigation link

In conclusion

sendBeacon is made for sending analytical information over HTTP. Using the appropriate interface can improve the user experience on your site and increase the application visibility.

--

--

The technology behind the platform that’s changing the future of work

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store