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 session timeout

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

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

// Creating an extended library configuration.
let configuration = AppMetricaConfiguration(apiKey: "API key")
// Setting the session timeout.
configuration?.sessionTimeout = 15
// Initializing the AppMetrica SDK.
AppMetrica.activate(with: configuration!)

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

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

// Creating an extended library configuration.
AMAAppMetricaConfiguration *configuration = [[AMAAppMetricaConfiguration alloc] initWithAPIKey:@"API key"];
// Setting the session timeout.
configuration.sessionTimeout = 15;
// Initializing the AppMetrica SDK.
[AMAAppMetrica activateWithConfiguration:configuration];

Tracking sessions manually

By default, AppMetrica automatically tracks the lifecycle of apps. To track sessions manually:

  1. Initialize the library with automatic session tracking sessionsAutoTracking disabled.

    // Creating an extended library configuration.
    if let configuration = AppMetricaConfiguration(apiKey: "API key") {
       // Disabling the automatic tracking of user activity.
       configuration.sessionsAutoTracking = false
       // ...
       // Initializing the AppMetrica SDK.
       AppMetrica.activate(with: configuration)
    }
    
    // Creating an extended library configuration.
    AMAAppMetricaConfiguration *configuration = [[AMAAppMetricaConfiguration alloc] initWithAPIKey:@"API key"];
    // Disabling the automatic tracking of user activity.
    configuration.sessionsAutoTracking = NO;
    // ...
    // Initializing the AppMetrica SDK.
    [AMAAppMetrica activateWithConfiguration:configuration];
    
  2. Set up session control using the resumeSession and pauseSession methods.

    AppMetrica.resumeSession()
    // ...
    AppMetrica.pauseSession()
    
    [AMAAppMetrica resumeSession];
    // ...
    [AMAAppMetrica pauseSession];
    

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.

Tracking sessions for reporters

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

guard let reporter = AppMetrica.reporter(for: "API key") else {
    print("AppMetrica reporter initialization failed.")
    return // or return someDefaultValue or throw someError
}
reporter.resumeSession()
// ...
reporter.reportEvent(name: "Updates installed", onFailure: { (error) in
    print("REPORT ERROR: \(error.localizedDescription)")
})
// ...
reporter.pauseSession()
id<AMAAppMetricaReporting> reporter = [AMAAppMetrica reporterForAPIKey:@"API key"];
[reporter resumeSession];
// ...
[reporter reportEvent:@"Updates installed" onFailure:^(NSError *error) {
    NSLog(@"REPORT ERROR: %@", [error localizedDescription]);
}];
// ...
[reporter pauseSession];

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.

Contact support