Sending Web Analytics the better way!!

Most of the big companies have there own analytics rather than using third party services like Google Analytics.

I have seen many of the sites using fetch API or XHRHttpReq for sending analytics event.

What’s the issue in using that fetch AP…


This content originally appeared on DEV Community and was authored by Abhishek Raj

Most of the big companies have there own analytics rather than using third party services like Google Analytics.

I have seen many of the sites using fetch API or XHRHttpReq for sending analytics event.

What's the issue in using that fetch API/XHR for sending analytics event?

  1. Even if its async, we are using our main thread to send the events.
  2. When the send analytics request is queued and user closes the page, your analytics is lost.
  3. Poor UX due to slow HTTP Requests, if we put analytics send req on document unload event

Read More

What should we do?

Almost all browser's (except IE) gives an API for this use case.

Beacon API

The Beacon API is used to send an asynchronous and non-blocking request to a web server. The request does not expect a response. Unlike requests made using XMLHttpRequest or the Fetch API, the browser guarantees to initiate beacon requests before the page is unloaded and to run them to completion. - MDN

Why is Beacon API good for sending analytics event?

  1. It doesn't block your thread.
  2. Browser queues it and takes care to sending the request.
  3. Even after queuing the page is closed, the request will be sent.
  4. It doesn't takes response from server, sends and forgets.
  5. Supported by most of the browsers.

Browser Compatability

Caveats

  1. It only sends POST request.
  2. We can't check if request is received by server.

Connect Me @ Linkedin, Github, Twitter 😃


This content originally appeared on DEV Community and was authored by Abhishek Raj


Print Share Comment Cite Upload Translate Updates
APA

Abhishek Raj | Sciencx (2021-12-16T10:23:17+00:00) Sending Web Analytics the better way!!. Retrieved from https://www.scien.cx/2021/12/16/sending-web-analytics-the-better-way/

MLA
" » Sending Web Analytics the better way!!." Abhishek Raj | Sciencx - Thursday December 16, 2021, https://www.scien.cx/2021/12/16/sending-web-analytics-the-better-way/
HARVARD
Abhishek Raj | Sciencx Thursday December 16, 2021 » Sending Web Analytics the better way!!., viewed ,<https://www.scien.cx/2021/12/16/sending-web-analytics-the-better-way/>
VANCOUVER
Abhishek Raj | Sciencx - » Sending Web Analytics the better way!!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/16/sending-web-analytics-the-better-way/
CHICAGO
" » Sending Web Analytics the better way!!." Abhishek Raj | Sciencx - Accessed . https://www.scien.cx/2021/12/16/sending-web-analytics-the-better-way/
IEEE
" » Sending Web Analytics the better way!!." Abhishek Raj | Sciencx [Online]. Available: https://www.scien.cx/2021/12/16/sending-web-analytics-the-better-way/. [Accessed: ]
rf:citation
» Sending Web Analytics the better way!! | Abhishek Raj | Sciencx | https://www.scien.cx/2021/12/16/sending-web-analytics-the-better-way/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.