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: preload events not processed with detached load call #1953

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 6 additions & 22 deletions packages/analytics-js/__tests__/app/RudderAnalytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,6 @@ describe('Core - Rudder Analytics Facade', () => {
]);
});

it('should return an empty array when globalThis.rudderanalytics is not an array', () => {
const rudderAnalyticsInstance = new RudderAnalytics();
(globalThis as typeof window).rudderanalytics = undefined;
const result = rudderAnalyticsInstance.getPreloadedEvents();
expect(result).toEqual([]);
});

it('should return buffered events array when globalThis.rudderanalytics is an array', () => {
const bufferedEvents = [
['track'],
['consent', { sendPageEvent: true }],
['load', 'dummyWriteKey', 'dummyDataPlaneUrl', { option1: true }],
['consent', { sendPageEvent: false }],
['track'],
];
(window as any).rudderanalytics = bufferedEvents;
const rudderAnalyticsInstance = new RudderAnalytics();
const result = rudderAnalyticsInstance.getPreloadedEvents();
expect(result).toEqual(bufferedEvents);
});

it('should return the global singleton if it exists', () => {
const globalSingleton = rudderAnalytics;
rudderAnalytics = new RudderAnalytics();
Expand Down Expand Up @@ -850,7 +829,12 @@ describe('Core - Rudder Analytics Facade', () => {
enabled: true,
},
});
expect(rudderAnalyticsInstance.trackPageLifecycleEvents).toHaveBeenCalledWith([], {
expect(rudderAnalyticsInstance.trackPageLifecycleEvents).toHaveBeenCalledWith([
['consent', { sendPageEvent: true }],
['consent', { sendPageEvent: false }],
['track'],
['track'],
], {
autoTrack: {
enabled: true,
},
Expand Down
23 changes: 8 additions & 15 deletions packages/analytics-js/src/app/RudderAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
promotePreloadedConsentEventsToTop,
} from '../components/preloadBuffer';
import type { PreloadedEventCall } from '../components/preloadBuffer/types';
import { setExposedGlobal } from '../components/utilities/globals';
import { getExposedGlobal, setExposedGlobal } from '../components/utilities/globals';
import type { IAnalytics } from '../components/core/IAnalytics';
import { Analytics } from '../components/core/Analytics';
import { defaultLogger } from '../services/Logger/Logger';
Expand Down Expand Up @@ -158,7 +158,9 @@ class RudderAnalytics implements IRudderAnalytics<IAnalytics> {
}

this.setDefaultInstanceKey(writeKey);
const preloadedEventsArray = this.getPreloadedEvents();
// Get the preloaded events array from global buffer instead of window.rudderanalytics
// as the constructor must have already pushed the events to the global buffer
const preloadedEventsArray = getExposedGlobal(GLOBAL_PRELOAD_BUFFER) as PreloadedEventCall[];

// Track page loaded lifecycle event if enabled
this.trackPageLifecycleEvents(preloadedEventsArray, loadOptions);
Expand All @@ -179,17 +181,6 @@ class RudderAnalytics implements IRudderAnalytics<IAnalytics> {
}
}

/**
* A function to get preloaded events array from global object
* @returns preloaded events array
*/
// eslint-disable-next-line class-methods-use-this
getPreloadedEvents() {
return Array.isArray((globalThis as typeof window).rudderanalytics)
? ((globalThis as typeof window).rudderanalytics as unknown as PreloadedEventCall[])
: ([] as PreloadedEventCall[]);
}

/**
* A function to track page lifecycle events like page loaded and page unloaded
* @param preloadedEventsArray
Expand Down Expand Up @@ -298,15 +289,17 @@ class RudderAnalytics implements IRudderAnalytics<IAnalytics> {
* remaining preloaded events array in global object
*/
triggerBufferedLoadEvent() {
const preloadedEventsArray = this.getPreloadedEvents();
const preloadedEventsArray = Array.isArray((globalThis as typeof window).rudderanalytics)
? ((globalThis as typeof window).rudderanalytics as unknown as PreloadedEventCall[])
: ([] as PreloadedEventCall[]);

// Get any load method call that is buffered if any
// BTW, load method is also removed from the array
// So, the Analytics object can directly consume the remaining events
const loadEvent: PreloadedEventCall = getPreloadedLoadEvent(preloadedEventsArray);

// Set the final preloaded events array in global object
setExposedGlobal(GLOBAL_PRELOAD_BUFFER, clone(preloadedEventsArray));
setExposedGlobal(GLOBAL_PRELOAD_BUFFER, clone([...preloadedEventsArray]));

// Process load method if present in the buffered requests
if (loadEvent.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
window.manualLoad(
$('#writeKey').val(),
$('#dataplaneURL').val(),
JSON.parse($('#loadOptions').val() ? $('#loadOptions').val() : {}),
JSON.parse($('#loadOptions').val() ? $('#loadOptions').val() : {})
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
window.manualLoad(
$('#writeKey').val(),
$('#dataplaneURL').val(),
JSON.parse($('#loadOptions').val() ? $('#loadOptions').val() : {}),
JSON.parse($('#loadOptions').val() ? $('#loadOptions').val() : {})
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,12 @@ const ignoredProperties = [
{
key: `message.integrations.Google Analytics 4 (GA4).sessionId`,
type: 'number',
optional: true,
},
{
key: `message.integrations.Google Analytics 4 (GA4).clientId`,
type: 'string',
optional: true,
},
{
key: `message.integrations.Google Analytics 4 (GA4).sessionNumber`,
Expand Down
Loading