From 0308ffb0509ea9f41cf1da861d0d76fd5aa49e2d Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 16 Nov 2016 10:16:55 -0800 Subject: [PATCH] Some small fixes --- app/analytics/__tests__/index.test.js | 17 ++++++++++++----- app/analytics/google.js | 14 ++++++++------ app/analytics/index.js | 6 +++--- app/analytics/segment.js | 18 +++++++----------- webpack/webpack.config.production.babel.js | 1 + 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/app/analytics/__tests__/index.test.js b/app/analytics/__tests__/index.test.js index f2f97321e6..6bb9aac0e0 100644 --- a/app/analytics/__tests__/index.test.js +++ b/app/analytics/__tests__/index.test.js @@ -1,5 +1,7 @@ import * as analytics from '../index'; import {GA_HOST} from '../../common/constants'; +import * as db from '../../common/database'; +import * as models from '../../models'; global.document = { getElementsByTagName () { @@ -14,6 +16,10 @@ global.document = { }; describe('init()', () => { + beforeEach(() => { + return db.init(models.types(), {inMemoryOnly: true}, true); + }); + it('correctly initializes', async () => { window.localStorage = {}; @@ -21,14 +27,15 @@ describe('init()', () => { analytics.setAccountId('acct_premature'); window.ga = jest.genMockFunction(); - analytics.init('acct_123'); + await analytics.init('acct_123'); // Verify that Google Analytics works expect(window.ga.mock.calls.length).toBe(5); - expect(window.ga.mock.calls[0]).toEqual(['create', 'UA-86416787-1', { - clientId: 'dd2ccc1a-2745-477a-881a-9e8ef9d42403', - storage: 'none' - }]); + expect(window.ga.mock.calls[0][0]).toBe('create'); + expect(window.ga.mock.calls[0][1]).toBe('UA-86416787-1'); + expect(window.ga.mock.calls[0][2].storage).toBe('none'); + expect(window.ga.mock.calls[0][2].clientId.length).toBe(36); + expect(window.ga.mock.calls[0].length).toBe(3); expect(window.ga.mock.calls[1].slice(0, 2)).toEqual(['set', 'checkProtocolTask']); expect(window.ga.mock.calls[1][2]()).toBeNull(); expect(window.ga.mock.calls[2]).toEqual(['set', 'location', `https://${GA_HOST}/`]); diff --git a/app/analytics/google.js b/app/analytics/google.js index 507fe335e7..519ed81bbc 100644 --- a/app/analytics/google.js +++ b/app/analytics/google.js @@ -4,7 +4,7 @@ let _sessionId = null; export function init (userId = null) { if (constants.isDevelopment()) { - console.log('-- Not initializing analytics for dev --'); + console.log(`[ga] Not initializing for dev`); return; } @@ -36,15 +36,17 @@ export function init (userId = null) { setUserId(userId); } - console.log(`-- Analytics Initialized for ${_sessionId} --`); + console.log(`[ga] Initialized for ${_sessionId}`); } -export function setUserId (accountId) { - window.ga && window.ga('set', 'userId', accountId); +export function setUserId (userId) { + window.ga && window.ga('set', 'userId', userId); + console.log(`[ga] Set userId ${userId}`); } export function sendEvent (...googleAnalyticsArgs) { window.ga && window.ga('send', 'event', ...googleAnalyticsArgs); + console.log(`[ga] Send event [${googleAnalyticsArgs.join(', ')}]`); } function _injectGoogleAnalyticsScript () { @@ -59,8 +61,8 @@ function _injectGoogleAnalyticsScript () { a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) - })(window, document, 'script', 'https://www.google-analytics.com/segment.js', 'ga'); + })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); } catch (e) { - console.warn('-- Failed to inject Google Analytics --') + console.warn('[ga] Failed to inject Google Analytics') } } diff --git a/app/analytics/index.js b/app/analytics/index.js index 936ccff065..9a7cb05eda 100644 --- a/app/analytics/index.js +++ b/app/analytics/index.js @@ -2,13 +2,13 @@ import * as segment from './segment'; import * as google from './google'; let initialized = false; -export function init (accountId) { +export async function init (accountId) { if (initialized) { return; } - segment.init(); - google.init(accountId); + await segment.init(); + await google.init(accountId); initialized = true; } diff --git a/app/analytics/segment.js b/app/analytics/segment.js index 90be706423..5ea9b4f4b7 100644 --- a/app/analytics/segment.js +++ b/app/analytics/segment.js @@ -7,19 +7,14 @@ let userId = null; export async function init () { if (isDevelopment()) { - console.log('-- Not initializing Legacy analytics in dev --'); + console.log('[segment] Not initializing for dev'); return; } analytics = new Analytics(SEGMENT_WRITE_KEY); - if (!userId) { - const stats = await models.stats.get(); - userId = stats._id; - - // Recurse now that we have a userId - return await init(); - } + const stats = await models.stats.get(); + userId = stats._id; analytics.identify({ userId, @@ -32,13 +27,12 @@ export async function init () { } }); - console.log(`-- Legacy analytics Initialized for ${userId} --`); + console.log(`[segment] Initialized for ${userId}`); } export function trackLegacyEvent (event, properties = {}) { - // Don't track events if we haven't set them up yet + if (analytics) { - // Add base properties Object.assign(properties, { appPlatform: process.platform, appVersion: getAppVersion() @@ -46,4 +40,6 @@ export function trackLegacyEvent (event, properties = {}) { analytics.track({userId, event, properties}); } + + console.log(`[segment] Track ${event} for ${userId}`); } diff --git a/webpack/webpack.config.production.babel.js b/webpack/webpack.config.production.babel.js index 3c9d51ab67..e7639aefd4 100644 --- a/webpack/webpack.config.production.babel.js +++ b/webpack/webpack.config.production.babel.js @@ -6,6 +6,7 @@ export default { devtool: 'source-map', plugins: [ ...baseConfig.plugins, + // NOTE: Uglification breaks everything! So many problems for some reason // new webpack.optimize.UglifyJsPlugin(), new webpack.DefinePlugin({ __DEV__: false,