Skip to content

Commit 635c0f9

Browse files
sinchangjakebolam
authored andcommitted
fix: turn off Analytics in a local or test environment (#121)
* fix: turn off Analytics in a local or test environment * lint * Tweak * Tweak
1 parent f2cba1e commit 635c0f9

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/utils/Analytics.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Analytics {
1010
apiKey = process.env.AMPLITUDE_API_KEY,
1111
log,
1212
funnelId = uuid.v4(),
13+
isMock,
1314
}) {
1415
this.user = user
1516
this.repo = repo
@@ -18,9 +19,20 @@ class Analytics {
1819
this.funnelId = funnelId
1920
this.apiKey = apiKey
2021
this.log = log
22+
this.isMock = isMock
2123
}
2224

2325
track(eventName, metadata = {}) {
26+
const log = this.log
27+
28+
if (
29+
process.env.NODE_ENV === 'local' ||
30+
(process.env.NODE_ENV === 'test' && !this.isMock)
31+
) {
32+
log.info('Turn off Analytics in a local or test environment.')
33+
return
34+
}
35+
2436
if (!eventName) {
2537
throw new Error('Analytics missing event name')
2638
}
@@ -51,7 +63,6 @@ class Analytics {
5163
params.append('api_key', this.apiKey)
5264
params.append('event', JSON.stringify(events))
5365

54-
const log = this.log
5566
const newEventPromise = nodeFetch('https://api.amplitude.com/httpapi', {
5667
method: 'POST',
5768
body: params,

test/utils/Analytics.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ describe('Analytics', () => {
1111
user: 'mockusername',
1212
apiKey: 'mock api key',
1313
funnelId: 'mockFunnelId',
14+
isMock: true,
1415
log: {
1516
error: mockFn,
17+
info: mockFn,
1618
},
1719
})
1820

@@ -27,6 +29,6 @@ describe('Analytics', () => {
2729

2830
await analytics.finishQueue()
2931

30-
expect(mockFn).not.toBeCalled()
32+
expect(mockFn).not.toHaveBeenCalled()
3133
})
3234
})

0 commit comments

Comments
 (0)