insomnia/app/backend/analytics.js
Gregory Schier 318c35c2cb Move a bunch of stuff to async/await (#39)
* Some minor implementations

* Some more

* Lots more

* Removed 'backend' alias

* removed all promises

* Removed a bunch of module exports stuff

* Some morE'

* Fix

* custom DNS

* Tests for DNS

* bug fix

* Some small adjustments

* Small stuff
2016-10-02 13:57:00 -07:00

46 lines
1.0 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 async function initAnalytics () {
analytics = new Analytics(SEGMENT_WRITE_KEY);
if (!userId) {
const stats = await db.stats.get();
userId = stats._id;
// Recurse now that we have a userId
return await initAnalytics();
}
analytics.identify({
userId,
traits: {
appPlatform: process.platform,
appVersion: getAppVersion(),
// Reserved Traits
createdAt: new Date()
}
});
console.log(`-- Analytics Initialized for ${userId} --`);
}
export function trackEvent (event, properties = {}) {
// Don't track events if we haven't set them up yet
if (analytics) {
// Add base properties
Object.assign(properties, {
appPlatform: process.platform,
appVersion: getAppVersion()
});
analytics.track({userId, event, properties});
}
}