2016-11-10 01:34:03 +00:00
|
|
|
import * as google from './google';
|
2017-06-01 13:35:29 +00:00
|
|
|
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) {
|
2017-06-01 13:35:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-21 23:37:48 +00:00
|
|
|
await google.init(accountId, getAppPlatform(), getAppVersion());
|
2016-11-10 19:08:45 +00:00
|
|
|
|
2016-12-21 23:37:48 +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');
|
2017-07-20 01:55:40 +00:00
|
|
|
console.error('Uncaught Error', e);
|
2017-07-17 22:37:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('unhandledrejection', e => {
|
|
|
|
trackEvent('Error', 'Uncaught Promise');
|
2017-07-20 01:55:40 +00:00
|
|
|
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
|
|
|
}
|