Managing Sessions in Mobile: Using the SessionController Interface

Kyla Oyamot  
Edited

Snowplow mobile trackers offer flexible session management tools, enabling developers to track user interactions effectively. Whether you need to manually start a new session, pause/resume session tracking, or access session properties for event stitching, the SessionController provides all the necessary functionality.

Managing Sessions

There may be cases where you may want to start, pause or resume a session. This can be easily done using the SessionController, and is particularly useful for use cases like:

  • Shared Devices: Resetting sessions between users, such as in kiosks or demo devices.
  • Explicit User Actions: Triggering a new session after a transaction, logout, or user profile switch.
  • Custom Business Logic: Aligning session resets with your specific app workflows.

Starting a New Session

The SessionController allows you to manually start a new session in scenarios where session resets are tied to user actions rather than automatic logic. 

To manually start a new session, use the startNewSession() method from the session property of your tracker:

tracker.session.startNewSession()

This method:

  1. Expires the current session immediately.
  2. Generates a new sessionId.
  3. Updates the sessionIndex to reflect the new session sequence.

Pause and Resume Session Tracking

The SessionController also provides methods to temporarily pause or resume session tracking, offering flexibility for specific app workflows:

tracker.session.pause()
tracker.session.resume()

 

Accessing Session Information for Event Stitching

When stitching Snowplow client-side events with server-side events, you often use identifiers like domainUserId and sessionId on the web. In mobile, the equivalent properties are accessible through the SessionController.

How to Access sessionIdm and userId on Mobile

Android
// Access the tracker:
val trackerInstance = Snowplow.defaultTracker()

// Access the desired variables
val sessionId = trackerInstance.session.sessionId
val sessionIndex = trackerInstance.session.sessionIndex
val userId = trackerInstance.session.userId
iOS
// Access the tracker:
let sessionController = Snowplow.defaultTracker?.session

// Access the desired variables
let sessionId = sessionController?.sessionId
let sessionIndex = sessionController?.sessionIndex
let userId = sessionController?.userId

Mobile Equivalents of Web Identifiers

  • sessionId: Equivalent to domain_sessionid on the web.
  • userId: Equivalent to domain_userid on the web. This is tied to the app installation and only changes if the app is uninstalled and reinstalled.
  • sessionIndex: Tracks the sequence of sessions for this user, useful for ordering data.

Forwarding Identifiers to Your Backend

These properties can be included in API requests from your mobile app to your backend services. By including sessionId and userId with server-side events, you can stitch them to client-side events for a unified view of user behavior.

 

Conclusion

The SessionController is a powerful tool for managing sessions in mobile apps. Whether you need to manually start new sessions, access session properties for stitching data, or pause/resume session tracking, Snowplow’s mobile trackers provide flexibility and control for every use case.

For more details, refer to the official documentation on the iOS SessionController and Android SessionController.