Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class AmplitudeSessionPlugin extends EventPlugin {
active = false;
sessionId = -1;
lastEventTime = -1;
eventSessionId = -1;
resetPending = false;

configure = async (analytics: SegmentClient): Promise<void> => {
this.analytics = analytics;
Expand All @@ -50,9 +52,7 @@ export class AmplitudeSessionPlugin extends EventPlugin {
if (this.sessionId === -1 || this.lastEventTime === -1) {
await this.loadSessionData();
}

await this.startNewSessionIfNecessary();

let result = event;
switch (result.type) {
case EventType.IdentifyEvent:
Expand All @@ -74,7 +74,6 @@ export class AmplitudeSessionPlugin extends EventPlugin {

this.lastEventTime = Date.now();
await this.saveSessionData();

return result;
}

Expand All @@ -83,6 +82,32 @@ export class AmplitudeSessionPlugin extends EventPlugin {
}

track(event: TrackEventType) {
const eventName = event.event;

if (eventName === AMP_SESSION_START_EVENT) {
this.resetPending = false;
this.eventSessionId = this.sessionId;
}

if (eventName === AMP_SESSION_END_EVENT) {
console.log(`[AmplitudeSession] EndSession = ${this.eventSessionId}`);
}

if (
eventName.startsWith('Amplitude') ||
eventName === AMP_SESSION_START_EVENT ||
eventName === AMP_SESSION_END_EVENT
) {
const integrations = this.disableAllIntegrations(event.integrations);
return {
...event,
integrations: {
...integrations,
[this.key]: { session_id: this.eventSessionId },
},
};
}

return this.insertSession(event) as TrackEventType;
}

Expand Down Expand Up @@ -140,42 +165,82 @@ export class AmplitudeSessionPlugin extends EventPlugin {
};

private async startNewSessionIfNecessary() {
if (this.eventSessionId === -1) {
this.eventSessionId = this.sessionId;
}

if (this.resetPending) {
return;
}

const current = Date.now();
const withinSessionLimit = this.withinMinSessionTime(current);

const isSessionExpired =
this.sessionId === -1 || this.lastEventTime === -1 || !withinSessionLimit;

const sessionExpired =
this.sessionId === -1 ||
this.lastEventTime === -1 ||
current - this.lastEventTime >= MAX_SESSION_TIME_IN_MS;
if (this.sessionId >= 0 && !isSessionExpired) {
// Continue current session
this.lastEventTime = current;

// Avoid loop: if session just started recently, skip restarting
if (!sessionExpired || current - this.sessionId < 1000) {
await this.saveSessionData();
return;
}

await this.endSession();
// End old session and start a new one
await this.startNewSession();
}

/**
* Handles the entire process of starting a new session.
* Can be called directly or from startNewSessionIfNecessary()
*/
private async startNewSession() {
this.sessionId = Date.now();
this.lastEventTime = this.sessionId;
if (this.resetPending) {
return;
}

this.resetPending = true;

const oldSessionId = this.sessionId;
if (oldSessionId >= 0) {
await this.endSession(oldSessionId);
}

const newSessionId = Date.now();
this.sessionId = newSessionId;
this.eventSessionId =
this.eventSessionId === -1 ? newSessionId : this.eventSessionId;
this.lastEventTime = newSessionId;

await this.saveSessionData();

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

await this.trackSessionStart(newSessionId);
}

/**
* Extracted analytics tracking into its own method
*/
private async trackSessionStart(sessionId: number) {
this.analytics?.track(AMP_SESSION_START_EVENT, {
integrations: {
[this.key]: { session_id: this.sessionId },
[this.key]: { session_id: sessionId },
},
});
}

private async endSession() {
private async endSession(sessionId: number) {
if (this.sessionId === -1) {
return;
}

console.log(`[AmplitudeSession] endSession -> ${this.sessionId}`);

this.analytics?.track(AMP_SESSION_END_EVENT, {
integrations: {
[this.key]: { session_id: this.sessionId },
[this.key]: { session_id: sessionId },
},
});
}
Expand All @@ -196,6 +261,24 @@ export class AmplitudeSessionPlugin extends EventPlugin {
);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private disableAllIntegrations(integrations?: Record<string, any>) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result: Record<string, any> = {};
if (!integrations) {
return result;
}
for (const key of Object.keys(integrations)) {
result[key] = false;
}
return result;
}

private withinMinSessionTime(timestamp: number): boolean {
const timeDelta = timestamp - this.lastEventTime;
return timeDelta < MAX_SESSION_TIME_IN_MS;
}

private handleAppStateChange = (nextAppState: string) => {
if (nextAppState === 'active') {
this.onForeground();
Expand Down
Loading