Some small fixes

This commit is contained in:
Gregory Schier
2016-11-16 10:16:55 -08:00
parent 1cf1037c93
commit 0308ffb050
5 changed files with 31 additions and 25 deletions

View File

@@ -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}/`]);

View File

@@ -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')
}
}

View File

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

View File

@@ -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}`);
}

View File

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