mixpanel / sample-android-mixpanel-integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

the best practice if I have multiple activities and fragments

starer93 opened this issue · comments

I have a project with multiple activities and fragments. So what is the best way to use Mixpanel? ( I would like to track the whole application)

  • Should I make MixpanelApi singleton?
  • Should I create instances everywhere?

any advice is appreciated.

Incase anyone needs this. The best advice I would give is that make MIxpanelApi singleton and then access the singleton in all the other classes.

`class MixPanelUtils private constructor(context : Context) {
private val mixPanel: MixpanelAPI = MixpanelAPI.getInstance(context, BuildConfig.MIXPANEL_TOKEN)

companion object {
    @Volatile
    private var instance: MixPanelUtils? = null

    fun getInstance(context: Context) = instance ?: synchronized(this) {
            instance ?: MixPanelUtils(context).also {
                instance = it
            }
        }

    }

}`