From 7a9d55e221d80e6df90240205b98b067d99dd182 Mon Sep 17 00:00:00 2001 From: Sai Kumar Battinoju Date: Thu, 9 Jan 2025 12:36:48 +0530 Subject: [PATCH] fix: data plane events buffer on load option flag --- .../src/components/core/Analytics.ts | 22 +++++---- .../eventRepository/EventRepository.ts | 46 ++++++++++--------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/packages/analytics-js/src/components/core/Analytics.ts b/packages/analytics-js/src/components/core/Analytics.ts index 8421dda6cf..50c2d6cbf1 100644 --- a/packages/analytics-js/src/components/core/Analytics.ts +++ b/packages/analytics-js/src/components/core/Analytics.ts @@ -291,6 +291,9 @@ class Analytics implements IAnalytics { } } + // Set in state the desired activeDestinations to inject in DOM + this.setActiveDestinations(); + // Initialize event manager this.eventManager?.init(); @@ -298,6 +301,16 @@ class Analytics implements IAnalytics { state.lifecycle.status.value = 'initialized'; } + private setActiveDestinations() { + this.pluginsManager?.invokeSingle( + 'nativeDestinations.setActiveDestinations', + state, + this.pluginsManager, + this.errorHandler, + this.logger, + ); + } + /** * Load plugins */ @@ -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) { diff --git a/packages/analytics-js/src/components/eventRepository/EventRepository.ts b/packages/analytics-js/src/components/eventRepository/EventRepository.ts index 0964d58aa9..59572cf27c 100644 --- a/packages/analytics-js/src/components/eventRepository/EventRepository.ts +++ b/packages/analytics-js/src/components/eventRepository/EventRepository.ts @@ -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); @@ -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(); @@ -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(); } /**