From f2cba1e98b74832a5108346bdbbbe1790f2bf66f Mon Sep 17 00:00:00 2001 From: Jake Bolam Date: Sun, 27 Jan 2019 12:15:00 -0500 Subject: [PATCH] fix: use httpapi --- src/utils/Analytics.js | 26 +++++++++---------- test/utils/Analytics.test.js | 2 +- .../__snapshots__/Analytics.test.js.snap | 7 ++++- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/utils/Analytics.js b/src/utils/Analytics.js index b1b3afb8..4c55fef3 100644 --- a/src/utils/Analytics.js +++ b/src/utils/Analytics.js @@ -1,5 +1,6 @@ const uuid = require('uuid') const nodeFetch = require('node-fetch') +const { URLSearchParams } = require('url') class Analytics { constructor({ @@ -41,22 +42,19 @@ 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, @@ -64,7 +62,7 @@ class Analytics { .then(response => { if (!response.ok) { log.error(response) - log.error(response.json()) + log.error(response.text()) } return response }) diff --git a/test/utils/Analytics.test.js b/test/utils/Analytics.test.js index 234912d6..9042cdb6 100644 --- a/test/utils/Analytics.test.js +++ b/test/utils/Analytics.test.js @@ -17,7 +17,7 @@ describe('Analytics', () => { }) nock('https://api.amplitude.com') - .post(`/batch`, body => { + .post(`/httpapi`, body => { expect(body).toMatchSnapshot() return true }) diff --git a/test/utils/__snapshots__/Analytics.test.js.snap b/test/utils/__snapshots__/Analytics.test.js.snap index e4204495..06b8d68e 100644 --- a/test/utils/__snapshots__/Analytics.test.js.snap +++ b/test/utils/__snapshots__/Analytics.test.js.snap @@ -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\\"}}]", +} +`;