r/FlutterDev • • Oct 30 '26

Discussion How do you keep analytics from turning into a god object in your Flutter app?

In most projects I worked on, analytics was treated as a secondary part of the app. We spend a lot of time on the architecture of main parts, and almost no time on analytics.

Then business asks to track this and that. Then to add a new vendor. Then to decide which event goes where. And in the end we have one AnalyticsManager with a huge list of methods, and every layer depends on it.

I came to a simple idea: the app should not know which analytics vendors exist. Feature code only says what happened, and everything else is split into small pieces:

  • Event describes what happened, in the app’s words
  • Factory decides what one vendor receives for an event, or ignores it
  • Tracker makes one call to one vendor SDK
  • Dispatcher passes calls from the app to the vendors

In feature code it looks like this:

class CheckoutController {
  CheckoutController(this._tracker);

  final EventTrackerService _tracker;

  void onTicketPurchased(String fare, double price, String currency) {
    _tracker.track(
      TicketPurchased(fare: fare, price: price, currency: currency),
    );
  }
}

No vendor SDKs, no god object. Each feature keeps its own factories, and DI collects them. Adding a vendor means writing its factories, and removing one means deleting them.

I wrote an article about this idea. The examples there are in Kotlin, but the concept is the same for Flutter:
https://proandroiddev.com/app-architecture-analytics-ec3471ca619b

I also built an open-source library around this idea, Herald, and it has a Flutter package:
https://mkhytarmkhoian.github.io/herald-docs/sdks/flutter/

Curious how you handle analytics in your Flutter projects. One service, a wrapper per vendor, or something else?

1 Upvotes

5 comments sorted by

1

u/Samus7070 Oct 30 '26

I set up a similar model using a pub/sub model. The app features push an event into the main service and it publishes it to all subscribers. Each subscriber formats the event for the collection service it wraps Google Analytics, a homegrown tracking endpoint, shitty service marketing wants, etc. I usually don’t turn the publisher into a router. The subscribers either handle the event or drop it. Multiple subscribers can handle the same event as the data going in is immutable. We had this in place for a while and then there was a requirement to trigger UI flows for things like asking for an app review or prompting for push notification permissions after a certain set of events. We added a local collector that stored events in SQLite. Then another collated the events into meaningful triggers the ui would understand (show push prompt for instance). It worked out great.

1

u/Ill-Recognition-4649 Oct 31 '26

the thing that helped me was drawing the line at what we want to know vs where it goes. feature code fires a plain event name, one mapper per vendor decides if it cares. adding a vendor then means adding one file instead of touching every screen.

3

u/[deleted] Oct 30 '26

[removed] — view removed comment

1

u/mkhytar_mkhoian Oct 30 '26

Your dive deeper in lib docs and I have showcase project you can explore to see how factory handle different payload. https://github.com/MkhytarMkhoian/Moove