mixpanel / sample-android-mixpanel-integration

59 stars 35 forks source link

the best practice if I have multiple activities and fragments #18

Open starer93 opened 4 years ago

starer93 commented 4 years ago

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)

any advice is appreciated.

RabbiNash commented 3 years ago

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
            }
        }

    }

}`