@@ -38,14 +38,16 @@ export class AmplitudeSessionPlugin extends EventPlugin {
3838 } ;
3939
4040 update ( settings : SegmentAPISettings , type : UpdateType ) {
41-
42- if ( type !== UpdateType . initial ) return ;
41+ if ( type !== UpdateType . initial ) {
42+ return ;
43+ }
4344 this . active = settings . integrations ?. hasOwnProperty ( this . key ) ?? false ;
4445 }
4546
4647 async execute ( event : SegmentEvent ) {
47-
48- if ( ! this . active ) return event ;
48+ if ( ! this . active ) {
49+ return event ;
50+ }
4951
5052 if ( this . sessionId === - 1 || this . lastEventTime === - 1 ) {
5153 await this . loadSessionData ( ) ;
@@ -163,74 +165,76 @@ export class AmplitudeSessionPlugin extends EventPlugin {
163165 } ;
164166
165167 private async startNewSessionIfNecessary ( ) {
166- if ( this . eventSessionId === - 1 ) {
167- this . eventSessionId = this . sessionId ;
168- }
169-
170- if ( this . resetPending ) return ;
171-
172- const current = Date . now ( ) ;
173- const withinSessionLimit = this . withinMinSessionTime ( current ) ;
174-
175- const isSessionExpired =
176- this . sessionId === - 1 ||
177- this . lastEventTime === - 1 ||
178- ! withinSessionLimit ;
168+ if ( this . eventSessionId === - 1 ) {
169+ this . eventSessionId = this . sessionId ;
170+ }
179171
180- this . lastEventTime = current ;
172+ if ( this . resetPending ) {
173+ return ;
174+ }
181175
182- if ( this . sessionId >= 0 && ! isSessionExpired ) {
183- // Continue current session
184-
185- await this . saveSessionData ( ) ;
186- return ;
187- }
176+ const current = Date . now ( ) ;
177+ const withinSessionLimit = this . withinMinSessionTime ( current ) ;
188178
189- // End old session and start a new one
190- await this . startNewSession ( ) ;
191- }
179+ const isSessionExpired =
180+ this . sessionId === - 1 || this . lastEventTime === - 1 || ! withinSessionLimit ;
192181
193- /**
194- * Handles the entire process of starting a new session.
195- * Can be called directly or from startNewSessionIfNecessary()
196- */
197- private async startNewSession ( ) {
198- if ( this . resetPending ) return ;
182+ if ( this . sessionId >= 0 && ! isSessionExpired ) {
183+ // Continue current session
184+ this . lastEventTime = current ;
199185
200- this . resetPending = true ;
186+ await this . saveSessionData ( ) ;
187+ return ;
188+ }
201189
202- const oldSessionId = this . sessionId ;
203- if ( oldSessionId >= 0 ) {
204- await this . endSession ( oldSessionId ) ;
190+ // End old session and start a new one
191+ await this . startNewSession ( ) ;
205192 }
206193
207- const newSessionId = Date . now ( ) ;
208- this . sessionId = newSessionId ;
209- this . eventSessionId = this . eventSessionId === - 1 ? newSessionId : this . eventSessionId ;
210- this . lastEventTime = newSessionId ;
194+ /**
195+ * Handles the entire process of starting a new session.
196+ * Can be called directly or from startNewSessionIfNecessary()
197+ */
198+ private async startNewSession ( ) {
199+ if ( this . resetPending ) {
200+ return ;
201+ }
211202
212- await this . saveSessionData ( ) ;
203+ this . resetPending = true ;
213204
214- console . log ( `[AmplitudeSession] startNewSession -> ${ newSessionId } ` ) ;
205+ const oldSessionId = this . sessionId ;
206+ if ( oldSessionId >= 0 ) {
207+ await this . endSession ( oldSessionId ) ;
208+ }
215209
216- await this . trackSessionStart ( newSessionId ) ;
210+ const newSessionId = Date . now ( ) ;
211+ this . sessionId = newSessionId ;
212+ this . eventSessionId =
213+ this . eventSessionId === - 1 ? newSessionId : this . eventSessionId ;
214+ this . lastEventTime = newSessionId ;
217215
218- }
216+ await this . saveSessionData ( ) ;
219217
220- /**
221- * Extracted analytics tracking into its own method
222- */
223- private async trackSessionStart ( sessionId : number ) {
224- this . analytics ?. track ( AMP_SESSION_START_EVENT , {
225- integrations : {
226- [ this . key ] : { session_id : sessionId } ,
227- } ,
228- } ) ;
229- }
218+ console . log ( `[AmplitudeSession] startNewSession -> ${ newSessionId } ` ) ;
230219
220+ await this . trackSessionStart ( newSessionId ) ;
221+ }
222+
223+ /**
224+ * Extracted analytics tracking into its own method
225+ */
226+ private async trackSessionStart ( sessionId : number ) {
227+ this . analytics ?. track ( AMP_SESSION_START_EVENT , {
228+ integrations : {
229+ [ this . key ] : { session_id : sessionId } ,
230+ } ,
231+ } ) ;
232+ }
231233
232234 private async endSession ( sessionId : number ) {
233- if ( this . sessionId === - 1 ) return ;
235+ if ( this . sessionId === - 1 ) {
236+ return ;
237+ }
234238
235239 console . log ( `[AmplitudeSession] endSession -> ${ this . sessionId } ` ) ;
236240
@@ -257,9 +261,13 @@ private async trackSessionStart(sessionId: number) {
257261 ) ;
258262 }
259263
264+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
260265 private disableAllIntegrations ( integrations ?: Record < string , any > ) {
266+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
261267 const result : Record < string , any > = { } ;
262- if ( ! integrations ) return result ;
268+ if ( ! integrations ) {
269+ return result ;
270+ }
263271 for ( const key of Object . keys ( integrations ) ) {
264272 result [ key ] = false ;
265273 }
@@ -268,7 +276,7 @@ private async trackSessionStart(sessionId: number) {
268276
269277 private withinMinSessionTime ( timestamp : number ) : boolean {
270278 const timeDelta = timestamp - this . lastEventTime ;
271- return timeDelta < MAX_SESSION_TIME_IN_MS ;
279+ return timeDelta < MAX_SESSION_TIME_IN_MS ;
272280 }
273281
274282 private handleAppStateChange = ( nextAppState : string ) => {
@@ -279,4 +287,3 @@ private async trackSessionStart(sessionId: number) {
279287 }
280288 } ;
281289}
282-
0 commit comments