Skip to content

Commit b6b2ca3

Browse files
committed
feat: make setSessionId return promise
1 parent 7530205 commit b6b2ca3

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

packages/analytics-browser/src/browser-client.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
setConnectorUserId,
1313
isNewSession,
1414
isPageViewTrackingEnabled,
15+
WebAttribution,
1516
} from '@amplitude/analytics-client-common';
1617
import {
1718
BrowserClient,
@@ -23,11 +24,12 @@ import {
2324
Revenue as IRevenue,
2425
TransportType,
2526
OfflineDisabled,
27+
AmplitudeReturn,
28+
Result,
2629
} from '@amplitude/analytics-types';
2730
import { convertProxyObjectToRealObject, isInstanceProxy } from './utils/snippet-helper';
2831
import { Context } from './plugins/context';
2932
import { useBrowserConfig, createTransport } from './config';
30-
import { WebAttribution } from '@amplitude/analytics-client-common';
3133
import { pageViewTrackingPlugin } from '@amplitude/plugin-page-view-tracking-browser';
3234
import { formInteractionTracking } from './plugins/form-interaction-tracking';
3335
import { fileDownloadTracking } from './plugins/file-download-tracking';
@@ -172,13 +174,14 @@ export class AmplitudeBrowser extends AmplitudeCore implements BrowserClient {
172174
}
173175

174176
setSessionId(sessionId: number) {
177+
const promise = [];
175178
if (!this.config) {
176179
this.q.push(this.setSessionId.bind(this, sessionId));
177-
return;
180+
return returnWrapper(Promise.resolve());
178181
}
179182
// Prevents starting a new session with the same session ID
180183
if (sessionId === this.config.sessionId) {
181-
return;
184+
return returnWrapper(Promise.resolve());
182185
}
183186

184187
const previousSessionId = this.getSessionId();
@@ -191,32 +194,37 @@ export class AmplitudeBrowser extends AmplitudeCore implements BrowserClient {
191194

192195
if (isSessionTrackingEnabled(this.config.defaultTracking)) {
193196
if (previousSessionId && lastEventTime) {
194-
this.track(DEFAULT_SESSION_END_EVENT, undefined, {
195-
device_id: this.previousSessionDeviceId,
196-
event_id: ++lastEventId,
197-
session_id: previousSessionId,
198-
time: lastEventTime + 1,
199-
user_id: this.previousSessionUserId,
200-
});
197+
promise.push(
198+
this.track(DEFAULT_SESSION_END_EVENT, undefined, {
199+
device_id: this.previousSessionDeviceId,
200+
event_id: ++lastEventId,
201+
session_id: previousSessionId,
202+
time: lastEventTime + 1,
203+
user_id: this.previousSessionUserId,
204+
}),
205+
);
201206
}
202207
this.config.lastEventTime = this.config.sessionId;
203208
}
204209

205210
// Fire web attribution event when enable webAttribution tracking
206211
// 1. has new campaign (call setSessionId from init function)
207212
// 2. or shouldTrackNewCampaign (call setSessionId from async process(event) when there has new campaign and resetSessionOnNewCampaign = true )
208-
const isCampaignEventTracked = this.trackCampaignEventIfNeeded(++lastEventId);
213+
const isCampaignEventTracked = this.trackCampaignEventIfNeeded(++lastEventId, promise);
209214

210215
if (isSessionTrackingEnabled(this.config.defaultTracking)) {
211-
this.track(DEFAULT_SESSION_START_EVENT, undefined, {
212-
event_id: isCampaignEventTracked ? ++lastEventId : lastEventId,
213-
session_id: this.config.sessionId,
214-
time: this.config.lastEventTime,
215-
});
216+
promise.push(
217+
this.track(DEFAULT_SESSION_START_EVENT, undefined, {
218+
event_id: isCampaignEventTracked ? ++lastEventId : lastEventId,
219+
session_id: this.config.sessionId,
220+
time: this.config.lastEventTime,
221+
}),
222+
);
216223
}
217224

218225
this.previousSessionDeviceId = this.config.deviceId;
219226
this.previousSessionUserId = this.config.userId;
227+
return returnWrapper(Promise.all(promise));
220228
}
221229

222230
extendSession() {
@@ -268,21 +276,22 @@ export class AmplitudeBrowser extends AmplitudeCore implements BrowserClient {
268276
return super.revenue(revenue, eventOptions);
269277
}
270278

271-
private trackCampaignEventIfNeeded(lastEventId?: number) {
279+
private trackCampaignEventIfNeeded(lastEventId?: number, eventsPromise?: AmplitudeReturn<Result>[]) {
272280
if (!this.webAttribution || !this.webAttribution.shouldTrackNewCampaign) {
273281
return false;
274282
}
275283
const campaignEvent = this.webAttribution.generateCampaignEvent(lastEventId);
276-
this.track(campaignEvent);
284+
eventsPromise?.push(this.track(campaignEvent));
277285
this.config.loggerProvider.log('Tracking attribution.');
278286
return true;
279287
}
280288

281289
async process(event: Event) {
282290
const currentTime = Date.now();
283291
const isEventInNewSession = isNewSession(this.config.sessionTimeout, this.config.lastEventTime);
284-
const shouldSetSessionIdOnNewCampaign = this.webAttribution && this.webAttribution.shouldSetSessionIdOnNewCampaign();
285-
292+
const shouldSetSessionIdOnNewCampaign =
293+
this.webAttribution && this.webAttribution.shouldSetSessionIdOnNewCampaign();
294+
286295
if (
287296
event.event_type !== DEFAULT_SESSION_START_EVENT &&
288297
event.event_type !== DEFAULT_SESSION_END_EVENT &&

0 commit comments

Comments
 (0)