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/helpers/db.js b/src/helpers/db.js index 3bc2574f..eabeff86 100644 --- a/src/helpers/db.js +++ b/src/helpers/db.js @@ -55,14 +55,14 @@ export default { get: (key: DBKey, defaults: any): any => { const db = store(key) const data = db.get('data', defaults) - logger.onDB('read', key, data) + logger.onDB('read', key) return middleware('get', key, data) }, set: (key: DBKey, val: any) => { const db = store(key) val = middleware('set', key, val) - logger.onDB('write', key, val) + logger.onDB('write', key) db.set('data', val) return val }, diff --git a/src/logger.js b/src/logger.js index f06c8fed..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 = [] @@ -24,6 +25,8 @@ const logs = [] const MAX_LOG_LENGTH = 500 const MAX_LOG_JSON_THRESHOLD = 2000 +const anonymousMode = !__DEV__ + function addLog(type, ...args) { logs.push({ type, date: new Date(), args }) if (logs.length > MAX_LOG_LENGTH) { @@ -31,6 +34,11 @@ function addLog(type, ...args) { } } +function anonymizeURL(url) { + if (!anonymousMode) return url + return url.replace(/\/addresses\/[^/]+/g, '/addresses/') +} + const makeSerializableLog = (o: mixed) => { if (typeof o === 'string') return o if (typeof o === 'number') return o @@ -64,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) => { @@ -87,10 +96,10 @@ export default { addLog('cmd', type, id, spentTime, data) }, - onDB: (way: 'read' | 'write' | 'clear', name: string, obj: ?Object) => { - const msg = `📁 ${way} ${name}:` + onDB: (way: 'read' | 'write' | 'clear', name: string) => { + const msg = `📁 ${way} ${name}` if (logDb) { - console.log(msg, obj) + console.log(msg) } addLog('db', msg) }, @@ -131,7 +140,7 @@ export default { }, network: ({ method, url }: { method: string, url: string }) => { - const log = `➡📡 ${method} ${url}` + const log = `➡📡 ${method} ${anonymizeURL(url)}` if (logNetwork) { console.log(log) } @@ -149,7 +158,9 @@ export default { status: number, responseTime: number, }) => { - const log = `✔📡 HTTP ${status} ${method} ${url} – finished in ${responseTime.toFixed(0)}ms` + const log = `✔📡 HTTP ${status} ${method} ${anonymizeURL( + url, + )} – finished in ${responseTime.toFixed(0)}ms` if (logNetwork) { console.log(log) } @@ -169,9 +180,9 @@ export default { error: string, responseTime: number, }) => { - const log = `✖📡 HTTP ${status} ${method} ${url} – ${error} – failed after ${responseTime.toFixed( - 0, - )}ms` + const log = `✖📡 HTTP ${status} ${method} ${anonymizeURL( + url, + )} – ${error} – failed after ${responseTime.toFixed(0)}ms` if (logNetwork) { console.log(log) } @@ -187,13 +198,43 @@ export default { url: string, responseTime: number, }) => { - const log = `✖📡 NETWORK DOWN – ${method} ${url} – after ${responseTime.toFixed(0)}ms` + const log = `✖📡 NETWORK DOWN – ${method} ${anonymizeURL(url)} – after ${responseTime.toFixed( + 0, + )}ms` if (logNetwork) { console.log(log) } 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) => {