fix(analytics): always use mock service in development

This commit is contained in:
Matteo Pagliazzi
2020-11-29 23:15:26 +01:00
parent 6b3a6eb59f
commit 7745a0e65a
5 changed files with 23 additions and 19 deletions

View File

@@ -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) {

View File

@@ -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,
};

View File

@@ -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,

View File

@@ -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 = [];

View File

@@ -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;