Skip to content

Commit

Permalink
Reverting always making analytics setup call
Browse files Browse the repository at this point in the history
  • Loading branch information
sponglord committed Aug 22, 2024
1 parent 22b22dc commit 14fedcb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
70 changes: 29 additions & 41 deletions packages/lib/src/core/Analytics/Analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@ 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 @@ -49,14 +37,7 @@ describe('Analytics initialisation and event queue', () => {
mockedCollectId.mockImplementation(() => collectIdPromiseMock);
collectIdPromiseMock.mockClear();

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

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

test('Should still make the setup call even when analytics is disabled, adding expected fields, including sanitising the passed analyticsData object', async () => {
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'
}
};

const checkoutAttemptId = 'my.attempt.id';

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

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

test('A second attempt to call "send" should fail (since we have retrieved a checkoutAttemptId)', () => {
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: '' });

void analytics.setUp(setUpEvent);

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

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', () => {
test('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: 3 additions & 5 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 { payload } = props; // TODO what is payload, is it ever used?
const { enabled, payload } = props; // TODO what is payload, is it ever used?

const analyticsData = processAnalyticsData(props.analyticsData);

if (!capturedCheckoutAttemptId) {
if (enabled === true && !capturedCheckoutAttemptId) {
try {
const checkoutAttemptId = await collectId({
...initialEvent,
Expand All @@ -86,8 +86,6 @@ 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 @@ -104,7 +102,7 @@ const Analytics = ({ locale, clientKey, analytics, amount, analyticsContext, bun
sendAnalytics: null
};

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

return anlModule;
};
Expand Down

0 comments on commit 14fedcb

Please sign in to comment.