diff --git a/website/server/controllers/api-v3/quests.js b/website/server/controllers/api-v3/quests.js index b0e2c08e6c..bcf62a0969 100644 --- a/website/server/controllers/api-v3/quests.js +++ b/website/server/controllers/api-v3/quests.js @@ -1,6 +1,6 @@ import _ from 'lodash'; import { authWithHeaders } from '../../middlewares/auth'; -import * as analytics from '../../libs/analyticsService'; +import { getAnalyticsServiceByEnvironment } from '../../libs/analyticsService'; import { model as Group, basicFields as basicGroupFields, @@ -20,6 +20,8 @@ import { sendNotification as sendPushNotification } from '../../libs/pushNotific import apiError from '../../libs/apiError'; import { questActivityWebhook } from '../../libs/webhook'; +const analytics = getAnalyticsServiceByEnvironment(); + const questScrolls = common.content.quests; function canStartQuestAutomatically (group) { diff --git a/website/server/libs/analyticsService.js b/website/server/libs/analyticsService.js index f990b60a9d..4287abaae1 100644 --- a/website/server/libs/analyticsService.js +++ b/website/server/libs/analyticsService.js @@ -338,15 +338,26 @@ 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: () => { }, }; +// Return the production or mock service based on the current environment +function getServiceByEnvironment () { + if (nconf.get('IS_PROD')) { + return { + track, + trackPurchase, + }; + } + + return mockAnalyticsService; +} + export { track, trackPurchase, mockAnalyticsService, + getServiceByEnvironment as getAnalyticsServiceByEnvironment, }; diff --git a/website/server/libs/payments/gems.js b/website/server/libs/payments/gems.js index c6aae19648..ba0d14b295 100644 --- a/website/server/libs/payments/gems.js +++ b/website/server/libs/payments/gems.js @@ -1,4 +1,4 @@ -import * as analytics from '../analyticsService'; +import { getAnalyticsServiceByEnvironment } from '../analyticsService'; import { getCurrentEvent } from '../worldState'; // eslint-disable-line import/no-cycle import { // eslint-disable-line import/no-cycle getUserInfo, @@ -11,6 +11,8 @@ import { } from '../errors'; import apiError from '../apiError'; +const analytics = getAnalyticsServiceByEnvironment(); + function getGiftMessage (data, byUsername, gemAmount, language) { const senderMsg = shared.i18n.t('giftedGemsFull', { username: data.gift.member.profile.name, diff --git a/website/server/libs/payments/subscriptions.js b/website/server/libs/payments/subscriptions.js index 8e62878e71..aef10bb191 100644 --- a/website/server/libs/payments/subscriptions.js +++ b/website/server/libs/payments/subscriptions.js @@ -1,7 +1,7 @@ import _ from 'lodash'; import moment from 'moment'; -import * as analytics from '../analyticsService'; +import { getAnalyticsServiceByEnvironment } from '../analyticsService'; import * as slack from '../slack'; // eslint-disable-line import/no-cycle import { // eslint-disable-line import/no-cycle getUserInfo, @@ -21,6 +21,7 @@ import calculateSubscriptionTerminationDate from './calculateSubscriptionTermina // @TODO: Abstract to shared/constant const JOINED_GROUP_PLAN = 'joined group plan'; +const analytics = getAnalyticsServiceByEnvironment(); function _findMysteryItems (user, dateMoment) { const pushedItems = []; diff --git a/website/server/middlewares/analytics.js b/website/server/middlewares/analytics.js index f493f0ac3d..7868b0c3e8 100644 --- a/website/server/middlewares/analytics.js +++ b/website/server/middlewares/analytics.js @@ -1,20 +1,8 @@ -import nconf from 'nconf'; import { - track, - trackPurchase, - mockAnalyticsService, + getAnalyticsServiceByEnvironment, } from '../libs/analyticsService'; -let service; - -if (nconf.get('IS_PROD')) { - service = { - track, - trackPurchase, - }; -} else { - service = mockAnalyticsService; -} +const service = getAnalyticsServiceByEnvironment(); export default function attachAnalytics (req, res, next) { res.analytics = service;