Skip to content

Commit

Permalink
refactor(analytics): send level field in the setup call
Browse files Browse the repository at this point in the history
  • Loading branch information
longyulongyu committed Nov 5, 2024
1 parent 0ebf396 commit 0f82ac0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-brooms-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@adyen/adyen-web': patch
---

Send `level` field to the analytic `setup` call. If analytics is `enabled`, we send `level` value `all`, otherwise we send `initial`.
47 changes: 47 additions & 0 deletions packages/lib/src/core/Analytics/Analytics.setup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Analytics from './Analytics';
import collectId from '../Services/analytics/collect-id';

jest.mock('../Services/analytics/collect-id');

const mockedCollectId = collectId as jest.Mock;

describe('Analytics setup call', () => {
const mockCollectIdPromise: jest.Mock = jest.fn();

beforeEach(() => {
mockCollectIdPromise.mockResolvedValue(null);
mockedCollectId.mockImplementation(() => mockCollectIdPromise);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should send "initial" value for the level field', async () => {
const analytics = Analytics({
amount: undefined,
bundleType: '',
clientKey: undefined,
loadingContext: undefined,
locale: undefined,
analytics: {
enabled: false
}
});

await analytics.setUp({ component: '', containerWidth: 0, flavor: '' });
expect(mockCollectIdPromise).toHaveBeenCalledWith(expect.objectContaining({ level: 'initial' }));
});

it('should send "all" value for the level field', async () => {
await Analytics({
amount: undefined,
bundleType: '',
clientKey: undefined,
loadingContext: undefined,
locale: undefined
}).setUp({ component: '', containerWidth: 0, flavor: '' });

expect(mockCollectIdPromise).toHaveBeenCalledWith(expect.objectContaining({ level: 'all' }));
});
});
2 changes: 1 addition & 1 deletion packages/lib/src/core/Analytics/Analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('Analytics initialisation and event queue', () => {
expect(collectIdPromiseMock).toHaveBeenCalled();
await Promise.resolve(); // wait for the next tick

const enhancedSetupEvent = { ...setUpEvent, applicationInfo, checkoutAttemptId };
const enhancedSetupEvent = { ...setUpEvent, applicationInfo, checkoutAttemptId, level: 'initial' };

expect(collectIdPromiseMock).toHaveBeenCalledWith({ ...enhancedSetupEvent });

Expand Down
20 changes: 13 additions & 7 deletions packages/lib/src/core/Analytics/Analytics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import CollectId from '../Services/analytics/collect-id';
import EventsQueue, { EventsQueueModule } from './EventsQueue';
import { ANALYTICS_EVENT, AnalyticsInitialEvent, AnalyticsObject, AnalyticsProps, CreateAnalyticsEventObject } from './types';
import { ANALYTICS_EVENT_ERROR, ANALYTICS_EVENT_INFO, ANALYTICS_EVENT_LOG, ANALYTICS_INFO_TIMER_INTERVAL, ANALYTICS_PATH } from './constants';
import {
ANALYTIC_LEVEL,
ANALYTICS_EVENT_ERROR,
ANALYTICS_EVENT_INFO,
ANALYTICS_EVENT_LOG,
ANALYTICS_INFO_TIMER_INTERVAL,
ANALYTICS_PATH
} from './constants';
import { debounce } from '../../utils/debounce';
import { AnalyticsModule } from '../../types/global-types';
import { createAnalyticsObject, processAnalyticsData } from './utils';
Expand Down Expand Up @@ -62,18 +69,17 @@ const Analytics = ({ locale, clientKey, analytics, amount, analyticsContext, bun
* @param initialEvent -
*/
setUp: async (initialEvent: AnalyticsInitialEvent) => {
const { payload } = props; // TODO what is payload, is it ever used?

const { payload, enabled } = props; // TODO what is payload, is it ever used?
const level = enabled ? ANALYTIC_LEVEL.all : ANALYTIC_LEVEL.initial;
const analyticsData = processAnalyticsData(props.analyticsData);

if (!capturedCheckoutAttemptId) {
try {
const checkoutAttemptId = await collectId({
capturedCheckoutAttemptId = await collectId({
...initialEvent,
...(payload && { ...payload }),
...(Object.keys(analyticsData).length && { ...analyticsData })
...(Object.keys(analyticsData).length && { ...analyticsData }),
...{ level }
});
capturedCheckoutAttemptId = checkoutAttemptId;
} catch (e: any) {
console.warn(`Fetching checkoutAttemptId failed.${e ? ` Error=${e}` : ''}`);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/lib/src/core/Analytics/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ export const ANALYTICS_EXPRESS_PAGES_ARRAY = ['cart', 'minicart', 'pdp', 'checko
export const ALLOWED_ANALYTICS_DATA = ['applicationInfo', 'checkoutAttemptId'];

export const NO_CHECKOUT_ATTEMPT_ID = 'fetch-checkoutAttemptId-failed';

export const ANALYTIC_LEVEL = {
all: 'all',
initial: 'initial'
};

0 comments on commit 0f82ac0

Please sign in to comment.