In Amplitude, sessions are a useful metric for understanding the frequency and duration of your users' engagement with your product. The most direct way to build a session-based analysis is with the User Sessions chart; however, you can incorporate sessions into other types of analyses through user and event properties.
This article will explain how Amplitude defines and tracks user sessions. By the time you're finished, you should have a solid understanding of how best to incorporate sessions into your Amplitude analyses.
NOTE: You may also find this video on User Sessions helpful.
How Amplitude defines "sessions"
Generally, a session is the period of time a user has your app in the foreground or has your website open. The specifics differ slightly between mobile and web applications:
- For mobile, a session begins when the app is brought into the foreground; it ends when the app goes into the background and no events are fired for at least five minutes. All events sent within five minutes of each other are counted towards the current session. Note that you can define your own session expiration time by calling setMinTimeBetweenSessionsMillis(timeout) , where the timeout input is in milliseconds.
- On a browser, a session begins when the website is opened and the SDK is initialized; it ends when the last event is triggered. Web sessions time out after 30 minutes by default. All events fired within 30 minutes of each other are counted as part of the same session. This timeout window can be customized via the JavaScript SDK configuration options.
Amplitude automatically generates a session ID for each new session; that ID is the session's start time in milliseconds since epoch (also known as the Unix timestamp). All events within the same session share the same session ID. If you are using Amplitude's SDKs, this happens automatically. However, if you are sending data to Amplitude using the HTTP API, you will have to explicitly set the session ID field in order to track sessions.
How Amplitude tracks your sessions
By default, the setting in Amplitude for the session property is session ID
. All events with the same session ID and the same user ID will be grouped into the same session. The session ID does not have to be unique across multiple users. You can also change the property you use to group sessions.
As noted above, session IDs for events sent via the Amplitude SDKs are automatically generated and managed. However, for events sent via the HTTP API, Amplitude defaults to a session ID of -1
. This means the event is excluded from all session metrics.
NOTE: This commonly occurs when sending data to Amplitude from Segment via a cloud-mode connection. As with sending data via HTTP API, you will have to explicitly set a session ID to track sessions.
Events included in the same session will be connected with a blue line, as shown above.
Start Session and End Session events
Amplitude does not track Start Session
and End Session
events by default, but it can still determine the beginning and ending times of sessions by tracking session IDs. Amplitude also uses session ID to calculate session lengths. If you use session IDs, Amplitude will not add additional events to your monthly event volume limit.
That said, if trackingStart Session
andEnd Session
events is critical to your analysis outside of session lengths, you can easily turn on tracking for these events by adding this line of code before initializing the SDK:
For Android:
Amplitude.getInstance().trackSessionEvents(true);
For iOS:
[Amplitude instance].trackingSessionEvents = true;
Important Notes:
- This only applies to Amplitude's Android and iOS SDKs. The JavaScript SDK cannot send default session events.
- Start/End Session events count towards your monthly event volume limit.
- The
End Session
event will be sent at the start of the user's next session. - You will not be able to add additional event properties to these Start Session and End Session events. If you would like to send event properties for session start/end events, try implementing your own custom
Open App
andClose App
events.
Out-of-session events
You can also log events as out-of-session by setting the session ID to -1
. Out-of-session events are not considered part of the current session. Because they do not extend the current session, they can be useful if you're logging events triggered by push notifications.
Out-of-session events are normally server-side events received by Amplitude (see our HTTP API article for more details). These events will appear in a user's event stream as disconnected green squares.
Custom session property
By default, Amplitude sorts events into sessions according to session ID. You can also define a session without instrumentation, simply by setting a constant property, custom timeout window, or beginning and ending events to group sessions.
You will need Admin or manager privileges to edit session definitions.
NOTE: Custom session definitions are only available in the User Sessions and Pathfinder charts, as well as user timelines. Sessions only include active events.
To set a custom session definition, follow these steps:
- From the left sidebar, navigate to Settings > Projects.
- Click on the project you want to work with.
- Click Session Definitions. The Session Definitions modal will appear.
Within the Session Definitions modal, you can set your specifications for any of three conditions that will define a session in your project. You can define one condition or multiple conditions, but be aware that all conditions you specify must be met in order for Amplitude to count a session.
If you do not define any of these conditions, Amplitude will default to Session ID as the session-defining property.
The conditions are:
-
-
-
-
- Session Property. Choose the property you want to use for grouping sessions. The default is session ID, but you can choose to group by an event or user property instead.
- Session Timeout. Enter the default timeout interval. Amplitude will count all events from the same user prior to the interval you specify as being part of a single session. We recommend a default of 30 minutes.
- Start and End Session Events. Specify the events that will signify the beginning and end of a session. The triggering of the ending event will end a session if it occurs before the timeout interval has elapsed.
-
-
-
- Finally, enter the confirmation phrase and click Save.
NOTE: Changing the session definition will apply to all User Session and Pathfinder charts, as well as user timelines, in your project. Be sure you understand what the impacts might be before setting or changing a custom session definition.
Be aware that Amplitude will treat combinations of conditions as a logical AND. For example, if you want to guarantee that all events in sessions are from the same source, you can accomplish this by using constant property and timeout window in tandem:
- Constant property =
device ID
- Session timeout =
30 min
Alternatively, to define sessions based on in-app usage, you can use start event and timeout window:
- Start event =
app open
- Session timeout =
5 min
How Amplitude calculates session length
Amplitude calculates session length via the following formula when session ID is used as the session property:
max(client_event_time) - min(client_event_time)
Where:
client_event_time
is the local timestamp (UTC) when the device logged the eventmax(client_event_time)
is the local timestamp (UTC) of the last event logged by the device
Additionally, hidden, inactive, and deleted events will not be included in the session length calculation. For example, let's say you have the following series of events:
Event A --> Event B --> Event C --> Event D
If Event C and Event D are hidden, inactive, or deleted in Govern, then the max(client_event_time)
is taken from Event B instead of Event D.