insomnia/app/analytics/google.js
Gregory Schier 1d45367aa1 Added eslint and fixed all problems (#101)
* Fixed duplication kve bug

* Added semistandard and updated code

* Actually got it working

* Even better

* I think it should work on Windows now
2017-03-03 12:09:08 -08:00

96 lines
2.3 KiB
JavaScript

import * as constants from '../common/constants';
let _sessionId = null;
const DIMENSION_PLATFORM = 'dimension1';
const DIMENSION_VERSION = 'dimension2';
export function init (userId, platform, version) {
if (constants.isDevelopment()) {
console.log(`[ga] Not initializing for dev`);
return;
}
if (!_sessionId) {
_injectGoogleAnalyticsScript();
}
if (!window.localStorage['gaClientId']) {
window.localStorage['gaClientId'] = require('uuid').v4();
}
_sessionId = window.localStorage['gaClientId'];
window.ga('create', constants.GA_ID, {
'storage': 'none',
'clientId': _sessionId
});
// Disable URL protocol check
window.ga('set', 'checkProtocolTask', () => null);
// Set a fake location
window.ga('set', 'location', `https://${constants.GA_HOST}/`);
setUserId(userId);
setPlatform(platform);
setVersion(version);
// Track the initial page view
window.ga('send', 'pageview');
console.log(`[ga] Initialized for ${_sessionId}`);
}
export function setPlatform (platform) {
if (!window.ga || !platform) {
return;
}
window.ga('set', DIMENSION_PLATFORM, platform);
console.log(`[ga] Set platform ${platform}`);
}
export function setVersion (version) {
if (!window.ga || !version) {
return;
}
window.ga('set', DIMENSION_VERSION, version);
console.log(`[ga] Set version ${version}`);
}
export function setUserId (userId) {
if (!window.ga || !userId) {
return;
}
window.ga('set', 'userId', userId);
console.log(`[ga] Set userId ${userId}`);
}
export function sendEvent (...googleAnalyticsArgs) {
window.ga && window.ga('send', 'event', ...googleAnalyticsArgs);
console.log(`[ga] Send event [${googleAnalyticsArgs.join(', ')}]`);
}
function _injectGoogleAnalyticsScript () {
try {
/* eslint-disable */
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o);
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
/* eslint-enable */
} catch (e) {
console.warn('[ga] Failed to inject Google Analytics');
}
}