From 0a21da318be10f3be6a1f26b89bf87a907547613 Mon Sep 17 00:00:00 2001 From: Yu Long Date: Tue, 5 Nov 2024 16:15:45 +0100 Subject: [PATCH] refactor(analytics): send level field in the setup call --- .changeset/tasty-brooms-knock.md | 5 ++ .../core/Analytics/Analytics.setup.test.ts | 47 +++++++++++++++++++ .../lib/src/core/Analytics/Analytics.test.ts | 2 +- packages/lib/src/core/Analytics/Analytics.ts | 20 +++++--- packages/lib/src/core/Analytics/constants.ts | 5 ++ 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 .changeset/tasty-brooms-knock.md create mode 100644 packages/lib/src/core/Analytics/Analytics.setup.test.ts diff --git a/.changeset/tasty-brooms-knock.md b/.changeset/tasty-brooms-knock.md new file mode 100644 index 0000000000..a44eb68000 --- /dev/null +++ b/.changeset/tasty-brooms-knock.md @@ -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`. diff --git a/packages/lib/src/core/Analytics/Analytics.setup.test.ts b/packages/lib/src/core/Analytics/Analytics.setup.test.ts new file mode 100644 index 0000000000..ca3b8d4e38 --- /dev/null +++ b/packages/lib/src/core/Analytics/Analytics.setup.test.ts @@ -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' })); + }); +}); diff --git a/packages/lib/src/core/Analytics/Analytics.test.ts b/packages/lib/src/core/Analytics/Analytics.test.ts index 231a174108..56ccd2fa39 100644 --- a/packages/lib/src/core/Analytics/Analytics.test.ts +++ b/packages/lib/src/core/Analytics/Analytics.test.ts @@ -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 }); diff --git a/packages/lib/src/core/Analytics/Analytics.ts b/packages/lib/src/core/Analytics/Analytics.ts index cb1625ab38..a542406c3b 100644 --- a/packages/lib/src/core/Analytics/Analytics.ts +++ b/packages/lib/src/core/Analytics/Analytics.ts @@ -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'; @@ -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}` : ''}`); } diff --git a/packages/lib/src/core/Analytics/constants.ts b/packages/lib/src/core/Analytics/constants.ts index 8d64a39106..16e0483c52 100644 --- a/packages/lib/src/core/Analytics/constants.ts +++ b/packages/lib/src/core/Analytics/constants.ts @@ -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' +};