@sergiodxa/analytics
Version:
Use my own analytics service to track events
191 lines (158 loc) • 5.47 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _stringify = require("babel-runtime/core-js/json/stringify");
var _stringify2 = _interopRequireDefault(_stringify);
exports.track = track;
exports.event = event;
exports.warning = warning;
exports.error = error;
exports.info = info;
var _isomorphicFetch = require("isomorphic-fetch");
var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch);
var _querystring = require("querystring");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @typedef {Object} Action
* @param {String} type The action type
* @param {String} action The action name
* @param {String} [description=""] The action long description
*/
/**
* Check if the user has Do Not Track enable it
* @private
* @function doNotTrack
* @returns {Boolean}
*/
/** @module analytics */
var doNotTrack = function doNotTrack() {
return navigator.doNotTrack === "1" || // Standard
navigator.doNotTrack === "yes" || // old FireFox
navigator.msDoNotTrack === "1" || // IE 9 and 10
window.doNotTrack === "1" || // IE11, Edge and Safari
localStorage.getItem("doNoTrack") === "true";
};
/**
* Log an action to the console
* @function log
* @param {Action} action An action to log
*/
function log() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
type = _ref.type,
action = _ref.action,
_ref$description = _ref.description,
description = _ref$description === undefined ? "" : _ref$description;
var message = ["[" + type + "]", action];
if (description) message.push(description);
message = message.join(" - ");
switch (type) {
case "error":
{
console.error(message);
}
case "warning":
{
console.warn(message);
}
case "info":
{
console.info(message);
}
default:
{
console.log(message);
}
}
}
/**
* Track an action
* @function track
* @param {Action} action An action to track
* @param {Boolean} [isProd=true] Check if the function is running in production
*/
function track() {
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
type = _ref2.type,
action = _ref2.action,
_ref2$description = _ref2.description,
description = _ref2$description === undefined ? "" : _ref2$description;
var isProd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (doNotTrack()) return;
var _parse = (0, _querystring.parse)(location.search),
source = _parse.source;
log({ type: type, action: action, description: description });
// only fetch the API in production
if (isProd) {
(0, _isomorphicFetch2.default)("https://sergiodxa.com/api/analytics", {
method: "POST",
body: (0, _stringify2.default)({
type: type,
action: action,
description: description,
source: source
})
});
}
}
/**
* Track an event action
* @function event
* @param {Action} event An event to track
* @param {Boolean} [isProd] Check if the function is running in production
*/
function event() {
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref3$action = _ref3.action,
action = _ref3$action === undefined ? "" : _ref3$action,
_ref3$description = _ref3.description,
description = _ref3$description === undefined ? "" : _ref3$description;
var isProd = arguments[1];
track({ type: "event", action: action, description: description }, isProd);
}
/**
* Track a warning action
* @function warning
* @param {Action} warning A warning to track
* @param {Boolean} [isProd] Check if the function is running in production
*/
function warning() {
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref4$action = _ref4.action,
action = _ref4$action === undefined ? "" : _ref4$action,
_ref4$description = _ref4.description,
description = _ref4$description === undefined ? "" : _ref4$description;
var isProd = arguments[1];
track({ type: "warning", action: action, description: description }, isProd);
}
/**
* Track an error action
* @function error
* @param {Action} error An error to track
* @param {Boolean} [isProd] Check if the function is running in production
*/
function error() {
var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref5$action = _ref5.action,
action = _ref5$action === undefined ? "" : _ref5$action,
_ref5$description = _ref5.description,
description = _ref5$description === undefined ? "" : _ref5$description;
var isProd = arguments[1];
track({ type: "error", action: action, description: description }, isProd);
}
/**
* Track an info action
* @function info
* @param {Action} info An info to track
* @param {Boolean} [isProd] Check if the function is running in production
*/
function info() {
var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref6$action = _ref6.action,
action = _ref6$action === undefined ? "" : _ref6$action,
_ref6$description = _ref6.description,
description = _ref6$description === undefined ? "" : _ref6$description;
var isProd = arguments[1];
track({ type: "info", action: action, description: description }, isProd);
}