Skip to content

Commit

Permalink
chore: addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MoumitaM committed Oct 18, 2024
1 parent 26a26c5 commit 6455d95
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
2 changes: 2 additions & 0 deletions packages/analytics-js-common/src/types/ApplicationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ export type LifecycleState = {
};

export type AutoTrackState = {
enabled: Signal<boolean>;
pageLifecycle: PageLifecycleState;
};

export type PageLifecycleState = {
enabled: Signal<boolean>;
visitId: Signal<string | undefined>;
pageLoadedTimestamp: Signal<number | undefined>;
};
Expand Down
32 changes: 17 additions & 15 deletions packages/analytics-js/__tests__/app/RudderAnalytics.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { LoadOptions } from '@rudderstack/analytics-js-common/types/LoadOptions';
import type { PreloadedEventCall } from '../../src/components/preloadBuffer/types';
import { state } from '../../src/state';
import { RudderAnalytics } from '../../src/app/RudderAnalytics';
import { Analytics } from '../../src/components/core/Analytics';
Expand Down Expand Up @@ -322,20 +323,22 @@ describe('trackPageLifecycleEvents', () => {
});

it('should not add pageLifecycleEvents in the buffer when the tracking is not enabled through load options', () => {
rudderAnalyticsInstance.trackPageLifecycleEvents({});
const bufferedEvents: PreloadedEventCall[] = [];
rudderAnalyticsInstance.trackPageLifecycleEvents(bufferedEvents, {});

expect(window.RudderStackGlobals?.app?.preloadedEventsBuffer).toEqual([]);
expect(bufferedEvents).toEqual([]);
});

