Learn about the session tracking feature in Kotlin SDK.
4 minute read
This guide covers the session tracking feature available in the Kotlin SDK.
Overview
A session is a group of user interactions with your mobile application taking place within a given timeframe. For example, a single session can contain multiple screen views, events, social interactions, and ecommerce transactions.
Tracking user sessions helps you gather insights into the user journey and analyze their behaviour in detail.
The Kotlin SDK supports two types of session tracking - Automatic and Manual.
Automatic session tracking
By default, RudderStack tracks user sessions automatically and attaches the session information to each event fired using the SDK.
When you fire an event from the Kotlin SDK, RudderStack automatically attaches the sessionId field to the event’s context. It is then persisted by the SDK, so the same session ID is attached to all the events of the same session, even if the app is restarted.
Automatic session start and end
An automatic session starts in the following scenarios:
After the previous user session has ended, or
No session data is present in the storage.
An automatic session ends when sessionTimoutInMillis amount of time has elapsed after the app is backgrounded or closed. This timeout is measured from the last app background/closed time to the app foreground/launch time.
Manage automatic session
As mentioned above, RudderStack enables automatic session tracking by default. To turn off this feature, set the sessionConfiguration parameter within Configuration while initializing the SDK, as shown:
Indicates whether automatic session tracking is enabled or disabled. The default value is true.
sessionTimeoutInMillis
Long
Denotes the session timeout duration. It is the time between the app closed or backgrounded to being foregrounded or relaunched. The session is timed out and new session is started after this time has elapsed. The default timeout value is 300,000 milliseconds (5 minutes).
Sample event
A sample event payload with the session information attached is shown below:
The sessionStart field is present only in the first event since the start of a new session.
Flow diagrams
The below flow diagrams explain the automatic session workflows when the user either launches or foregrounds the app.
App launched App foregrounded
Manual session tracking
A manual session is fully managed by the user, that is, the user is responsible for the session start and end and there is no concept of timeout. This feature also lets you provide a custom sessionId for the manual session.
Supported APIs
RudderStack provides the following APIs for manual session tracking:
API
Description
sessionStart
Used to start a new manual session. It takes sessionId as an optional parameter. If you do not provide any sessionId, then the current timestamp is used as the sessionId instead.
endSession
Must be called to end a session manually as there is no concept of automatic session end due to timeout.
If automatic session tracking is enabled and you call the startSessionAPI, then RudderStack disables automatic session tracking. Once you restart the app, the SDK resumes automatic session tracking if it is still enabled.
If automatic session tracking is enabled, calling the endSession API causes the active session to end. The automatic session tracking resumes once the app is relaunched, provided automatic session tracking is still enabled.
If automatic session tracking is disabled and you call the startSession() API, the manual session is active until you end it by calling the endSession API.
Sample snippets
// Starts a new manual session and automatically assigns a session ID.
rudderClient.startSession()// Passes a custom session ID while creating a new session.
rudderClient.startSession(sessionId)// Ends the user session and clears the session ID.
rudderClient.endSession()
Get the session ID
You can use the sessionId API to retrieve the current session ID for the manual or automatic session, as shown:
valsessionId=analytics.sessionId
Effect of Reset API on session tracking
Calling the reset API causes the session to refresh irrespective of whether it is automatic or manual - this means RudderStack restarts the session and generates a new session ID.
Note that the identify API also calls the reset API internally whenever you identify a new user and thus refreshes the session.
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.