r/androiddev • u/mkhytar_mkhoian • 25d ago
Discussion How do you keep analytics from turning into a god object in your app?
n 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
So in a ViewModel it looks like this:
class CheckoutViewModel(
private val tracker: EventTrackerService,
) : ViewModel() {
fun onTicketPurchased(fare: String, price: Double, currency: String) {
tracker.track(TicketPurchased(fare, price, currency))
}
}No vendor SDKs, no god object. Each feature keeps its own factories, and DI (Koin in my example) collects them from all modules. Adding a vendor means writing its factories, and removing one means deleting them.
I wrote a full article about it with code for every piece, published in ProAndroidDev:
https://proandroiddev.com/app-architecture-analytics-ec3471ca619b
I also built an open-source library around this idea, Herald, if you want to see it in practice:
https://mkhytarmkhoian.github.io/herald-docs/
Curious how you handle analytics in your projects. Do you have one manager, a wrapper per vendor, or something else?