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..955632706a 100644 --- a/packages/analytics-js/src/components/eventRepository/EventRepository.ts +++ b/packages/analytics-js/src/components/eventRepository/EventRepository.ts @@ -96,24 +96,26 @@ class EventRepository implements IEventRepository { }); const bufferEventsBeforeConsent = shouldBufferEventsForPreConsent(state); + if (!bufferEventsBeforeConsent) { + this.startDpEventsQueue(); + } + } - // 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; - effect(() => { - const shouldBufferDpEvents = - state.loadOptions.value.bufferDataPlaneEventsUntilReady === true && - state.nativeDestinations.clientDestinationsReady.value === false; + private startDpEventsQueue() { + const bufferEventsUntilReady = state.loadOptions.value + .bufferDataPlaneEventsUntilReady as boolean; - const hybridDestExist = state.nativeDestinations.activeDestinations.value.some( - (dest: Destination) => isHybridModeDestination(dest), - ); + const hybridDestExist = state.nativeDestinations.activeDestinations.value.some( + (dest: Destination) => isHybridModeDestination(dest), + ); + const shouldBufferDpEvents = bufferEventsUntilReady && hybridDestExist; + let timeoutId: number; + // Start the queue when no event buffering is required + // or when buffering is required and the client destinations are ready + effect(() => { if ( - (hybridDestExist === false || shouldBufferDpEvents === false) && - !bufferEventsBeforeConsent && + (!shouldBufferDpEvents || state.nativeDestinations.clientDestinationsReady.value) && this.dataplaneEventsQueue?.scheduleTimeoutActive !== true ) { (globalThis as typeof window).clearTimeout(timeoutId); @@ -122,7 +124,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 +134,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(); } /**