How to handle and gather webpage data after user leaves a site in Javascript?

I am adding some interactive features to my webpage with Javascript but I want to collect the data about how each page is accessed and used by site visitors (for analytical reasons) How to handle that task by using and applying those events. Please help me with a real-life example.

»Javascript @devuntildeath

» 1 answer  » 178 views   1 like

Add photos

Gadrawin • 1 year ago
Use unload event it will get all your jobs done for you. Briefly call unload event which triggers when user leaves the page. Lets take a real-life example, You built site like Quora and you want to collect data about scrolls, page views, user session (time spent) and more data (window.onunload) will help.

let analyticsData = {


window.addEventListener("unload", function() {
  navigator.sendBeacon("/analytics", JSON.stringify(analyticsData));

0  Dislike  •  Reply

Back to top

[SHARE THIS POST]: The post's archived Spread it to others!

Posted under this donnektish »
This community is about sharing contents such as questions and answers discussing about Javascript and TypeScript, ES5, ES6
90 people joined this donnektish

Maintain social distancing, to avoid Covid19