insomnia/app/analytics/segment.js

46 lines
1000 B
JavaScript
Raw Normal View History

import Analytics from 'analytics-node';
import {SEGMENT_WRITE_KEY, getAppVersion, isDevelopment} from '../common/constants';
2016-11-10 05:56:23 +00:00
import * as models from '../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()) {
2016-11-16 18:16:55 +00:00
console.log('[segment] Not initializing for dev');
2016-10-29 22:15:13 +00:00
return;
}
analytics = new Analytics(SEGMENT_WRITE_KEY);
2016-11-16 18:16:55 +00:00
const stats = await models.stats.get();
userId = stats._id;
analytics.identify({
userId,
traits: {
appPlatform: process.platform,
appVersion: getAppVersion(),
// Reserved Traits
createdAt: new Date()
}
});
2016-11-16 18:16:55 +00:00
console.log(`[segment] Initialized for ${userId}`);
}
2016-10-27 03:41:30 +00:00
export function trackLegacyEvent (event, properties = {}) {
2016-11-16 18:16:55 +00:00
if (analytics) {
2016-09-13 18:46:51 +00:00
Object.assign(properties, {
appPlatform: process.platform,
appVersion: getAppVersion()
});
analytics.track({userId, event, properties});
}
2016-11-16 18:16:55 +00:00
console.log(`[segment] Track ${event} for ${userId}`);
}