Skip to content

Commit

Permalink
fix: data plane events buffer on load option flag
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Jan 9, 2025
1 parent 2b6b668 commit 7a9d55e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
22 changes: 14 additions & 8 deletions packages/analytics-js/src/components/core/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,26 @@ class Analytics implements IAnalytics {
}
}

// Set in state the desired activeDestinations to inject in DOM
this.setActiveDestinations();

// Initialize event manager
this.eventManager?.init();

// Mark the SDK as initialized
state.lifecycle.status.value = 'initialized';
}

private setActiveDestinations() {
this.pluginsManager?.invokeSingle(
'nativeDestinations.setActiveDestinations',
state,
this.pluginsManager,
this.errorHandler,
this.logger,
);
}

/**
* Load plugins
*/
Expand Down Expand Up @@ -383,14 +396,7 @@ class Analytics implements IAnalytics {
return;
}

// Set in state the desired activeDestinations to inject in DOM
this.pluginsManager?.invokeSingle(
'nativeDestinations.setActiveDestinations',
state,
this.pluginsManager,
this.errorHandler,
this.logger,
);
this.setActiveDestinations();

const totalDestinationsToLoad = state.nativeDestinations.activeDestinations.value.length;
if (totalDestinationsToLoad === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,25 @@ class EventRepository implements IEventRepository {
});

const bufferEventsBeforeConsent = shouldBufferEventsForPreConsent(state);
if (!bufferEventsBeforeConsent) {
this.startDpEventsQueue();
}
}

private startDpEventsQueue() {
const bufferEventsUntilReady = state.loadOptions.value
.bufferDataPlaneEventsUntilReady as boolean;
const hybridDestExist = state.nativeDestinations.activeDestinations.value.some(
(dest: Destination) => isHybridModeDestination(dest),
);
const shouldBufferDpEvents = bufferEventsUntilReady && hybridDestExist;

// Start the queue processing only when the destinations are ready or hybrid mode destinations exist
// However, events will be enqueued for now.
// At the time of processing the events, the integrations config data from destinations
// is merged into the event object
let timeoutId: number;
// Start the queue when no event buffering is required
// or when the client destinations are ready
effect(() => {
const shouldBufferDpEvents =
state.loadOptions.value.bufferDataPlaneEventsUntilReady === true &&
state.nativeDestinations.clientDestinationsReady.value === false;

const hybridDestExist = state.nativeDestinations.activeDestinations.value.some(
(dest: Destination) => isHybridModeDestination(dest),
);

if (
(hybridDestExist === false || shouldBufferDpEvents === false) &&
!bufferEventsBeforeConsent &&
(!shouldBufferDpEvents || state.nativeDestinations.clientDestinationsReady.value) &&
this.dataplaneEventsQueue?.scheduleTimeoutActive !== true
) {
(globalThis as typeof window).clearTimeout(timeoutId);
Expand All @@ -142,7 +143,7 @@ class EventRepository implements IEventRepository {
});

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

resume() {
if (this.dataplaneEventsQueue?.scheduleTimeoutActive !== true) {
if (state.consents.postConsent.value.discardPreConsentEvents) {
this.dataplaneEventsQueue?.clear();
this.destinationsEventsQueue?.clear();
}

this.dataplaneEventsQueue?.start();
if (
this.dataplaneEventsQueue?.scheduleTimeoutActive !== true &&
state.consents.postConsent.value.discardPreConsentEvents
) {
this.dataplaneEventsQueue?.clear();
this.destinationsEventsQueue?.clear();
}

this.startDpEventsQueue();
}

/**
Expand Down

0 comments on commit 7a9d55e

Please sign in to comment.