it('should inherit enabled and options properties from autoTrack load option', () => {
rudderAnalyticsInstance.trackPageLifecycleEvents({
const bufferedEvents: PreloadedEventCall[] = [];
rudderAnalyticsInstance.trackPageLifecycleEvents(bufferedEvents, {
autoTrack: {
enabled: true,
options: { key: 'value' },
},
});

expect(window.RudderStackGlobals?.app?.preloadedEventsBuffer).toEqual([
expect(bufferedEvents).toEqual([
[
'track',
'Page Loaded',
Expand All @@ -346,7 +349,8 @@ describe('trackPageLifecycleEvents', () => {
});

it('should override enabled and options properties of autoTrack if provided in load option', () => {
rudderAnalyticsInstance.trackPageLifecycleEvents({
const bufferedEvents: PreloadedEventCall[] = [];
rudderAnalyticsInstance.trackPageLifecycleEvents(bufferedEvents, {
autoTrack: {
enabled: true,
options: { key: 'value' },
Expand All @@ -356,11 +360,12 @@ describe('trackPageLifecycleEvents', () => {
},
});

expect(window.RudderStackGlobals?.app?.preloadedEventsBuffer).toEqual([]);
expect(bufferedEvents).toEqual([]);
});

it('should track Page Loaded event irrespective of useBeacon load option', () => {
rudderAnalyticsInstance.trackPageLifecycleEvents({
const bufferedEvents: PreloadedEventCall[] = [];
rudderAnalyticsInstance.trackPageLifecycleEvents(bufferedEvents, {
useBeacon: false,
autoTrack: {
pageLifecycle: {
Expand All @@ -369,7 +374,7 @@ describe('trackPageLifecycleEvents', () => {
},
});

expect(window.RudderStackGlobals?.app?.preloadedEventsBuffer).toEqual([
expect(bufferedEvents).toEqual([
[
'track',
'Page Loaded',
Expand All @@ -380,8 +385,9 @@ describe('trackPageLifecycleEvents', () => {
});

it('should track Page Unloaded event if useBeacon is set to true and trackPageLifecycle feature is enabled', () => {
const bufferedEvents: PreloadedEventCall[] = [];
rudderAnalyticsInstance.track = jest.fn();
rudderAnalyticsInstance.trackPageLifecycleEvents({
rudderAnalyticsInstance.trackPageLifecycleEvents(bufferedEvents, {
useBeacon: true,
autoTrack: {
pageLifecycle: {
Expand All @@ -402,17 +408,13 @@ describe('trackPageLifecycleEvents', () => {
});

it('should invoke trackPageLifecycleEvents method when load API is called', () => {
const trackPageLifecycleEventsSpy = jest.spyOn(
rudderAnalyticsInstance,
'trackPageLifecycleEvents',
);

rudderAnalyticsInstance.trackPageLifecycleEvents = jest.fn();
rudderAnalyticsInstance.load('writeKey', 'data-plane-url', {
autoTrack: {
enabled: true,
},
});
expect(trackPageLifecycleEventsSpy).toHaveBeenCalledWith({
expect(rudderAnalyticsInstance.trackPageLifecycleEvents).toHaveBeenCalledWith([], {
autoTrack: {
enabled: true,
},
Expand Down
5 changes: 4 additions & 1 deletion packages/analytics-js/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@
// 'StorageMigrator',
// 'XhrQueue'
// ]
// trackPageLifecycle:{
// autoTrack:{
// enabled: true,
// pageLifecycle:{
// enabled:true
// }
// }
};

Expand Down
32 changes: 20 additions & 12 deletions packages/analytics-js/src/app/RudderAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,15 @@ class RudderAnalytics implements IRudderAnalytics<IAnalytics> {
}

this.setDefaultInstanceKey(writeKey);
const preloadedEventsArray = this.getPreloadedEvents();

// Track page loaded lifecycle event if enabled
this.trackPageLifecycleEvents(loadOptions);
this.trackPageLifecycleEvents(preloadedEventsArray, loadOptions);

// The array will be mutated in the below method
promotePreloadedConsentEventsToTop(preloadedEventsArray);

setExposedGlobal(GLOBAL_PRELOAD_BUFFER, clone(preloadedEventsArray));

this.analyticsInstances[writeKey] = new Analytics();
this.getAnalyticsInstance(writeKey).load(writeKey, dataPlaneUrl, loadOptions);
Expand All @@ -164,7 +171,10 @@ class RudderAnalytics implements IRudderAnalytics<IAnalytics> {
* @param loadOptions
* @returns
*/
trackPageLifecycleEvents(loadOptions?: Partial<LoadOptions>) {
trackPageLifecycleEvents(
preloadedEventsArray: PreloadedEventCall[],
loadOptions?: Partial<LoadOptions>,
) {
const { autoTrack, useBeacon } = loadOptions ?? {};
const {
enabled: autoTrackEnabled = false,
Expand All @@ -177,17 +187,14 @@ class RudderAnalytics implements IRudderAnalytics<IAnalytics> {
options = autoTrackOptions,
} = pageLifecycle ?? {};

const preloadedEventsArray = this.getPreloadedEvents();
state.autoTrack.enabled.value = autoTrackEnabled;
state.autoTrack.pageLifecycle.enabled.value = enabled;

if (enabled) {
this.trackPageLoadedEvent(events, options, preloadedEventsArray);
this.setupPageUnloadTracking(events, useBeacon, options);
if (!enabled) {
return;
}

// The array will be mutated in the below method
promotePreloadedConsentEventsToTop(preloadedEventsArray);

setExposedGlobal(GLOBAL_PRELOAD_BUFFER, clone(preloadedEventsArray));
this.trackPageLoadedEvent(events, options, preloadedEventsArray);
this.setupPageUnloadTracking(events, useBeacon, options);
}

/**
Expand All @@ -196,6 +203,7 @@ class RudderAnalytics implements IRudderAnalytics<IAnalytics> {
* @param options
* @param preloadedEventsArray
*/
// eslint-disable-next-line class-methods-use-this
private trackPageLoadedEvent(
events: PageLifecycleEvents[],
options: ApiOptions,
Expand Down Expand Up @@ -241,7 +249,7 @@ class RudderAnalytics implements IRudderAnalytics<IAnalytics> {
* Register page unload listener to track page unload event
* @param options
*/
private registerPageUnloadListener(options: object) {
private registerPageUnloadListener(options: ApiOptions) {
onPageLeave((isAccessible: boolean) => {
if (isAccessible === false && state.lifecycle.loaded.value) {
const pageUnloadedTimestamp = Date.now();
Expand Down
2 changes: 2 additions & 0 deletions packages/analytics-js/src/state/slices/autoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { signal } from '@preact/signals-core';
import type { AutoTrackState } from '@rudderstack/analytics-js-common/types/ApplicationState';

const autoTrackState: AutoTrackState = {
enabled: signal(false),
pageLifecycle: {
enabled: signal(false),
visitId: signal(undefined),
pageLoadedTimestamp: signal(undefined),
},
Expand Down
7 changes: 0 additions & 7 deletions packages/analytics-js/src/state/slices/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ const defaultLoadOptions: LoadOptions = {
},
sendAdblockPageOptions: {},
useServerSideCookies: false,
autoTrack: {
enabled: false,
pageLifecycle: {
enabled: false,
events: [PageLifecycleEvents.LOADED, PageLifecycleEvents.UNLOADED],
},
},
};

const loadOptionsState: LoadOptionsState = signal(clone(defaultLoadOptions));
Expand Down

0 comments on commit 6455d95

Please sign in to comment.