Tracking user activity
In AppMetrica, a session is the period of time a user interacts with the app.
Background session
A background session starts with the AppMetrica SDK initialization.
If, during a background session, AppMetrica receives the onResume()
system event for Android or the UIApplicationDidBecomeActiveNotification
system event for iOS, a user session begins.
Any events sent before these system events refer to background sessions.
A background session can refer to:
- A period of time between clicking an app icon and displaying the app's interface.
- Start of services that accept push notifications.
User session
A user session starts when AppMetrica receives the following system events:
- For Android:
onResume()
. - For iOS:
UIApplicationDidBecomeActiveNotification
.
A user session ends when the session timeout expires. The session timeout is counted from the moment the user minimizes the app.
Any events sent after the system events mentioned above refer to user sessions.
By default, AppMetrica considers a session new if a user returns to the app after a significant period of time after the app switched to background mode (the user minimized the app or opened system settings).
Setting the session timeout
The session timeout is the period of time that must pass after a user minimizes the app and before they open it again. You can set the session timeout in the SDK.
- If the user opens the app again before the timeout expires, the current session continues.
- If the user opens the app again after the timeout expires, a new session starts.
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:
-
Initialize the library with automatic session tracking
sessionsAutoTracking
disabled.SwiftObjective-C// 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];
-
Set up session control using the
resumeSession
andpauseSession
methods.SwiftObjective-CAppMetrica.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];
Sessions for App Extensions
By default, the AppMetrica SDK doesn't track sessions for App Extensions and all activity is tracked in background sessions. If required, use the methods provided in the Tracking sessions manually section.
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.
Session timeout that you set in the SDK.