Skip to content

Commit

Permalink
Always make analytics /setup call
Browse files Browse the repository at this point in the history
  • Loading branch information
sponglord committed Aug 15, 2024
1 parent 6adc3b1 commit adda88b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-peas-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@adyen/adyen-web': minor
---

Always make the analytics /setup call. Only use the "enabled" boolean to control the sending of subsequent events.
70 changes: 41 additions & 29 deletions packages/lib/src/core/Analytics/Analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ const analyticsEventObj = {
target: 'PAN input'
} as CreateAnalyticsObject;

const applicationInfo = {
merchantApplication: {
name: 'merchant_application_name',
version: 'version'
},
externalPlatform: {
name: 'external_platform_name',
version: 'external_platform_version',
integrator: 'getSystemIntegratorName'
}
};

let analytics;

describe('Analytics initialisation and event queue', () => {
Expand All @@ -37,7 +49,14 @@ describe('Analytics initialisation and event queue', () => {
mockedCollectId.mockImplementation(() => collectIdPromiseMock);
collectIdPromiseMock.mockClear();

analytics = Analytics({ analytics: {}, loadingContext: '', locale: '', clientKey: '', amount, bundleType: '' });
analytics = Analytics({
analytics: { payload: { payloadData: 'test' } },
loadingContext: '',
locale: '',
clientKey: '',
amount,
bundleType: ''
});
});

test('Creates an Analytics module with defaultProps', () => {
Expand All @@ -50,33 +69,15 @@ describe('Analytics initialisation and event queue', () => {
expect(collectIdPromiseMock).toHaveLength(0);
});

test('Should not fire any calls if analytics is disabled', () => {
const analytics = Analytics({ analytics: { enabled: false }, loadingContext: '', locale: '', clientKey: '', amount, bundleType: '' });

void analytics.setUp(setUpEvent);
expect(collectIdPromiseMock).not.toHaveBeenCalled();
});

test('Calls the collectId endpoint by default, adding expected fields, including sanitising the passed analyticsData object', async () => {
const applicationInfo = {
merchantApplication: {
name: 'merchant_application_name',
version: 'version'
},
externalPlatform: {
name: 'external_platform_name',
version: 'external_platform_version',
integrator: 'getSystemIntegratorName'
}
};

test('Should still make the setup call even when analytics is disabled, adding expected fields, including sanitising the passed analyticsData object', async () => {
const checkoutAttemptId = 'my.attempt.id';

analytics = Analytics({
analytics: {
enabled: false,
analyticsData: {
applicationInfo,
checkoutAttemptId,
checkoutAttemptId, // checking that we can also pass in a checkoutAttemptId
// @ts-ignore - this is one of the things we're testing (that this object gets stripped out)
foo: {
bar: 'val'
Expand All @@ -86,6 +87,7 @@ describe('Analytics initialisation and event queue', () => {
loadingContext: '',
locale: '',
clientKey: '',
bundleType: '',
amount
});

Expand All @@ -101,18 +103,28 @@ describe('Analytics initialisation and event queue', () => {
expect(analytics.getCheckoutAttemptId()).toEqual(mockCheckoutAttemptId);
});

test('A second attempt to call "send" should fail (since we already have a checkoutAttemptId)', () => {
const payload = {
payloadData: 'test'
};
const analytics = Analytics({ analytics: { payload }, loadingContext: '', locale: '', clientKey: '', amount, bundleType: '' });

test('A second attempt to call "send" should fail (since we have retrieved a checkoutAttemptId)', () => {
void analytics.setUp(setUpEvent);

expect(collectIdPromiseMock).toHaveLength(0);
});

test('Create info event and see that it is held in a queue', () => {
test('Try to create info event but see that it fails because analytics.enabled is false', () => {
const analytics = Analytics({
analytics: { enabled: false },
loadingContext: '',
locale: '',
clientKey: '',
amount,
bundleType: ''
});

const aObj: AnalyticsObject = analytics.createAnalyticsEvent({ event: 'info', data: analyticsEventObj });

expect(aObj).toBe(undefined);
});

test('With Analytics being enabled by default, create info event and see that it is held in a queue', () => {
const aObj: AnalyticsObject = analytics.createAnalyticsEvent({ event: 'info', data: analyticsEventObj });

expect(aObj.timestamp).not.toBe(undefined);
Expand Down
8 changes: 5 additions & 3 deletions packages/lib/src/core/Analytics/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ const Analytics = ({ locale, clientKey, analytics, amount, analyticsContext, bun
* @param initialEvent -
*/
setUp: async (initialEvent: AnalyticsInitialEvent) => {
const { enabled, payload } = props; // TODO what is payload, is it ever used?
const { payload } = props; // TODO what is payload, is it ever used?

const analyticsData = processAnalyticsData(props.analyticsData);

if (enabled === true && !capturedCheckoutAttemptId) {
if (!capturedCheckoutAttemptId) {
try {
const checkoutAttemptId = await collectId({
...initialEvent,
Expand All @@ -86,6 +86,8 @@ const Analytics = ({ locale, clientKey, analytics, amount, analyticsContext, bun
getEventsQueue: () => eventsQueue,

createAnalyticsEvent: ({ event, data }: CreateAnalyticsEventObject): AnalyticsObject => {
if (!props.enabled) return;

const aObj: AnalyticsObject = createAnalyticsObject({
event,
...data
Expand All @@ -102,7 +104,7 @@ const Analytics = ({ locale, clientKey, analytics, amount, analyticsContext, bun
sendAnalytics: null
};

anlModule.sendAnalytics = analyticsPreProcessor(anlModule);
anlModule.sendAnalytics = props.enabled === true ? analyticsPreProcessor(anlModule) : () => {};

return anlModule;
};
Expand Down

0 comments on commit adda88b

Please sign in to comment.