Canonical Way To Disable Analytics In Flutter Debug Build

- 1 answer

I have a flutter app using firebase and google analytics. My data about active users is wrong as I am testing a lot in emulators and google analytics thinks that the installs there are real users. As I often reinstall the app for testing it somehow seems to count it as a new user every time.

As a solution, I'd like to disable google analytics when building the app in debug mode and only activate it when I build it with --release.

Does anyone know how do achieve this with a flutter app?



I assume you're using Firebase Analytics in Flutter (I don't think there's GA for Flutter). You can use kReleaseMode flag when setting navigatorObservers of MaterialApp and creating FirebaseAnalyticsObserver instance. Here's how I do it in my app (notice that I also use preferences which allow user to turn on/off analytics, as well as desktop platforms where there's no Firebase support which is why I specifically check for Web, iOS and Android platforms):

    navigatorObservers: preferences.isAnalyticsEnabled &&
        kReleaseMode &&
        (kIsWeb || Platform.isAndroid || Platform.isIOS)
        ? [
            FirebaseAnalyticsObserver(analytics: FirebaseAnalytics()),
        : [],