amplitude / Amplitude-Android

Native Android SDK for Amplitude
MIT License
161 stars 90 forks source link

App Close Event not getting tracked when app is getting Closed #197

Open nk-mohan opened 5 years ago

nk-mohan commented 5 years ago

I want to track App Open and App Closed events, Below code i have used. I'm getting App Open Event but i couldn't get App Close Event, is there anyone know how track app close event

class AppLifecycleTracker : Application.ActivityLifecycleCallbacks { private var numActivitiesStarted = 0

override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
    if (numActivitiesStarted == 0) {
        // app launched
        Amplitude.getInstance().logEvent("APP_OPEN")
    }
    numActivitiesStarted++
}

override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
}

override fun onActivityDestroyed(activity: Activity?) {
    numActivitiesStarted--
    if (numActivitiesStarted == 0) {
        // app killed
        Amplitude.getInstance().logEvent("APP_CLOSE")
    }
}

override fun onActivityResumed(activity: Activity?) {
}

override fun onActivityPaused(activity: Activity?) {
}

override fun onActivityStarted(activity: Activity?) {

}

override fun onActivityStopped(activity: Activity?) {

}

}

raghunandankavi2010 commented 3 years ago

Any update on this. Even in our app end session events are not getting tracked. Also if you reopen the app again there is no start session event logged. Below is the code

    sharedPref = getSharedPreferences(PREF_EMPLOYEE, PRIVATE_MODE)
    val empId =  sharedPref.getString("uuid","") //editor.putString("uuid", employeData.uuid)
    val client: AmplitudeClient = Amplitude.getInstance()
        .initialize(applicationContext, "APIKEY")
        .enableForegroundTracking(application)
        .trackSessionEvents(true)
        .setLogLevel(Log.VERBOSE)

    if(!empId.isNullOrBlank()) {
        client.userId = empId
    }
qingzhuozhen commented 3 years ago

@raghunandankavi2010 How do you implement the app open and close events? For session events, if they are within a short time, we won't create new session events.

raghunandankavi2010 commented 3 years ago

Got it. I was trying to open and close within 10 seconds. Also I see that when I open the app again the previous end session gets logged.

qingzhuozhen commented 3 years ago

@raghunandankavi2010 Great to know. That's expected behavior.