aio-analytics
Version:
[Victor Tran](https://stackoverflow.com/users/11862231/victor-tran)
52 lines (51 loc) • 1.48 kB
JavaScript
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
import ReactGA from 'react-ga';
var isInit = false;
var initGA = function (gaId) {
if (gaId) {
ReactGA.initialize(gaId);
isInit = true;
}
else {
isInit = false;
}
};
var logPageView = function (url) {
if (!isInit)
return;
ReactGA.set({ page: url });
ReactGA.pageview(url);
};
// args often contain category and action, value should be an valid object
var logEvent = function (args, value) {
if (!isInit)
return;
if (args && value) {
var params = Object.values(value).reduce(function (pre, current) {
if (typeof pre === 'string' || typeof pre === 'number') {
return [pre, current];
}
return __spreadArray(__spreadArray([], pre), [current]);
});
ReactGA.event(args, params);
}
};
var logException = function (description, fatal) {
if (description === void 0) { description = ''; }
if (fatal === void 0) { fatal = false; }
if (!isInit)
return;
if (description) {
ReactGA.exception({ description: description, fatal: fatal });
}
};
export default {
initGA: initGA,
logPageView: logPageView,
logEvent: logEvent,
logException: logException
};