Skip to content

Commit

Permalink
feat: add sendEnterpriseTrackEventWithDelay
Browse files Browse the repository at this point in the history
  • Loading branch information
long74100 committed Aug 24, 2022
1 parent 2d03231 commit c6654b5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
23 changes: 21 additions & 2 deletions packages/utils/src/analytics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sendTrackEvent } from '@edx/frontend-platform/analytics';

const sendEnterpriseTrackEvent = (enterpriseUUID, eventName, properties = {}) => {
export const sendEnterpriseTrackEvent = (enterpriseUUID, eventName, properties = {}) => {
sendTrackEvent(
eventName,
{
Expand All @@ -10,4 +10,23 @@ const sendEnterpriseTrackEvent = (enterpriseUUID, eventName, properties = {}) =>
);
};

export default sendEnterpriseTrackEvent;
// How long to delay an event, so that we allow enough time for any async analytics event call to resolve.
// https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/
export const TRACK_EVENT_DELAY_MS = 300; // 300ms replicates Segment's ``trackLink`` function

/**
* Delay for a certain period to allow time for analytics event call to resolve.
*/
export const sendEnterpriseTrackEventWithDelay = (
enterpriseId,
eventName,
properties = {},
delay = TRACK_EVENT_DELAY_MS,
) => {
sendEnterpriseTrackEvent(
enterpriseId,
eventName,
properties,
);
return new Promise((resolve) => setTimeout(resolve, delay));
};
2 changes: 1 addition & 1 deletion packages/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export {
hasFeatureFlagEnabled,
} from './utils';
export { renderWithRouter } from './test-utils';
export { default as sendEnterpriseTrackEvent } from './analytics';
export { sendEnterpriseTrackEvent, sendEnterpriseTrackEventWithDelay, TRACK_EVENT_DELAY_MS } from './analytics';

export { default as getLearnerPortalLinks } from './learnerPortalLinks';
39 changes: 38 additions & 1 deletion packages/utils/src/tests/analytics.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sendTrackEvent } from '@edx/frontend-platform/analytics';

import sendEnterpriseTrackEvent from '../analytics';
import { sendEnterpriseTrackEvent, sendEnterpriseTrackEventWithDelay, TRACK_EVENT_DELAY_MS } from '../analytics';

jest.mock('@edx/frontend-platform/analytics');

Expand All @@ -17,3 +17,40 @@ describe('sendEnterpriseTrackEvent', () => {
});
});
});

describe('sendEnterpriseTrackEventWithDelay', () => {
jest.spyOn(global, 'setTimeout');

const mockEnterpriseUUID = '1';
const mockEventName = 'event';
const mockEventProperties = { property1: 'property 1' };

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

it('should call sendTrackEvent after default delay', async () => {
await sendEnterpriseTrackEventWithDelay(mockEnterpriseUUID, mockEventName, mockEventProperties);

expect(sendTrackEvent).toHaveBeenCalledWith(mockEventName, {
enterpriseUUID: mockEnterpriseUUID,
...mockEventProperties,
});

expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), TRACK_EVENT_DELAY_MS);
});

it('should call sendTrackEvent after custom delay', async () => {
const customDelay = 3000;
await sendEnterpriseTrackEventWithDelay(mockEnterpriseUUID, mockEventName, mockEventProperties, customDelay);

expect(sendTrackEvent).toHaveBeenCalledWith(mockEventName, {
enterpriseUUID: mockEnterpriseUUID,
...mockEventProperties,
});

expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), customDelay);
});
});

0 comments on commit c6654b5

Please sign in to comment.