From adc47d70a083257a39dbbbcfffad9dde3e20381c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Wed, 27 Jun 2018 21:57:23 +0200 Subject: [PATCH] log analytics events for better logs (we'll know better how to repro error) --- src/analytics/segment.js | 19 +++++-------------- src/logger.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/analytics/segment.js b/src/analytics/segment.js index d239db10..0d34366b 100644 --- a/src/analytics/segment.js +++ b/src/analytics/segment.js @@ -4,7 +4,6 @@ import uuid from 'uuid/v4' import logger from 'logger' import invariant from 'invariant' import user from 'helpers/user' -import { DEBUG_ANALYTICS } from 'config/constants' import { langAndRegionSelector } from 'reducers/settings' import { getSystemLocale } from 'helpers/systemLocale' import { load } from './inject-in-window' @@ -32,13 +31,14 @@ const getContext = store => { let storeInstance // is the redux store. it's also used as a flag to know if analytics is on or off. export const start = (store: *) => { + const { id } = user() + logger.analyticsStart(id) storeInstance = store const { analytics } = window if (typeof analytics === 'undefined') { logger.error('analytics is not available') return } - const { id } = user() load() analytics.identify( id, @@ -47,12 +47,10 @@ export const start = (store: *) => { context: getContext(store), }, ) - if (DEBUG_ANALYTICS) { - logger.log(`analytics: start() with user id ${id}`) - } } export const stop = () => { + logger.analyticsStop() storeInstance = null const { analytics } = window if (typeof analytics === 'undefined') { @@ -60,12 +58,10 @@ export const stop = () => { return } analytics.reset() - if (DEBUG_ANALYTICS) { - logger.log(`analytics: stop()`) - } } export const track = (event: string, properties: ?Object) => { + logger.analyticsTrack(event, properties) if (!storeInstance) { return } @@ -77,12 +73,10 @@ export const track = (event: string, properties: ?Object) => { analytics.track(event, properties, { context: getContext(storeInstance), }) - if (DEBUG_ANALYTICS) { - logger.log(`analytics: track(${event},`, properties) - } } export const page = (category: string, name: ?string, properties: ?Object) => { + logger.analyticsPage(category, name, properties) if (!storeInstance) { return } @@ -94,7 +88,4 @@ export const page = (category: string, name: ?string, properties: ?Object) => { analytics.page(category, name, properties, { context: getContext(storeInstance), }) - if (DEBUG_ANALYTICS) { - logger.log(`analytics: page(${category}, ${name || ''},`, properties) - } } diff --git a/src/logger.js b/src/logger.js index ac2374fe..552fc071 100644 --- a/src/logger.js +++ b/src/logger.js @@ -17,6 +17,7 @@ import { DEBUG_TAB_KEY, DEBUG_LIBCORE, DEBUG_WS, + DEBUG_ANALYTICS, } from 'config/constants' const logs = [] @@ -71,6 +72,7 @@ const logTabkey = !__DEV__ || DEBUG_TAB_KEY const logLibcore = !__DEV__ || DEBUG_LIBCORE const logWS = !__DEV__ || DEBUG_WS const logNetwork = !__DEV__ || DEBUG_NETWORK +const logAnalytics = !__DEV__ || DEBUG_ANALYTICS export default { onCmd: (type: string, id: string, spentTime: number, data?: any) => { @@ -205,6 +207,34 @@ export default { addLog('network-down', log) }, + analyticsStart: (id: string) => { + if (logAnalytics) { + console.log(`△ start() with user id ${id}`) + } + addLog('anaytics-start', id) + }, + + analyticsStop: () => { + if (logAnalytics) { + console.log(`△ stop()`) + } + addLog('anaytics-stop') + }, + + analyticsTrack: (event: string, properties: ?Object) => { + if (logAnalytics) { + console.log(`△ track ${event}`, properties) + } + addLog('anaytics-track', `${event}`) + }, + + analyticsPage: (category: string, name: ?string, properties: ?Object) => { + if (logAnalytics) { + console.log(`△ page ${category} ${name || ''}`, properties) + } + addLog('anaytics-page', `${category} ${name || ''}`) + }, + // General functions in case the hooks don't apply log: (...args: any) => {