Tracking user activity
This is an archived version of documentation. You can find the current documentation for all platforms here.
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 considers that the session is active and commits regular data exchange 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
Working with an additional API key
By sending data to an additional API key, you can control the access to statistics. Use reporters to transmit events. For more information, see Usage examples.
public class YourActivity extends Activity { ... @Override protected void onResume() { super.onResume(); YandexMetrica.getReporter(getApplicationContext(), API_key).resumeSession(); } @Override protected void onPause() { YandexMetrica.getReporter(getApplicationContext(), 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.