Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data plane events buffer on load option flag #1997

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading