Skip to content

Commit 8caf67b

Browse files
committed
fix: ensure analytics logs correctly
1 parent 107a603 commit 8caf67b

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

src/utils/Analytics.js

Lines changed: 11 additions & 11 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({ user, repo, owner, apiKey, log, funnelId }) {
@@ -26,27 +27,26 @@ class Analytics {
2627
},
2728
}
2829

29-
const payload = {
30-
api_key: this.apiKey,
31-
event: [
32-
// TODO batch up to 10 events at a time
33-
event,
34-
],
35-
}
30+
const events = [
31+
// TODO batch up to 10 events at a time
32+
event,
33+
]
3634

3735
const log = this.log
3836

37+
const params = new URLSearchParams()
38+
params.append('api_key', this.apiKey)
39+
params.append('event', JSON.stringify(events))
40+
3941
const newEventPromise = nodeFetch('https://api.amplitude.com/httpapi', {
40-
method: 'post',
41-
body: JSON.stringify(payload),
42-
headers: { 'Content-Type': 'application/json' },
42+
method: 'POST',
43+
body: params,
4344
timeout: 5000,
4445
redirect: 'error',
4546
follow: 0,
4647
})
4748
.then(response => {
4849
if (!response.ok) {
49-
// TODO: error handling
5050
log.error(response)
5151
}
5252
return response

test/utils/__snapshots__/Analytics.test.js.snap

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33
exports[`Analytics Analytics 1`] = `
44
Object {
55
"api_key": "mock api key",
6-
"event": Array [
7-
Object {
8-
"event_properties": Object {
9-
"funnel_id": "mockFunnelId",
10-
},
11-
"event_type": "my-event",
12-
"user_id": "mockusername",
13-
"user_properties": Object {
14-
"owner": "all-contributors",
15-
"repo": "all-contributors-bot",
16-
},
17-
},
18-
],
6+
"event": "[{\\"user_id\\":\\"mockusername\\",\\"event_type\\":\\"my-event\\",\\"user_properties\\":{\\"repo\\":\\"all-contributors-bot\\",\\"owner\\":\\"all-contributors\\"},\\"event_properties\\":{\\"funnel_id\\":\\"mockFunnelId\\"}}]",
197
}
208
`;

0 commit comments

Comments
 (0)