A session is the period of time a user has your app in the foreground or has your website open. For mobile, a session will begin when the app is brought into the foreground and a session will end when the app goes into the background and no events are fired for more than 5 minutes. Events that are sent within 5 minutes of each other are counted towards the current session.
On a browser, a session begins when the website is opened (when the SDK is initialized) and the last event triggered marks the end time of a session.
The session_id is the start time of the session in milliseconds since epoch (Unix Timestamp). All events within the same session will share the same session_id. If you are our SDKs, sessions are tracked automatically. If you are sending Amplitude using our HTTP API, you will have to explicitly set the session_id field to track sessions.
Table of Contents
- How Sessions Are Tracked
- Session Length
- Timeout Windows
How Sessions Are Tracked
The property 'session_id' is used to track session data. The session_id is the start time of the session in milliseconds since epoch (Unix Timestamp). All events with the same session_id are in the same session.
'[Amplitude] Start/End Session' Events
As of August 2015, we no longer by default track the '[Amplitude] Start Session' and '[Amplitude] End Session' events. We use session_id to track sessions instead so that start/end session events do not contribute to your monthly event count. If you would like to send event properties for session events, we recommend tracking your own 'Open App' and 'Close App' events so that you can send event properties with them (e.g. source of the app opening).
If 'Start/End Session' events are critical to your analyses, you can easily turn them on by including the following line before initializing the SDK.
[Amplitude instance].trackingSessionEvents = true;
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, meaning they do not extend the current session. This might be useful if you are logging events triggered by push notifications. Out-of-session events are normally server-side events sent to Amplitude. See our HTTP API article for information.
Session length is calculated by the following formula:
max(client_event_time) - session_id
client_event_time: Local timestamp (UTC) of when the device logged the event.
max(client_event_time): The local timestamp (UTC) of the last event logged by the device.
session_id: Local timestamp (UTC) of the start of the session.
Note: All timestamps are converted into number of milliseconds since epoch.
When the SDK is initialized, it determines whether the app is launched into the foreground or background. By default, the timeout window is set to 5 minutes, so if the app is launched in the foreground or if it has been in the background for greater than 5 minutes, a new session is created.Events triggered within the timeout window of each other are merged into a single session, and the timeout window can be customized via the SDK.
By default, the timeout window is set to 30 minutes, so when the SDK is initialized on the browser, after 30 minutes of inactivity, a new session will be created. Events triggered within the timeout window of each other are merged into a single session, and the timeout window can be customized via the SDK.