4

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?

Alexandr
  • 3,859
  • 5
  • 32
  • 56

2 Answers2

7

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).

sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • 6
    I disagree that `Analytics` belongs to the `View` layer. `View` is about showing info, not tracking it. But, wow! `CompositeAndroid` looks like a solution for my _"extends hell"_, thanks anyway! :) – Alexandr Aug 23 '16 at 08:17
-1

I think analytics belong to presenter, but as i answered on similar question having analytics in View it's easier to jump on button/labels/... definitions and see where this button is located in UI and have better idea what to send for Category, Actio, Label and Value param of GAnalytics. I think i don't need to mention presenter mustn't have any android specific dependecies so you can't jump to button/labels/... definitions from presenter. Regards

Community
  • 1
  • 1
xyman
  • 399
  • 3
  • 10