2016-10-02 20:57:00 +00:00
|
|
|
import Analytics from 'analytics-node';
|
|
|
|
import {getAppVersion} from './appInfo';
|
|
|
|
import * as db from './database';
|
|
|
|
import {SEGMENT_WRITE_KEY} from './constants';
|
2016-07-25 22:27:29 +00:00
|
|
|
|
|
|
|
let analytics = null;
|
|
|
|
let userId = null;
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export async function initAnalytics () {
|
|
|
|
analytics = new Analytics(SEGMENT_WRITE_KEY);
|
2016-07-25 22:27:29 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
if (!userId) {
|
|
|
|
const stats = await db.stats.get();
|
|
|
|
userId = stats._id;
|
2016-07-25 22:27:29 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Recurse now that we have a userId
|
|
|
|
return await initAnalytics();
|
|
|
|
}
|
2016-07-25 22:27:29 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
analytics.identify({
|
|
|
|
userId,
|
|
|
|
traits: {
|
|
|
|
appPlatform: process.platform,
|
|
|
|
appVersion: getAppVersion(),
|
2016-07-25 22:27:29 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Reserved Traits
|
|
|
|
createdAt: new Date()
|
|
|
|
}
|
2016-07-25 22:27:29 +00:00
|
|
|
});
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
console.log(`-- Analytics Initialized for ${userId} --`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function trackEvent (event, properties = {}) {
|
2016-07-25 22:27:29 +00:00
|
|
|
// Don't track events if we haven't set them up yet
|
|
|
|
if (analytics) {
|
2016-09-13 18:46:51 +00:00
|
|
|
// Add base properties
|
|
|
|
Object.assign(properties, {
|
|
|
|
appPlatform: process.platform,
|
|
|
|
appVersion: getAppVersion()
|
|
|
|
});
|
|
|
|
|
2016-07-25 22:27:29 +00:00
|
|
|
analytics.track({userId, event, properties});
|
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|