Skip to content

Commit 4e3f07b

Browse files
author
Sunita Prajapati
committed
refactor: refactored to add properties for Event_Session_ID and Last_Event_Time to automatic update to storage
- Added new properties - Modified persistent test cases
1 parent d5cf5a6 commit 4e3f07b

File tree

2 files changed

+63
-25
lines changed

2 files changed

+63
-25
lines changed

packages/plugins/plugin-amplitudeSession/src/AmplitudeSessionPlugin.tsx

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { AppState } from 'react-native';
1818

1919
const MAX_SESSION_TIME_IN_MS = 300000;
2020
const SESSION_ID_KEY = 'previous_session_id';
21+
const EVENT_SESSION_ID_KEY = 'event_session_id';
2122
const LAST_EVENT_TIME_KEY = 'last_event_time';
2223
const AMP_SESSION_START_EVENT = 'session_start';
2324
const AMP_SESSION_END_EVENT = 'session_end';
@@ -27,10 +28,41 @@ export class AmplitudeSessionPlugin extends EventPlugin {
2728
key = 'Actions Amplitude';
2829
active = false;
2930
sessionId = -1;
30-
lastEventTime = -1;
31-
eventSessionId = -1;
31+
private _eventSessionId = -1;
32+
private _lastEventTime = -1;
3233
resetPending = false;
3334

35+
get eventSessionId() {
36+
return this._eventSessionId;
37+
}
38+
set eventSessionId(value: number) {
39+
this._eventSessionId = value;
40+
if (value !== -1) {
41+
AsyncStorage.setItem(EVENT_SESSION_ID_KEY, value.toString()).catch(
42+
(err) =>
43+
console.warn(
44+
'[AmplitudeSessionPlugin] Failed to persist eventSessionId:',
45+
err
46+
)
47+
);
48+
}
49+
}
50+
51+
get lastEventTime() {
52+
return this._lastEventTime;
53+
}
54+
set lastEventTime(value: number) {
55+
this._lastEventTime = value;
56+
if (value !== -1) {
57+
AsyncStorage.setItem(LAST_EVENT_TIME_KEY, value.toString()).catch((err) =>
58+
console.warn(
59+
'[AmplitudeSessionPlugin] Failed to persist lastEventTime:',
60+
err
61+
)
62+
);
63+
}
64+
}
65+
3466
configure = async (analytics: SegmentClient): Promise<void> => {
3567
this.analytics = analytics;
3668
await this.loadSessionData();
@@ -73,7 +105,7 @@ export class AmplitudeSessionPlugin extends EventPlugin {
73105
}
74106

75107
this.lastEventTime = Date.now();
76-
await this.saveSessionData();
108+
//await this.saveSessionData();
77109
return result;
78110
}
79111

@@ -129,9 +161,13 @@ export class AmplitudeSessionPlugin extends EventPlugin {
129161

130162
async reset() {
131163
this.sessionId = -1;
164+
this.eventSessionId = -1;
132165
this.lastEventTime = -1;
133-
await AsyncStorage.removeItem(SESSION_ID_KEY);
134-
await AsyncStorage.removeItem(LAST_EVENT_TIME_KEY);
166+
await AsyncStorage.multiRemove([
167+
SESSION_ID_KEY,
168+
EVENT_SESSION_ID_KEY,
169+
LAST_EVENT_TIME_KEY,
170+
]);
135171
}
136172

137173
private insertSession = (event: SegmentEvent) => {
@@ -157,7 +193,7 @@ export class AmplitudeSessionPlugin extends EventPlugin {
157193

158194
private onBackground = () => {
159195
this.lastEventTime = Date.now();
160-
this.saveSessionData();
196+
//this.saveSessionData();
161197
};
162198

163199
private onForeground = () => {
@@ -180,10 +216,6 @@ export class AmplitudeSessionPlugin extends EventPlugin {
180216
this.sessionId === -1 || this.lastEventTime === -1 || !withinSessionLimit;
181217

182218
if (this.sessionId >= 0 && !isSessionExpired) {
183-
// Continue current session
184-
this.lastEventTime = current;
185-
186-
await this.saveSessionData();
187219
return;
188220
}
189221

@@ -213,7 +245,7 @@ export class AmplitudeSessionPlugin extends EventPlugin {
213245
this.eventSessionId === -1 ? newSessionId : this.eventSessionId;
214246
this.lastEventTime = newSessionId;
215247

216-
await this.saveSessionData();
248+
//await this.saveSessionData();
217249

218250
console.log(`[AmplitudeSession] startNewSession -> ${newSessionId}`);
219251

@@ -248,18 +280,24 @@ export class AmplitudeSessionPlugin extends EventPlugin {
248280
private async loadSessionData() {
249281
const storedSessionId = await AsyncStorage.getItem(SESSION_ID_KEY);
250282
const storedLastEventTime = await AsyncStorage.getItem(LAST_EVENT_TIME_KEY);
283+
const storedEventSessionId = await AsyncStorage.getItem(
284+
EVENT_SESSION_ID_KEY
285+
);
286+
251287
this.sessionId = storedSessionId != null ? Number(storedSessionId) : -1;
252288
this.lastEventTime =
253289
storedLastEventTime != null ? Number(storedLastEventTime) : -1;
290+
this.eventSessionId =
291+
storedEventSessionId != null ? Number(storedEventSessionId) : -1;
254292
}
255293

256-
private async saveSessionData() {
257-
await AsyncStorage.setItem(SESSION_ID_KEY, this.sessionId.toString());
258-
await AsyncStorage.setItem(
259-
LAST_EVENT_TIME_KEY,
260-
this.lastEventTime.toString()
261-
);
262-
}
294+
// private async saveSessionData() {
295+
// await AsyncStorage.setItem(SESSION_ID_KEY, this.sessionId.toString());
296+
// await AsyncStorage.setItem(
297+
// LAST_EVENT_TIME_KEY,
298+
// this.lastEventTime.toString()
299+
// );
300+
// }
263301

264302
// eslint-disable-next-line @typescript-eslint/no-explicit-any
265303
private disableAllIntegrations(integrations?: Record<string, any>) {

packages/plugins/plugin-amplitudeSession/src/__tests__/AmplitudeSessionPlugin.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ describe('AmplitudeSessionPlugin', () => {
664664
await plugin.execute(mockEvent);
665665

666666
expect(mockAsyncStorage.setItem).toHaveBeenCalledWith(
667-
'previous_session_id',
667+
'event_session_id',
668668
plugin.sessionId.toString()
669669
);
670670
expect(mockAsyncStorage.setItem).toHaveBeenCalledWith(
@@ -680,12 +680,12 @@ describe('AmplitudeSessionPlugin', () => {
680680

681681
expect(plugin.sessionId).toBe(-1);
682682
expect(plugin.lastEventTime).toBe(-1);
683-
expect(mockAsyncStorage.removeItem).toHaveBeenCalledWith(
684-
'previous_session_id'
685-
);
686-
expect(mockAsyncStorage.removeItem).toHaveBeenCalledWith(
687-
'last_event_time'
688-
);
683+
expect(plugin.eventSessionId).toBe(-1);
684+
expect(mockAsyncStorage.multiRemove).toHaveBeenCalledWith([
685+
'previous_session_id',
686+
'event_session_id',
687+
'last_event_time',
688+
]);
689689
});
690690
});
691691

0 commit comments

Comments
 (0)