insomnia/app/analytics/index.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-11-10 01:34:03 +00:00
import * as google from './google';
import * as models from '../models';
2016-11-23 19:49:10 +00:00
import {ipcRenderer} from 'electron';
2016-12-05 22:42:40 +00:00
import {getAppVersion, getAppPlatform} from '../common/constants';
2016-11-10 01:34:03 +00:00
2016-11-10 19:08:45 +00:00
let initialized = false;
2016-11-16 18:16:55 +00:00
export async function init (accountId) {
const settings = await models.settings.getOrCreate();
if (settings.disableAnalyticsTracking) {
console.log(`[ga] Not initializing due to user settings`);
return;
}
2016-11-10 19:08:45 +00:00
if (initialized) {
return;
}
await google.init(accountId, getAppPlatform(), getAppVersion());
2016-11-10 19:08:45 +00:00
initialized = true;
2016-11-23 19:49:10 +00:00
ipcRenderer.on('analytics-track-event', (_, args) => {
trackEvent(...args);
});
2017-07-17 22:37:24 +00:00
if (window) {
window.addEventListener('error', e => {
trackEvent('Error', 'Uncaught Error');
console.error('Uncaught Error', e);
2017-07-17 22:37:24 +00:00
});
window.addEventListener('unhandledrejection', e => {
trackEvent('Error', 'Uncaught Promise');
console.error('Unhandled Promise', e);
2017-07-17 22:37:24 +00:00
});
}
2016-11-10 01:34:03 +00:00
}
export function trackEvent (...args) {
2016-12-08 17:20:10 +00:00
// Do on next tick in case it fails or blocks
process.nextTick(() => {
2016-11-17 21:22:19 +00:00
google.sendEvent(...args);
2016-12-08 17:20:10 +00:00
});
2016-11-10 01:34:03 +00:00
}
export function setAccountId (accountId) {
2016-12-08 17:20:10 +00:00
// Do on next tick in case it fails or blocks
process.nextTick(() => {
2016-11-17 21:22:19 +00:00
google.setUserId(accountId);
2016-12-08 17:20:10 +00:00
});
2016-11-10 01:34:03 +00:00
}