PostHog / posthog-js

Send usage data from your web app or site to PostHog, with autocapture.
https://posthog.com/docs/libraries/js
Other
303 stars 126 forks source link

Add onError handler for posthog init #482

Open MarkLyck opened 1 year ago

MarkLyck commented 1 year ago

One major problem with Posthog is that ad blockers are blocking it. Which I'm fine with for analytics. But because I also use posthog for feature flags that is core functionality, that a user would likely want to work.

However right now, if you have an adblocker like the default one in Brave Browser, posthog init will just fail and keep retrying. But there's no way to catch the error and prompt the user that they might want to disable their adblocker.

Please add an onError handler to catch the error when it fails to init for whatever reason.

EugeneBalabai commented 1 year ago

Any news? I want to set a default value for my feature flags if an error occurs but I didn't find any solution

mtunyk commented 1 year ago

Sending events to PostHog from your own domain should be a workaround of the issue with an ad blocker interceptor. But we definitely need an onError handler.

neilkakkar commented 1 year ago

@EugeneBalabai for your use-case, you can check if posthog.getFeatureFlag returns undefined instead of a value, which implies an error and you can use that to set the default

EugeneBalabai commented 1 year ago

@neilkakkar yep, but I also want to show the loader to a customer until I get the values from Posthog to prevent flickering

vonBrax commented 2 months ago

I'm facing the same or a similar problem here.

For me the problem is with the surveys module which uses a setInterval with no break conditions. Every second it tries to load the surveys and check if there is an active matching survey. If an ad blocker is enabled, the call to the surveys endpoint will fail and then retry indefinitely every second. The error logs completely take over the browser console and the network tab, which is really annoying.

I had to add a workaround where I manually check if posthog server is reachable before initializing posthog client.

It would be very nice if posthog could expose an api to do some health checks before the initialisation method is called or perfom the checks on the init method and bail out in case the server can't be reached.