web-api-beacon
Reactive icon

Web API Beacon

Stable version 0.3.0 (Compatible with OutSystems 11)
Uploaded
 on 20 January 2022
 by 
0.0
 (0 ratings)
web-api-beacon

Web API Beacon

Documentation
0.3.0

Description


This method is intended for analytics and diagnostics code to send data to a server.

A problem with sending analytics is that a site often wants to send analytics when the user has finished with a page: for example, when the user navigates to another page. In this situation the browser may be about to unload the page, and in that case the browser may choose not to send asynchronous XMLHttpRequest requests.

In the past, web pages have tried to delay page unload long enough to send data. To do this they have used workarounds such as:

  • Submitting the data with a blocking synchronous XMLHttpRequest call.
  • Creating an <img> element and setting its src. Most user agents will delay the unload to load the image.
  • Creating a no-op loop for several seconds.

All these methods block unloading the document, which slows down navigation to the next page. There's nothing the next page can do to avoid this, so the new page seems slow, even though it's the fault of the previous page.

With the SendBeaconJSON method, the data is transmitted asynchronously when the user agent has an opportunity to do so, without delaying unload or the next navigation. This means:

  • The data is sent reliably
  • It's sent asynchronously
  • It doesn't impact the loading of the next page



Methods


SendBeaconJSON - The navigator.sendBeacon() method asynchronously sends a small amount of data over HTTP to a web server using POST. It’s intended to be used for sending analytics data to a web server, and avoids some of the problems with legacy techniques for sending analytics, such as the use of XMLHttpRequest. This JSON variant includes the 'application/json' header so you can ping OutSystems Platform REST entry points.

See unit tests for implementation details.