From c879560445886820febbfead685c66f6d8b771ee Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Fri, 24 Apr 2020 19:02:27 +0200 Subject: [PATCH] add ability to log amplitude events (#12120) --- config.json.example | 3 ++- website/server/libs/analyticsService.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/config.json.example b/config.json.example index 03a3994cb0..ced1432b2f 100644 --- a/config.json.example +++ b/config.json.example @@ -79,5 +79,6 @@ "APPLE_TEAM_ID": "", "APPLE_AUTH_CLIENT_ID": "", "APPLE_AUTH_KEY_ID": "", - "BLOCKED_IPS": "" + "BLOCKED_IPS": "", + "LOG_AMPLITUDE_EVENTS": "false" } diff --git a/website/server/libs/analyticsService.js b/website/server/libs/analyticsService.js index 0d741581ca..45de7fef6c 100644 --- a/website/server/libs/analyticsService.js +++ b/website/server/libs/analyticsService.js @@ -13,6 +13,9 @@ import logger from './logger'; const AMPLITUDE_TOKEN = nconf.get('AMPLITUDE_KEY'); const GA_TOKEN = nconf.get('GA_ID'); + +const LOG_AMPLITUDE_EVENTS = nconf.get('LOG_AMPLITUDE_EVENTS') === 'true'; + const GA_POSSIBLE_LABELS = ['gaLabel', 'itemKey']; const GA_POSSIBLE_VALUES = ['gaValue', 'gemCost', 'goldCost']; const AMPLITUDE_PROPERTIES_TO_SCRUB = [ @@ -176,6 +179,10 @@ function _sendDataToAmplitude (eventType, data) { amplitudeData.event_type = eventType; + if (LOG_AMPLITUDE_EVENTS) { + logger.info('Amplitude Event', amplitudeData); + } + return amplitude .track(amplitudeData) .catch(err => logger.error(err, 'Error while sending data to Amplitude.')); @@ -245,6 +252,10 @@ function _sendPurchaseDataToAmplitude (data) { amplitudeData.event_type = 'purchase'; amplitudeData.revenue = data.purchaseValue; + if (LOG_AMPLITUDE_EVENTS) { + logger.info('Amplitude Purchase Event', amplitudeData); + } + return amplitude .track(amplitudeData) .catch(err => logger.error(err, 'Error while sending data to Amplitude.')); @@ -325,6 +336,8 @@ async function trackPurchase (data) { } // Stub for non-prod environments +// @TODO instead of exporting a different interface why not have track +// and trackPurchase be no ops when not in production? const mockAnalyticsService = { track: () => { }, trackPurchase: () => { },