Tracking user activity

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).

Setting the length of the session timeout

By default, the session timeout is 10 seconds. The minimum acceptable value for the sessionTimeout parameter is 10 seconds.

To change the length of the timeout, pass the value in seconds to the withSessionTimeout(int sessionTimeout) method when creating the extended library configuration.

// Creating an extended library configuration.
YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder(API_key)
        // Setting the length of the session timeout.
        .withSessionTimeout(15)
        .build();
// Initializing the AppMetrica SDK.
YandexMetrica.activate(getApplicationContext(), config);
Copied to clipboard

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);
        ...
    }
}
Copied to clipboard

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();
      }
      ...
}
Copied to clipboard

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();
    }
    ...
}
Copied to clipboard

This helps the library accurately monitor:

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