Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions trackers/react-native-tracker/src/plugins/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function newSessionPlugin({
sessionContext = true,
foregroundSessionTimeout,
backgroundSessionTimeout,
onSessionUpdateCallback,
}: TrackerConfiguration & SessionConfiguration & { asyncStorage: AsyncStorage }): Promise<SessionPlugin> {
let sessionState = await resumeStoredSession(namespace, asyncStorage);
await storeSessionState(namespace, sessionState, asyncStorage);
Expand All @@ -82,15 +83,17 @@ export async function newSessionPlugin({
// check if session has timed out and start a new one if necessary
const now = new Date();
const timeDiff = now.getTime() - lastUpdateTs;
if (timeDiff > getTimeoutMs()) {
const didPreviousSessionTimeout = timeDiff > getTimeoutMs();
if (didPreviousSessionTimeout) {
startNewSession();
storeSessionState(namespace, sessionState, asyncStorage);
}
lastUpdateTs = now.getTime();

// update event properties
sessionState.eventIndex = (sessionState.eventIndex ?? 0) + 1;
if (sessionState.eventIndex === 1) {
const isFirstEvent = sessionState.eventIndex === 1;
if (isFirstEvent) {
sessionState.firstEventId = payloadBuilder.getPayload().eid as string;
sessionState.firstEventTimestamp = now.toISOString();
}
Expand All @@ -112,6 +115,10 @@ export async function newSessionPlugin({
data: { ...sessionState },
});
}

if (didPreviousSessionTimeout || isFirstEvent) {
onSessionUpdateCallback?.(sessionState);
}
};

return {
Expand Down
4 changes: 4 additions & 0 deletions trackers/react-native-tracker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export interface SessionConfiguration {
* @defaultValue true
*/
sessionContext?: boolean;
/**
* A callback function that is called when the session changes
*/
onSessionUpdateCallback?: (session: SessionState) => void;
}

/**
Expand Down