Skip to content

Commit 7a9d55e

Browse files
committed
fix: data plane events buffer on load option flag
1 parent 2b6b668 commit 7a9d55e

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

packages/analytics-js/src/components/core/Analytics.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,26 @@ class Analytics implements IAnalytics {
291291
}
292292
}
293293

294+
// Set in state the desired activeDestinations to inject in DOM
295+
this.setActiveDestinations();
296+
294297
// Initialize event manager
295298
this.eventManager?.init();
296299

297300
// Mark the SDK as initialized
298301
state.lifecycle.status.value = 'initialized';
299302
}
300303

304+
private setActiveDestinations() {
305+
this.pluginsManager?.invokeSingle(
306+
'nativeDestinations.setActiveDestinations',
307+
state,
308+
this.pluginsManager,
309+
this.errorHandler,
310+
this.logger,
311+
);
312+
}
313+
301314
/**
302315
* Load plugins
303316
*/
@@ -383,14 +396,7 @@ class Analytics implements IAnalytics {
383396
return;
384397
}
385398

386-
// Set in state the desired activeDestinations to inject in DOM
387-
this.pluginsManager?.invokeSingle(
388-
'nativeDestinations.setActiveDestinations',
389-
state,
390-
this.pluginsManager,
391-
this.errorHandler,
392-
this.logger,
393-
);
399+
this.setActiveDestinations();
394400

395401
const totalDestinationsToLoad = state.nativeDestinations.activeDestinations.value.length;
396402
if (totalDestinationsToLoad === 0) {

packages/analytics-js/src/components/eventRepository/EventRepository.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,25 @@ class EventRepository implements IEventRepository {
116116
});
117117

118118
const bufferEventsBeforeConsent = shouldBufferEventsForPreConsent(state);
119+
if (!bufferEventsBeforeConsent) {
120+
this.startDpEventsQueue();
121+
}
122+
}
123+
124+
private startDpEventsQueue() {
125+
const bufferEventsUntilReady = state.loadOptions.value
126+
.bufferDataPlaneEventsUntilReady as boolean;
127+
const hybridDestExist = state.nativeDestinations.activeDestinations.value.some(
128+
(dest: Destination) => isHybridModeDestination(dest),
129+
);
130+
const shouldBufferDpEvents = bufferEventsUntilReady && hybridDestExist;
119131

120-
// Start the queue processing only when the destinations are ready or hybrid mode destinations exist
121-
// However, events will be enqueued for now.
122-
// At the time of processing the events, the integrations config data from destinations
123-
// is merged into the event object
124132
let timeoutId: number;
133+
// Start the queue when no event buffering is required
134+
// or when the client destinations are ready
125135
effect(() => {
126-
const shouldBufferDpEvents =
127-
state.loadOptions.value.bufferDataPlaneEventsUntilReady === true &&
128-
state.nativeDestinations.clientDestinationsReady.value === false;
129-
130-
const hybridDestExist = state.nativeDestinations.activeDestinations.value.some(
131-
(dest: Destination) => isHybridModeDestination(dest),
132-
);
133-
134136
if (
135-
(hybridDestExist === false || shouldBufferDpEvents === false) &&
136-
!bufferEventsBeforeConsent &&
137+
(!shouldBufferDpEvents || state.nativeDestinations.clientDestinationsReady.value) &&
137138
this.dataplaneEventsQueue?.scheduleTimeoutActive !== true
138139
) {
139140
(globalThis as typeof window).clearTimeout(timeoutId);
@@ -142,7 +143,7 @@ class EventRepository implements IEventRepository {
142143
});
143144

144145
// Force start the data plane events queue processing after a timeout
145-
if (state.loadOptions.value.bufferDataPlaneEventsUntilReady === true) {
146+
if (shouldBufferDpEvents) {
146147
timeoutId = (globalThis as typeof window).setTimeout(() => {
147148
if (this.dataplaneEventsQueue?.scheduleTimeoutActive !== true) {
148149
this.dataplaneEventsQueue?.start();
@@ -152,14 +153,15 @@ class EventRepository implements IEventRepository {
152153
}
153154

154155
resume() {
155-
if (this.dataplaneEventsQueue?.scheduleTimeoutActive !== true) {
156-
if (state.consents.postConsent.value.discardPreConsentEvents) {
157-
this.dataplaneEventsQueue?.clear();
158-
this.destinationsEventsQueue?.clear();
159-
}
160-
161-
this.dataplaneEventsQueue?.start();
156+
if (
157+
this.dataplaneEventsQueue?.scheduleTimeoutActive !== true &&
158+
state.consents.postConsent.value.discardPreConsentEvents
159+
) {
160+
this.dataplaneEventsQueue?.clear();
161+
this.destinationsEventsQueue?.clear();
162162
}
163+
164+
this.startDpEventsQueue();
163165
}
164166

165167
/**

0 commit comments

Comments
 (0)