Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MVP and Analytics

How to apply Firebase Analytics(for example) on MVP app architecture? (I use Mosby to build MVP)

I want to track events of "opening screen", "do click action".

There is how I send "opening screen" event.

private const val ANALYTICS_SCREEN_NAME = "ask_password"
private const val ANALYTICS_ACTION_DONE = "done"
class AskPasswordPresenter : MyDiaryPresenter<AskPasswordView> {

    @Inject
    constructor(analytics: AnalyticsManager) : super(analytics) // AnalyticsManager is wrapper around Firebase Analytics API

    override fun initialize() { // this method called when new ViewState created
        super.initialize()
        analytics.doScreenOpened(ANALYTICS_SCREEN_NAME)
    }

    fun done(password: String) { // called when user click on 'Done' button
        ...
        analytics.doAction(ANALYTICS_SCREEN_NAME, ANALYTICS_ACTION_DONE)
    }
}
  1. doAction(...) called as it must. Okay.

  2. initialize() called even when user navigates back to the screen from backstack. I want it to send event ONLY when user navigates to screen in "front direction". It also looks like a bad solution as initialize() method introduced for initializing Presenter when ViewState was created at the first time, not for logging analytics events.

It sounds like I must share Fragment's lifecycle to Presenter. Not good.

What can you recommend? Must I create another entity, like AnalyticsPresenter for each Fragment? How do you handle this case?

like image 555
Alexandr Avatar asked Oct 23 '25 12:10

Alexandr


1 Answers

In my opinion Analytics belongs to the View layer and not Presenter layer. So track it either directly in Fragment / Activity or (what I usually do) use one of the libraries like lightcycle or CompositeAndroid to kind of plug in a "Analytics component" to your Activity / Fragment. By doing so your Fragment / Activity doesn't contain the code for Analytics but is rather decoupled into its own class (single responsibility).

like image 168
sockeqwe Avatar answered Oct 26 '25 02:10

sockeqwe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!