Skip to content

Commit

Permalink
fix: use httpapi
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolam committed Jan 27, 2019
1 parent 29d593c commit f2cba1e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
26 changes: 12 additions & 14 deletions src/utils/Analytics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const uuid = require('uuid')
const nodeFetch = require('node-fetch')
const { URLSearchParams } = require('url')

class Analytics {
constructor({
Expand Down Expand Up @@ -41,30 +42,27 @@ class Analytics {
},
}

const inputBody = {
api_key: this.apiKey,
events: [
// TODO batch up to 10 events at a time
event,
],
}
const events = [
// TODO batch up to 10 events at a time
event,
]

const params = new URLSearchParams()
params.append('api_key', this.apiKey)
params.append('event', JSON.stringify(events))

const log = this.log
const newEventPromise = nodeFetch('https://api.amplitude.com/batch', {
const newEventPromise = nodeFetch('https://api.amplitude.com/httpapi', {
method: 'POST',
body: inputBody,
headers: {
'Content-Type': 'application/json',
Accept: '*/*',
},
body: params,
timeout: 5000,
redirect: 'error',
follow: 0,
})
.then(response => {
if (!response.ok) {
log.error(response)
log.error(response.json())
log.error(response.text())
}
return response
})
Expand Down
2 changes: 1 addition & 1 deletion test/utils/Analytics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Analytics', () => {
})

nock('https://api.amplitude.com')
.post(`/batch`, body => {
.post(`/httpapi`, body => {
expect(body).toMatchSnapshot()
return true
})
Expand Down
7 changes: 6 additions & 1 deletion test/utils/__snapshots__/Analytics.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

0 comments on commit f2cba1e

Please # to comment.