Transmitting analytics events and crash telemetry to remote monitoring servers on every single user interaction creates high network overhead and drains mobile batteries. Conversely, sending telemetry when the user closes a tab often fails because standard fetch() calls are abruptly aborted. Telemetry Buffering and navigator.sendBeacon() solve both problems.
Historically, developers attempted to send telemetry during window.onbeforeunload or window.onunload:
Because the tab is being terminated, the browser terminates all active HTTP connections, and the telemetry is lost.
navigator.sendBeacon()navigator.sendBeacon(url, data) sends small amounts of data asynchronously over HTTP POST. The browser guarantees that the data is transmitted to the server, even after the page has finished closing!
visibilitychange event rather than unload or beforeunload. Mobile browsers frequently suspend background tabs without firing unload!fetch() with keepalive: true AlternativeModern Fetch API supports keepalive: true, which keeps the HTTP request alive after the page is discarded:
To avoid spamming analytics endpoints with hundreds of tiny HTTP calls, buffer events in memory and flush them in batches based on buffer size or a periodic timer:
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Integrating Error Tracking (Sentry/Monitoring Concepts) | Proxy & Reflect API Deep Dive |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.