Skip to content

Commit 25f95ca

Browse files
author
Rokas Lengvenis
committed
feat: return empty functions if analytics disabled
1 parent 9f2745a commit 25f95ca

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,22 @@ async function eventHandle(track, key, e) {
7979
}
8080

8181
// Export out init() function that kicks everything off
82-
export default function init(key) {
82+
export default function init(key, config) {
8383
if (!key) {
8484
throw new Error('A segment key must be passed to init');
8585
}
8686

87+
// If analytics are explicitly disabled
88+
if (config && config.disableBrowserAnalytics) {
89+
return {
90+
track: () => {},
91+
identify: () => {},
92+
page: () => {},
93+
group: () => {},
94+
alias: () => {},
95+
};
96+
}
97+
8798
const track = trackFactory(key);
8899

89100
// Listen to all click events in a page and track if enabled

src/index.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,16 @@ describe('init', () => {
5252
expect(spy).toHaveBeenCalled();
5353
expect(spy.mock.calls[0][0]).toEqual('click');
5454
});
55+
56+
it('should return empty functions in case analytics are disabled', () => {
57+
const { track, identify, page, group, alias } = init(KEY, {
58+
disableBrowserAnalytics: true,
59+
});
60+
61+
expect(track()).toBeUndefined();
62+
expect(identify()).toBeUndefined();
63+
expect(page()).toBeUndefined();
64+
expect(group()).toBeUndefined();
65+
expect(alias()).toBeUndefined();
66+
});
5567
});

0 commit comments

Comments
 (0)