insomnia/app/analytics/segment.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

import Analytics from 'analytics-node';
import {SEGMENT_WRITE_KEY, getAppVersion, isDevelopment} from '../common/constants';
import * as models from '../backend/models';
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;
}
analytics = new Analytics(SEGMENT_WRITE_KEY);
if (!userId) {
2016-11-10 01:15:27 +00:00
const stats = await models.stats.get();
userId = stats._id;
// Recurse now that we have a userId
2016-11-10 01:34:03 +00:00
return await init();
}
analytics.identify({
userId,
traits: {
appPlatform: process.platform,
appVersion: getAppVersion(),
// Reserved Traits
createdAt: new Date()
}
});
2016-10-27 03:43:51 +00:00
console.log(`-- Legacy analytics Initialized for ${userId} --`);
}
2016-10-27 03:41:30 +00:00
export function trackLegacyEvent (event, properties = {}) {
// 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()
});
analytics.track({userId, event, properties});
}
}