From 63d36cacbffa27ff6464be66764f33d0c195c137 Mon Sep 17 00:00:00 2001 From: Sai Kumar Battinoju Date: Thu, 9 Jan 2025 12:36:48 +0530 Subject: [PATCH 1/2] 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 187b5dcd4e..07a9ffd611 100644 --- a/packages/analytics-js/src/components/core/Analytics.ts +++ b/packages/analytics-js/src/components/core/Analytics.ts @@ -297,6 +297,9 @@ class Analytics implements IAnalytics { } } + // Set in state the desired activeDestinations to inject in DOM + this.setActiveDestinations(); + // Initialize event manager this.eventManager?.init(); @@ -304,6 +307,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 */ @@ -389,14 +402,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 b2698e5d2f..dc119dfa9e 100644 --- a/packages/analytics-js/src/components/eventRepository/EventRepository.ts +++ b/packages/analytics-js/src/components/eventRepository/EventRepository.ts @@ -96,24 +96,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); @@ -122,7 +123,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(); @@ -132,14 +133,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(); } /** From 663d9cd6bef61568df084a9ded9bf171fbcf4bfa Mon Sep 17 00:00:00 2001 From: Sai Kumar Battinoju Date: Thu, 17 Apr 2025 20:55:26 +0530 Subject: [PATCH 2/2] chore: update documentation --- .../src/components/eventRepository/EventRepository.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/analytics-js/src/components/eventRepository/EventRepository.ts b/packages/analytics-js/src/components/eventRepository/EventRepository.ts index dc119dfa9e..955632706a 100644 --- a/packages/analytics-js/src/components/eventRepository/EventRepository.ts +++ b/packages/analytics-js/src/components/eventRepository/EventRepository.ts @@ -104,6 +104,7 @@ class EventRepository implements IEventRepository { private startDpEventsQueue() { const bufferEventsUntilReady = state.loadOptions.value .bufferDataPlaneEventsUntilReady as boolean; + const hybridDestExist = state.nativeDestinations.activeDestinations.value.some( (dest: Destination) => isHybridModeDestination(dest), ); @@ -111,7 +112,7 @@ class EventRepository implements IEventRepository { let timeoutId: number; // Start the queue when no event buffering is required - // or when the client destinations are ready + // or when buffering is required and the client destinations are ready effect(() => { if ( (!shouldBufferDpEvents || state.nativeDestinations.clientDestinationsReady.value) &&