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

Add onError handler for posthog init

Open MarkLyck opened this issue 3 years ago • 5 comments

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.

MarkLyck avatar Dec 06 '22 14:12 MarkLyck

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

EugeneBalabai avatar Mar 29 '23 17:03 EugeneBalabai

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.

mtunyk avatar Mar 29 '23 19:03 mtunyk

@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

neilkakkar avatar Apr 04 '23 13:04 neilkakkar

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

EugeneBalabai avatar Apr 04 '23 13:04 EugeneBalabai

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.

vonBrax avatar Sep 02 '24 10:09 vonBrax