Tracking user activity

Concept of a session

A single AppMetrica session is a certain period of the user interacting with your app.

In a standard scenario, a new session begins if the user returns to your app when a significant amount of time has passed since the app switched to background mode (the user hid the app or opened system settings).

Monitoring the app lifecycle

If the app has the minimum Android version set to 4.0 or later, the AppMetrica library can track the app lifecycle automatically. After library initialization, call the YandexMetrica.enableActivityAutoTracking(Application application) method:

public void YourApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Initializing the AppMetrica SDK.
        ...
        // Automatic tracking user activity.
        YandexMetrica.enableActivityAutoTracking(this);
        ...
    }}

If the minimum Android version in the app is earlier than 4.0, use the following methods: YandexMetrica.resumeSessoin(activity) and YandexMetrica.pauseSession(activity) in the corresponding methods for your Activity: onResume() and onPause(). When using these methods you should track that the active session is always terminated by calling the YandexMetrica.pauseSession(activity) method. If this method is not called, the library assumes that the session is active and continues to exchange data with the server part of AppMetrica. This can lead to incorrect tracking of the session duration and energy consumption.

Example:

public class YourActivity extends Activity {
      ...
      @Override
      protected void onResume() {
          super.onResume();
          YandexMetrica.resumeSession(this);
      }

      @Override
      protected void onPause() {
          YandexMetrica.pauseSession(this);
          super.onPause();
      }
      ...
}

For working with an object of the IReporter interface use the resumeSession() and pauseSession() methods:

public class YoutLibraryActivity extends Activity {
    ...
    @Override
    protected void onResume() {
        super.onResume();
        YandexMetrica.getReporter(API_key).resumeSession();
    }

    @Override
    protected void onPause() {
        YandexMetrica.getReporter(API_key).pauseSession();
        super.onPause();
    }
    ...
}

This helps the library accurately monitor:

  • the number of active users.
  • session length.
  • frequency of app use.

Controlling session length

To set the length of time after which a new session starts when a user returns to the app, use the YandexMetricaConfig.withSessionTimeout(seconds) method when activating YandexMetrica.

To work with the IReporter interface, activate the reporter with the extended configuration with the necessary timeout.