levibostian / AndroidBlanky

Create a new Android app fast. Comes pre-installed with libraries you already use.
7 stars 0 forks source link

Avoid false positives when using app analytics #45

Closed levibostian closed 5 years ago

levibostian commented 5 years ago

Problem: Analytics tracking is located all over a code base. Actions are performed all over the place between activities, fragments, services, UI, backend, you name it. The user performs actions that we care about.

However, I have hit a problem recently with an app of mine. I have a function in app analytics for "track screen" where I am logging when a user goes into a certain screen to measure how often a screen is used. Sometimes I have analytics.trackScreen() called twice even though it should only be called once. I cannot think of an example at this moment, unfortunately.

This is my thought (without being able to provide an example). At first I was going to suggest that I add more testing to the app. Test, "make sure that the app analytics mock is only interacted with this much and no more". I even had a utility function created to do just that:

import com.nhaarman.mockito_kotlin.MockitoKotlin
import com.nhaarman.mockito_kotlin.verifyNoMoreInteractions
import com.themedalz.scorekeeper.service.analytics.AppAnalytics

object MockitoTestUtil {

    fun <T> verifyOnly(verifying: () -> T) {
        val mock = verifying.invoke()

        verifyNoMoreInteractions(mock)
    }

}

However, once I thought about it, I thought that there is a better way.

Maybe I am using analytics wrong? There should not be any false positives if analytics is only being logged in the onCreate() of UI objects, for example.

Am I logging too many types of activities in analytics? First start with what am I trying to measure. Then, I would imagine measuring that is much easier then what I currently have outlined.

levibostian commented 5 years ago

Through working through this problem with another app of mine, I believe the issue is not so much my code for analytics, it's my use of analytics.

What do you need to track? I believe that I could avoid this issue if I define better what metrics need to be tracked (which is usually user generated events) and when the user performs that action, perform that tracking.

By only tracking events when an event is triggered by a user instead of my code triggering the event, this issue could be avoided.