Skip to content

Commit 13dd162

Browse files
committed
fix: use what the docs say for nodejs
1 parent 90c1f7b commit 13dd162

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/utils/Analytics.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const uuid = require('uuid')
22
const nodeFetch = require('node-fetch')
3-
const { URLSearchParams } = require('url')
43

54
class Analytics {
65
constructor({
@@ -38,28 +37,30 @@ class Analytics {
3837
},
3938
}
4039

41-
const events = [
42-
// TODO batch up to 10 events at a time
43-
event,
44-
]
40+
const inputBody = {
41+
api_key: this.apiKey,
42+
events: [
43+
// TODO batch up to 10 events at a time
44+
event,
45+
],
46+
}
4547

4648
const log = this.log
47-
48-
const params = new URLSearchParams()
49-
params.append('api_key', this.apiKey)
50-
params.append('event', JSON.stringify(events))
51-
52-
const newEventPromise = nodeFetch('https://api.amplitude.com/httpapi', {
49+
const newEventPromise = nodeFetch('https://api.amplitude.com/batch', {
5350
method: 'POST',
54-
body: params,
51+
body: inputBody,
52+
headers: {
53+
'Content-Type': 'application/json',
54+
Accept: '*/*',
55+
},
5556
timeout: 5000,
5657
redirect: 'error',
5758
follow: 0,
5859
})
5960
.then(response => {
6061
if (!response.ok) {
6162
log.error(response)
62-
log.error(response.text())
63+
log.error(response.json())
6364
}
6465
return response
6566
})

test/utils/Analytics.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ const Analytics = require('../../src/utils/Analytics')
44

55
describe('Analytics', () => {
66
test('Analytics', async () => {
7+
const mockFn = jest.fn()
78
const analytics = new Analytics({
89
repo: 'all-contributors-bot',
910
owner: 'all-contributors',
1011
user: 'mockusername',
1112
apiKey: 'mock api key',
1213
funnelId: 'mockFunnelId',
14+
log: {
15+
error: mockFn,
16+
},
1317
})
1418

1519
nock('https://api.amplitude.com')
16-
.post(`/httpapi`, body => {
20+
.post(`/batch`, body => {
1721
expect(body).toMatchSnapshot()
1822
return true
1923
})
@@ -22,5 +26,7 @@ describe('Analytics', () => {
2226
analytics.track('my-event')
2327

2428
await analytics.finishQueue()
29+
30+
expect(mockFn).not.toBeCalled()
2531
})
2632
})
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Analytics Analytics 1`] = `
4-
Object {
5-
"api_key": "mock api key",
6-
"event": "[{\\"user_id\\":\\"mockusername\\",\\"event_type\\":\\"my-event\\",\\"user_properties\\":{\\"repo\\":\\"all-contributors-bot\\",\\"owner\\":\\"all-contributors\\"},\\"event_properties\\":{\\"funnel_id\\":\\"mockFunnelId\\"}}]",
7-
}
8-
`;
3+
exports[`Analytics Analytics 1`] = `"[object Object]"`;

0 commit comments

Comments
 (0)