Skip to content

Conversation

voidus
Copy link

@voidus voidus commented Mar 16, 2025

Closes #56.

For a more radical approach, see #58

Copy link
Owner

@jahilldev jahilldev left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution, @voidus 👍

As discussed in the linked issue, if you have time, could we add a fallback method using fetch with keepalive: true so that we're still recording metrics.

Something simple like this should do:

const queryParams = getQueryParams(trackingId, { type, event, debug });
const endpoint = window.minimalAnalytics?.analyticsEndpoint || analyticsEndpoint;

if(navigator.sendBeacon) {
  navigator.sendBeacon(`${endpoint}?${queryParams}`);
} else {
  fetch(`${endpoint}?${queryParams}`, {
    method: 'POST',
    keepalive: true
  });
}

bindEvents(trackingId);
trackCalled = true;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check if navigator.sendBeacon is defined before calling it
2 participants