mixpanel-js icon indicating copy to clipboard operation
mixpanel-js copied to clipboard

Support intercepting errors

Open pedromcunha opened this issue 4 years ago • 5 comments

I'm having some issues I want to debug on production and I'm having a hard time intercepting errors from mixpanel and throwing them to my error handling service. To debug I basically have to turn on debug mode for all my users in production to see if there are any errors in the wild and then I attach a global handler to the error function. It would be nice to pass in a callback to trigger whenever mixpanel fires an error.

pedromcunha avatar May 28 '20 19:05 pedromcunha

That's a nice idea. Are you talking about error responses while trying to track to the API, or other JS exceptions that the SDK catches? The former should be basically covered by the existing track() callback param and return value. The latter would require some extra work to hook into here (which is currently stateless and doesn't know about the context it's in or config options): https://github.com/mixpanel/mixpanel-js/blob/bf905cfcda6fa1f8c29edfb857d7e28932a4fd4d/src/utils.js#L373-L376

Not sure whether this would work for you: an alternative that wouldn't require any lib config changes would be for the SDK to fire a CustomEvent when it catches an error, and you could define a handler anywhere you like with window.addEventListener('mixpanel-error', ev => doSomeErrorReporting(ev.detail)).

tdumitrescu avatar May 28 '20 21:05 tdumitrescu

That's a brilliant idea with the CustomEvent. I'm already tracking failures from the callback but for some reason I'm noticing a lot of errors where the payload is undefined (both the event name and the event properties). It's really weird and I want to debug it further. I know some users may have adblock enabled, which is fine, but there are instances where half of the events work and the other half don't make it to mixpanel for whatever reason. I also want to use this as a way to validate the data I'm sending. It could be that whatever I'm sending is being rejected by mixpanel (unsure of what would really cause that to happen) but looking at all available options to have more reliable data.

pedromcunha avatar May 28 '20 21:05 pedromcunha

I hit the same wall trying to debug increased error rates in our app as well. We proxy the mixpanel API through our own API, and noticed that errors coming from our own API do not come through in the error parameter of the track callback. They are definitely caught by the SDK though, as turning on debug mode logs them (as well as all the other debug information).

Although I'd prefer to see the errors intercepted and passed through to the error parameter, I'd gladly take a custom event as well.

dpraul avatar Jun 04 '20 20:06 dpraul