mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 06:39:48 +00:00
d369f85a12
* Segment integratrion + stats object * Some things
50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
import Analytics from 'analytics-node';
|
|
import {getAppVersion} from './appInfo';
|
|
import * as db from '../database';
|
|
import {SEGMENT_WRITE_KEY} from './constants';
|
|
|
|
let analytics = null;
|
|
let userId = null;
|
|
|
|
export function initAnalytics () {
|
|
return new Promise((resolve, reject) => {
|
|
analytics = new Analytics(SEGMENT_WRITE_KEY);
|
|
|
|
if (!userId) {
|
|
return db.statsGet().then(stats => {
|
|
userId = stats._id;
|
|
|
|
// Recurse now that we have a userId
|
|
return initAnalytics();
|
|
}).then(resolve, reject);
|
|
}
|
|
|
|
analytics.identify({
|
|
userId,
|
|
traits: {
|
|
appPlatform: process.platform,
|
|
appVersion: getAppVersion(),
|
|
|
|
// Reserved Traits
|
|
createdAt: new Date()
|
|
}
|
|
});
|
|
|
|
console.log(`-- Analytics Initialized for ${userId} --`);
|
|
resolve();
|
|
});
|
|
}
|
|
|
|
export function trackEvent (event, properties = {}) {
|
|
// add some extra properties
|
|
properties = Object.assign({
|
|
appPlatform: process.platform,
|
|
appVersion: getAppVersion()
|
|
}, properties);
|
|
|
|
// Don't track events if we haven't set them up yet
|
|
if (analytics) {
|
|
analytics.track({userId, event, properties});
|
|
}
|
|
}
|