Skip to content

Commit f2cba1e

Browse files
committed
fix: use httpapi
1 parent 29d593c commit f2cba1e

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

src/utils/Analytics.js

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

45
class Analytics {
56
constructor({
@@ -41,30 +42,27 @@ class Analytics {
4142
},
4243
}
4344

44-
const inputBody = {
45-
api_key: this.apiKey,
46-
events: [
47-
// TODO batch up to 10 events at a time
48-
event,
49-
],
50-
}
45+
const events = [
46+
// TODO batch up to 10 events at a time
47+
event,
48+
]
49+
50+
const params = new URLSearchParams()
51+
params.append('api_key', this.apiKey)
52+
params.append('event', JSON.stringify(events))
5153

5254
const log = this.log
53-
const newEventPromise = nodeFetch('https://api.amplitude.com/batch', {
55+
const newEventPromise = nodeFetch('https://api.amplitude.com/httpapi', {
5456
method: 'POST',
55-
body: inputBody,
56-
headers: {
57-
'Content-Type': 'application/json',
58-
Accept: '*/*',
59-
},
57+
body: params,
6058
timeout: 5000,
6159
redirect: 'error',
6260
follow: 0,
6361
})
6462
.then(response => {
6563
if (!response.ok) {
6664
log.error(response)
67-
log.error(response.json())
65+
log.error(response.text())
6866
}
6967
return response
7068
})

test/utils/Analytics.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Analytics', () => {
1717
})
1818

1919
nock('https://api.amplitude.com')
20-
.post(`/batch`, body => {
20+
.post(`/httpapi`, body => {
2121
expect(body).toMatchSnapshot()
2222
return true
2323
})
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Analytics Analytics 1`] = `"[object Object]"`;
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+
`;

0 commit comments

Comments
 (0)