Skip to content

Update analytics data sending method - #4002

Open
Shrishti0423 wants to merge 2 commits into
javascript-tutorial:masterfrom
Shrishti0423:Shrishti0423-patch-1
Open

Shrishti0423 wants to merge 2 commits into
javascript-tutorial:masterfrom
Shrishti0423:Shrishti0423-patch-1

Conversation

@Shrishti0423

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.

Replaced 'unload' event with 'visibilitychange' to send analytics data when the document is hidden.
@javascript-translate-bot javascript-translate-bot added review needed Review needed, please approve or request changes labels Oct 22, 2026
@javascript-translate-bot
javascript-translate-bot requested a review from a team October 22, 2026 19:30

@AKzar1el 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.

window.addEventListener("unload", function() {
navigator.sendBeacon("/analytics", JSON.stringify(analyticsData));
document.addEventListener("visibilitychange", function() {
if (document.visibilityState === "hidden") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visibilitychange also fires when someone switches tabs or minimizes the browser, so this can send the same accumulated analyticsData multiple times without an unload. Could the example either clear the sent batch after a successfully queued beacon or guard one-time reporting, and update the surrounding window.onunload explanation? Otherwise readers copying it may double-count activity.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I've updated the example to use a one-time reporting guard based on the return value of navigator.sendBeacon(). This prevents the same analytics data from being queued multiple times when the page becomes hidden repeatedly. I've also updated the surrounding explanation to describe the visibilitychange event and when it fires.

Updated the explanation of using the visibilitychange event and navigator.sendBeacon for sending analytics data.
@CLAassistant

CLAassistant commented Nov 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review needed Review needed, please approve or request changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants