Update analytics data sending method by Shrishti0423 · Pull Request #4002 · javascript-tutorial/en.javascript.info
Repository navigation
Update analytics data sending method - #4002
Open
Shrishti0423 wants to merge 2 commits into javascript-tutorial:master from Shrishti0423:Shrishti0423-patch-1
Open
Update analytics data sending method #4002 Shrishti0423 wants to merge 2 commits into javascript-tutorial:master from Shrishti0423:Shrishti0423-patch-1
Conversation
Shrishti0423 commented Oct 22, 2026
Copy link Copy Markdown
Updated the analytics example to use the visibilitychange event instead of unload.
The analytics data is sent using navigator.sendBeacon() when the document becomes hidden, which is more reliable for sending data when a user leaves or switches away from a page.
Update analytics data sending method
8212df2
Replaced 'unload' event with 'visibilitychange' to send analytics data when the document is hidden.javascript-translate-bot added review needed Review needed, please approve or request changes labels Oct 22, 2026 javascript-translate-bot requested a review from a team October 22, 2026 19:30 AKzar1el reviewed Nov 2, 2026
View reviewed changes
AKzar1el left a comment
Copy link Copy Markdown
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
The new handler can run repeatedly while the page stays open, but the surrounding section still describes a one-time unload event.
Comment thread 2-ui/5-loading/01-onload-ondomcontentloaded/article.mdOutdated
| window.addEventListener("unload", function() { | ||
| navigator.sendBeacon("/analytics", JSON.stringify(analyticsData)); | ||
| document.addEventListener("visibilitychange", function() { | ||
| if (document.visibilityState === "hidden") { |
Fix analytics reporting on visibility change
1a7496a
Updated the explanation of using the visibilitychange event and navigator.sendBeacon for sending analytics data.
CLAassistant commented Nov 3, 2026 · edited Loading
Copy link Copy Markdown
|
|