Tracking user activity

Attention.

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

  1. Setting the length of the session timeout
  2. Tracking sessions manually

Setting the length of the session timeout

To change the length of the timeout, pass the value in seconds to the sessionTimeout property of the library configuration YMMYandexMetricaConfiguration.

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

// Creating an extended library configuration.
YMMYandexMetricaConfiguration *configuration = [[YMMYandexMetricaConfiguration alloc] initWithApiKey:@"API_key"];
// Setting the session timeout.
configuration.sessionTimeout = 15;
// Initializing the AppMetrica SDK.
[YMMYandexMetrica activateWithConfiguration:configuration];
Copied to clipboard

Tracking sessions manually

AppMetrica automatically tracks the lifecycle of applications by default. To track sessions manually:
  1. Initialize the library with automatic session tracking disabled sessionsAutoTracking.
    // Creating an extended library configuration.
    YMMYandexMetricaConfiguration *configuration = [[YMMYandexMetricaConfiguration alloc] initWithApiKey:API_key];
    // Disabling automatic tracking user activity.
    configuration.sessionsAutoTracking = NO;
    ...
    // Initializing the AppMetrica SDK.
    [YMMYandexMetrica activateWithConfiguration:configuration];
    Copied to clipboard
  2. Set up session control using the methods +resumeSession: and +pauseSession:.
    [YMMYandexMetrica resumeSession];
    ...
    [YMMYandexMetrica pauseSession];
    Copied to clipboard
When using manual tracking, make sure that the current active session always ends with the +pauseSession: method invoke. If you don't invoke the +pauseSession: method the session will be ended the next time the app is launched.

For correct tracking, the reporters should be manually configured to send events about the start and the pause of the session for each reporter:

id<YMMYandexMetricaReporting> reporter = [YMMYandexMetrica reporterForApiKey:API_key];
[reporter resumeSession];
...
[reporter reportEvent:@"Updates installed" onFailure:^(NSError *error) {
    NSLog(@"REPORT ERROR: %@", [error localizedDescription]);
}];
...
[reporter pauseSession];
Copied to clipboard

If you didn't find the answer you were looking for, you can use the feedback form to submit your question. Please describe the problem in as much detail as possible. Attach a screenshot if possible.