diff --git a/src/utils/Analytics.js b/src/utils/Analytics.js index 090f292d..f0874586 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({ user, repo, owner, apiKey, log, funnelId }) { @@ -26,27 +27,26 @@ class Analytics { }, } - const payload = { - api_key: this.apiKey, - event: [ - // TODO batch up to 10 events at a time - event, - ], - } + const events = [ + // TODO batch up to 10 events at a time + event, + ] const log = this.log + const params = new URLSearchParams() + params.append('api_key', this.apiKey) + params.append('event', JSON.stringify(events)) + const newEventPromise = nodeFetch('https://api.amplitude.com/httpapi', { - method: 'post', - body: JSON.stringify(payload), - headers: { 'Content-Type': 'application/json' }, + method: 'POST', + body: params, timeout: 5000, redirect: 'error', follow: 0, }) .then(response => { if (!response.ok) { - // TODO: error handling log.error(response) } return response diff --git a/test/utils/__snapshots__/Analytics.test.js.snap b/test/utils/__snapshots__/Analytics.test.js.snap index 8bbeb932..06b8d68e 100644 --- a/test/utils/__snapshots__/Analytics.test.js.snap +++ b/test/utils/__snapshots__/Analytics.test.js.snap @@ -3,18 +3,6 @@ exports[`Analytics Analytics 1`] = ` Object { "api_key": "mock api key", - "event": Array [ - Object { - "event_properties": Object { - "funnel_id": "mockFunnelId", - }, - "event_type": "my-event", - "user_id": "mockusername", - "user_properties": Object { - "owner": "all-contributors", - "repo": "all-contributors-bot", - }, - }, - ], + "event": "[{\\"user_id\\":\\"mockusername\\",\\"event_type\\":\\"my-event\\",\\"user_properties\\":{\\"repo\\":\\"all-contributors-bot\\",\\"owner\\":\\"all-contributors\\"},\\"event_properties\\":{\\"funnel_id\\":\\"mockFunnelId\\"}}]", } `;