2016-10-02 20:57:00 +00:00
|
|
|
import Analytics from 'analytics-node';
|
2016-11-10 05:47:20 +00:00
|
|
|
import {SEGMENT_WRITE_KEY, getAppVersion, isDevelopment} from '../common/constants';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../models';
|
2016-07-25 22:27:29 +00:00
|
|
|
|
|
|
|
let analytics = null;
|
|
|
|
let userId = null;
|
|
|
|
|
2016-11-10 01:34:03 +00:00
|
|
|
export async function init () {
|
2016-10-29 22:15:13 +00:00
|
|
|
if (isDevelopment()) {
|
|
|
|
console.log('-- Not initializing Legacy analytics in dev --');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
analytics = new Analytics(SEGMENT_WRITE_KEY);
|
2016-07-25 22:27:29 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
if (!userId) {
|
2016-11-10 01:15:27 +00:00
|
|
|
const stats = await models.stats.get();
|
2016-10-02 20:57:00 +00:00
|
|
|
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
|
2016-11-10 01:34:03 +00:00
|
|
|
return await init();
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
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-27 03:43:51 +00:00
|
|
|
console.log(`-- Legacy analytics Initialized for ${userId} --`);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 03:41:30 +00:00
|
|
|
export function trackLegacyEvent (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
|
|
|
}
|