Skip to content

Commit d896ef4

Browse files
refactor: handled foreground and background app state
1 parent 3643518 commit d896ef4

1 file changed

Lines changed: 47 additions & 48 deletions

File tree

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

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515

1616
import AsyncStorage from '@react-native-async-storage/async-storage';
1717
import { AppState } from 'react-native';
18-
//import { AppState } from 'react-native';
1918

2019
const MAX_SESSION_TIME_IN_MS = 300000;
2120
const SESSION_ID_KEY = 'previous_session_id';
@@ -29,18 +28,12 @@ export class AmplitudeSessionPlugin extends EventPlugin {
2928
active = false;
3029
sessionId = -1;
3130
lastEventTime = -1;
32-
sessionTimer: ReturnType<typeof setTimeout> | undefined;
3331

34-
configure(analytics: SegmentClient): void {
32+
configure = async (analytics: SegmentClient): Promise<void> => {
3533
this.analytics = analytics;
34+
await this.loadSessionData();
3635
AppState.addEventListener('change', this.handleAppStateChange);
37-
this.loadSessionData();
38-
if (this.sessionId === -1) {
39-
this.startNewSession();
40-
} else {
41-
this.startNewSessionIfNecessary();
42-
}
43-
}
36+
};
4437

4538
update(settings: SegmentAPISettings, type: UpdateType) {
4639
if (type !== UpdateType.initial) {
@@ -53,13 +46,14 @@ export class AmplitudeSessionPlugin extends EventPlugin {
5346
if (!this.active) {
5447
return event;
5548
}
56-
await this.startNewSessionIfNecessary();
5749

58-
let result = event;
59-
if (result.type === EventType.TrackEvent) {
60-
console.log(result.event);
50+
if (this.sessionId === -1 || this.lastEventTime === -1) {
51+
await this.loadSessionData();
6152
}
6253

54+
await this.startNewSessionIfNecessary();
55+
56+
let result = event;
6357
switch (result.type) {
6458
case EventType.IdentifyEvent:
6559
result = this.identify(result);
@@ -108,77 +102,82 @@ export class AmplitudeSessionPlugin extends EventPlugin {
108102
return this.insertSession(event) as AliasEventType;
109103
}
110104

111-
reset() {
112-
//this.resetSession();
105+
async reset() {
106+
this.sessionId = -1;
107+
this.lastEventTime = -1;
108+
await AsyncStorage.removeItem(SESSION_ID_KEY);
109+
await AsyncStorage.removeItem(LAST_EVENT_TIME_KEY);
113110
}
114111

115112
private insertSession = (event: SegmentEvent) => {
116-
const returnEvent = event;
117113
const integrations = event.integrations || {};
118114
const existingIntegration = integrations[this.key];
119115
const hasSessionId =
120116
typeof existingIntegration === 'object' &&
121117
existingIntegration !== null &&
122118
'session_id' in existingIntegration;
123119

124-
// If session_id exists, return as is
125120
if (hasSessionId) {
126-
return returnEvent;
121+
return event;
127122
}
128123

129-
returnEvent.integrations = {
130-
...integrations,
131-
[this.key]: {
132-
session_id: this.sessionId,
124+
return {
125+
...event,
126+
integrations: {
127+
...integrations,
128+
[this.key]: { session_id: this.sessionId },
133129
},
134130
};
135-
return returnEvent;
136131
};
137132

138-
private onBackground() {
133+
private onBackground = () => {
139134
this.lastEventTime = Date.now();
140-
141135
this.saveSessionData();
142-
}
136+
};
143137

144-
private onForeground() {
138+
private onForeground = () => {
145139
this.startNewSessionIfNecessary();
146-
}
140+
};
147141

148142
private async startNewSessionIfNecessary() {
149143
const current = Date.now();
150-
const withinSessionLimit =
151-
current - this.lastEventTime < MAX_SESSION_TIME_IN_MS;
152-
if (this.sessionId >= 0 && withinSessionLimit) {
144+
145+
const sessionExpired =
146+
this.sessionId === -1 ||
147+
this.lastEventTime === -1 ||
148+
current - this.lastEventTime >= MAX_SESSION_TIME_IN_MS;
149+
150+
// Avoid loop: if session just started recently, skip restarting
151+
if (!sessionExpired || current - this.sessionId < 1000) {
153152
return;
154153
}
155-
this.lastEventTime = current;
154+
156155
await this.endSession();
157156
await this.startNewSession();
158157
}
159158

160159
private async startNewSession() {
161160
this.sessionId = Date.now();
162-
const copy = this.sessionId;
163-
if (this.analytics) {
164-
this.analytics.track(AMP_SESSION_START_EVENT, {
165-
integrations: {
166-
[this.key]: { session_id: copy },
167-
},
168-
});
169-
}
161+
this.lastEventTime = this.sessionId;
170162
await this.saveSessionData();
163+
164+
this.analytics?.track(AMP_SESSION_START_EVENT, {
165+
integrations: {
166+
[this.key]: { session_id: this.sessionId },
167+
},
168+
});
171169
}
172170

173171
private async endSession() {
174-
const copy = this.sessionId;
175-
if (this.analytics) {
176-
this.analytics.track(AMP_SESSION_END_EVENT, {
177-
integrations: {
178-
[this.key]: { session_id: copy },
179-
},
180-
});
172+
if (this.sessionId === -1) {
173+
return;
181174
}
175+
176+
this.analytics?.track(AMP_SESSION_END_EVENT, {
177+
integrations: {
178+
[this.key]: { session_id: this.sessionId },
179+
},
180+
});
182181
}
183182

184183
private async loadSessionData() {

0 commit comments

Comments
 (0)