Swetrix / swetrix-js

Ultimate, privacy-focused and feature-rich alternative to Google Analytics; Swetrix JavaScript tracking client.
https://swetrix.com
MIT License
172 stars 8 forks source link

A way to listen to event being sent #14

Closed iipanda closed 2 months ago

iipanda commented 3 months ago

I'm trying to track link clicks: Swetrix.track({ ev: 'cta_link_click' }); The issue with this is the navigation happens before the event finishes sending and thus the request fails with NS_BINDING_ABORTED status without being sent. The solution would be returning a promise from the Swetrix.track function or accepting a callback to know when the event has finished sending and proceed with the navigation then.

I can implement this functionality I just wanted to make sure this contribution would be welcome.

iipanda commented 3 months ago

I'm also noticing you are using XMLHttpRequest to send requests instead of the fetch API. Is there any particular reason? If there is no need to keep this I would replace it with fetch since XHR would require me to wrestle with callbacks instead of simply returning a promise.

Blaumaus commented 3 months ago

Hi @iipanda, there's no reason to use XMLHttpRequest, feel free to rewrite it to Fetch and return a promise in the track method