Session Management¶
Events collected by the SDK are annotated with a session identifier (ID). This session identifier is utilized to group events emitted by the SDK and can be used to retrieve events from a specific session.
The SDK manages the used session identifier using one of the two following strategies:
ActivityBased(Kotlin) /activityBased(Swift) /SessionStrategy.Activity(JavaScript): A session strategy that generates a new session ID after a certain period of app inactivity. The inactivity duration is measured by the minutes elapsed since the last log. The session ID is persisted to disk and survives app restarts.Fixed(Kotlin) /fixed(Swift) /SessionStrategy.Fixed: A session strategy that never expires the session ID but does not survive process restart.
The minimal session strategy setup looks as follows:
// `ActivityBased` session strategy
Logger.start(
// ...
sessionStrategy = SessionStrategy.ActivityBased()
)
// `Fixed` session strategy
Logger.start(
// ...
sessionStrategy = SessionStrategy.Fixed
)
// `ActivityBased` session strategy
Logger.start(
// ...
new SessionStrategy.ActivityBased()
);
// `Fixed` session strategy
Logger.start(
// ...
new SessionStrategy.Fixed()
);
// `activityBased` session strategy
Logger.start(
// ...
sessionStrategy: .activityBased()
)
// `fixed` session strategy
Logger.start(
// ...
sessionStrategy: .fixed()
)
// `activityBased` session strategy
[CAPLogger
// ...
sessionStrategy: [CAPSessionStrategy activityBased]
];
// `fixed` session strategy
[CAPLogger
// ...
sessionStrategy: [CAPSessionStrategy fixed]
];
// `activityBased` session strategy
init("<your-api-key>", SessionStrategy.Activity);
// `fixed` session strategy
init("<your-api-key>", SessionStrategy.Fixed);
// `activityBased` session strategy
init("<your-api-key>", SessionStrategy.Activity);
// `fixed` session strategy
init("<your-api-key>", SessionStrategy.Fixed);
Retrieving Session ID¶
The current session identifier can be retrieved when needed using the Logger getter. Session ID is only available after the Logger has been started.
Logger.sessionId
Logger.getSessionId();
Logger.sessionID
[CAPLogger sessionID];
import { getSessionId } from '@bitdrift/react-native';
getSessionId();
Retrieving Session URL¶
Similarly, the full session permalink URL can be retrieved using the Logger getter. This is particularly helpful when integrating with external systems. Session URL is only available after the Logger has been started.
Logger.sessionUrl
Logger.getSessionUrl();
Logger.sessionURL
import { getSessionUrl } from '@bitdrift/react-native';
getSessionUrl();
Generating New Session ID¶
It may be desirable to generate a new session identifier in response to specific user action(s) (e.g., user logging out) or other events.
A new session identifier can be generated using a simple method call:
Logger.startNewSession()
Logger.startNewSession();
Logger.startNewSession()
Depending on the session strategy used, the new session identifier is generated in the following way:
ActivityBased(Kotlin) /activityBased(Swift) /Activity: A random, unique session identifier is generated each time thestartNewSession` method is called.Fixed(Kotlin) /fixed(Swift): A session identifier is set to the identifier retrieved using thesessionIdGenerator(Kotlin) lambda /sessionIDGenerator(Swift) closure. This closure is passed to the initializer of theFixed(Kotlin) /fixed(Swift) session strategy.
For the Fixed (Kotlin) / fixed (Swift) session strategy, a session ID generator can be passed in the following way:
var counter = 0;
Logger.start(
// ...
sessionStrategy = SessionStrategy.Fixed(sessionIdGenerator = { (++counter).toString() })
)
println(Logger.sessionId) // prints "1"
Logger.startNewSession()
println(Logger.sessionId) // prints "2"
AtomicInteger counter = new AtomicInteger(0);
Logger.start(
// ...
new SessionStrategy.Fixed(() -> Integer.toString(counter.incrementAndGet()))
);
println(Logger.getSessionId()); // prints "1"
Logger.startNewSession();
println(Logger.getSessionId()); // prints "2"
var counter = 0
Logger.start(
// ...
sessionStrategy: .fixed {
counter += 1;
return \"(counter)"
}
)
print(Logger.sessionID) // prints "1"
Logger.startNewSession()
print(Logger.sessionID) // prints "2"
Observing Session ID Changes¶
In the case of the ActivityBased (Kotlin) / activityBased (Swift) session strategy, the session identifier is managed on behalf of SDK customers and can change without their explicit request. For this reason, the SDK provides a way for customers to register for session identifier changes specific to this session strategy type:
Logger.start(
// ...
sessionStrategy = SessionStrategy.ActivityBased(
inactivityThresholdMins = 30,
onSessionIdChanged = { newSessionId ->
// Do something with the new session ID
}
)
)
Logger.start(
// ...
new SessionStrategy.ActivityBased(
30, //inactivityThresholdMins
newSessionId -> {
// Do something with the new session ID
return Unit.INSTANCE;
}
)
);
Logger.start(
// ...
sessionStrategy: .activityBased(
inactivityThresholdMins: 30,
onSessionIDChanged: { newSessionID in
// Do something with the new session ID
}
)
)