Skip to content

Commit

Permalink
feat: add sendEnterpriseTrackEventWithDelay (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
long74100 committed Aug 24, 2022
1 parent 2d03231 commit 97879ea
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ frontend-enterprise
- `@edx/frontend-enterprise-catalog-search </packages/catalog-search>`_
- `@edx/frontend-enterprise-logistration </packages/logistration>`_
- `@edx/frontend-enterprise-utils </packages/utils>`_
- `@edx/frontend-enterprise-hotjar </packages/hotjar>`
- `@edx/frontend-enterprise-hotjar </packages/hotjar>`_

Each of these packages is published to NPM and have their own README files. The packages can be found in the ``packages/*`` folder.

Expand Down
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 97879ea

Please sign in to comment.