0

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  {
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?) {
}

private var numActivitiesStarted = 0

override fun onActivityStarted(activity: Activity?) {

}

override fun onActivityStopped(activity: Activity?) {

}

}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Mohan K
  • 464
  • 5
  • 18

1 Answers1

0

It looks like you are not sending any event properties with APP_CLOSE event.

If you are not intending to you could just turn on session event to get automatic [Amplitude] Start Session / [Amplitude] End Session events in your data without need to maintain your own code for this. Note however these system events cannot carry event properties.

To do this you simply need to call

Amplitude.getInstance().trackSessionEvents(true);
Kimmo Hintikka
  • 13,472
  • 7
  • 34
  • 63
  • i have tried same in ios. but its not working func applicationWillTerminate(_ application: UIApplication) { Amplitude.instance()?.logEvent("APP_CLOSED")} – Rinto Andrews Aug 29 '19 at 13:17