-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathAmplitudeSessionPlugin.tsx
More file actions
206 lines (173 loc) · 5.11 KB
/
Copy pathAmplitudeSessionPlugin.tsx
File metadata and controls
206 lines (173 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
EventPlugin,
EventType,
IdentifyEventType,
PluginType,
SegmentAPISettings,
SegmentEvent,
TrackEventType,
ScreenEventType,
GroupEventType,
UpdateType,
AliasEventType,
SegmentClient,
} from '@segment/analytics-react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { AppState } from 'react-native';
const MAX_SESSION_TIME_IN_MS = 300000;
const SESSION_ID_KEY = 'previous_session_id';
const LAST_EVENT_TIME_KEY = 'last_event_time';
const AMP_SESSION_START_EVENT = 'session_start';
const AMP_SESSION_END_EVENT = 'session_end';
export class AmplitudeSessionPlugin extends EventPlugin {
type = PluginType.enrichment;
key = 'Actions Amplitude';
active = false;
sessionId = -1;
lastEventTime = -1;
configure = async (analytics: SegmentClient): Promise<void> => {
this.analytics = analytics;
await this.loadSessionData();
AppState.addEventListener('change', this.handleAppStateChange);
};
update(settings: SegmentAPISettings, type: UpdateType) {
if (type !== UpdateType.initial) {
return;
}
this.active = settings.integrations?.hasOwnProperty(this.key) ?? false;
}
async execute(event: SegmentEvent) {
if (!this.active) {
return event;
}
if (this.sessionId === -1 || this.lastEventTime === -1) {
await this.loadSessionData();
}
await this.startNewSessionIfNecessary();
let result = event;
switch (result.type) {
case EventType.IdentifyEvent:
result = this.identify(result);
break;
case EventType.TrackEvent:
result = this.track(result);
break;
case EventType.ScreenEvent:
result = this.screen(result);
break;
case EventType.AliasEvent:
result = this.alias(result);
break;
case EventType.GroupEvent:
result = this.group(result);
break;
}
this.lastEventTime = Date.now();
await this.saveSessionData();
return result;
}
identify(event: IdentifyEventType) {
return this.insertSession(event) as IdentifyEventType;
}
track(event: TrackEventType) {
return this.insertSession(event) as TrackEventType;
}
screen(event: ScreenEventType) {
event.properties = {
...event.properties,
name: event.name,
};
return this.insertSession(event) as ScreenEventType;
}
group(event: GroupEventType) {
return this.insertSession(event) as GroupEventType;
}
alias(event: AliasEventType) {
return this.insertSession(event) as AliasEventType;
}
async reset() {
this.sessionId = -1;
this.lastEventTime = -1;
await AsyncStorage.removeItem(SESSION_ID_KEY);
await AsyncStorage.removeItem(LAST_EVENT_TIME_KEY);
}
private insertSession = (event: SegmentEvent) => {
const integrations = event.integrations || {};
const existingIntegration = integrations[this.key];
const hasSessionId =
typeof existingIntegration === 'object' &&
existingIntegration !== null &&
'session_id' in existingIntegration;
if (hasSessionId) {
return event;
}
return {
...event,
integrations: {
...integrations,
[this.key]: { session_id: this.sessionId },
},
};
};
private onBackground = () => {
this.lastEventTime = Date.now();
this.saveSessionData();
};
private onForeground = () => {
this.startNewSessionIfNecessary();
};
private async startNewSessionIfNecessary() {
const current = Date.now();
const sessionExpired =
this.sessionId === -1 ||
this.lastEventTime === -1 ||
current - this.lastEventTime >= MAX_SESSION_TIME_IN_MS;
// Avoid loop: if session just started recently, skip restarting
if (!sessionExpired || current - this.sessionId < 1000) {
return;
}
await this.endSession();
await this.startNewSession();
}
private async startNewSession() {
this.sessionId = Date.now();
this.lastEventTime = this.sessionId;
await this.saveSessionData();
this.analytics?.track(AMP_SESSION_START_EVENT, {
integrations: {
[this.key]: { session_id: this.sessionId },
},
});
}
private async endSession() {
if (this.sessionId === -1) {
return;
}
this.analytics?.track(AMP_SESSION_END_EVENT, {
integrations: {
[this.key]: { session_id: this.sessionId },
},
});
}
private async loadSessionData() {
const storedSessionId = await AsyncStorage.getItem(SESSION_ID_KEY);
const storedLastEventTime = await AsyncStorage.getItem(LAST_EVENT_TIME_KEY);
this.sessionId = storedSessionId != null ? Number(storedSessionId) : -1;
this.lastEventTime =
storedLastEventTime != null ? Number(storedLastEventTime) : -1;
}
private async saveSessionData() {
await AsyncStorage.setItem(SESSION_ID_KEY, this.sessionId.toString());
await AsyncStorage.setItem(
LAST_EVENT_TIME_KEY,
this.lastEventTime.toString()
);
}
private handleAppStateChange = (nextAppState: string) => {
if (nextAppState === 'active') {
this.onForeground();
} else if (nextAppState === 'background') {
this.onBackground();
}
};
}