UNPKG

@philpl/ga-lite

Version:

Small, cacheable subset of Google Analytics JS client

262 lines (235 loc) 6.47 kB
"use strict"; function doNotTrackEnabled() { return "undefined" == typeof navigator || "undefined" == typeof window ? !1 : 1 === parseInt(navigator.msDoNotTrack || window.doNotTrack || navigator.doNotTrack, 10); } var store = {}; function getTracker(a) { return store[a]; } function _extends() { return (_extends = Object.assign || function(a) { for (var b = 1; b < arguments.length; b++) { var d, c = arguments[b]; for (d in c) { Object.prototype.hasOwnProperty.call(c, d) && (a[d] = c[d]); } } return a; }).apply(this, arguments); } function fallbackSendTo(a) { (new Image).src = a; } function sendTo(a) { if ("undefined" == typeof navigator || !navigator.sendBeacon || !navigator.sendBeacon(a)) { try { var b = new XMLHttpRequest; b.timeout = 5e3; b.ontimeout = b.onerror = fallbackSendTo.bind(null, a); b.open("GET", a, !0); b.send(); } catch (c) { fallback(a); } } } function getOptionalStr(a, b) { void 0 === b && (b = []); return !a || -1 < b.indexOf(void 0) ? "" : "&" + a + "=" + b.map(encodeURIComponent).join(""); } function booleansToNumbers(a) { return "boolean" == typeof a ? +a : a; } var FIELDS_TO_PARAMS_MAP = { anonymizeIp: "aip", dataSource: "ds", queueTime: "qt", userId: "uid", sessionControl: "sc", referrer: "dr", campaignName: "cn", campaignSource: "cs", campaignMedium: "cm", campaignKeyword: "ck", campaignContent: "cc", campaignId: "ci", screenResolution: "sr", viewportSize: "vp", encoding: "de", screenColors: "sd", language: "ul", javaEnabled: "je", flashVersion: "fl", hitType: "t", nonInteraction: "ni", location: "dl", hostname: "dh", page: "dp", title: "dt", screenName: "cd", linkid: "linkid", appName: "an", appId: "aid", appVersion: "av", appInstallerId: "aiid", eventCategory: "ec", eventAction: "ea", eventLabel: "el", eventValue: "ev", currencyCode: "cu", socialNetwork: "sn", socialAction: "sa", socialTarget: "st", timingCategory: "utc", timingVar: "utv", timingValue: "utt", timingLabel: "utl", exDescription: "exd", exFatal: "exf", expId: "xid", expVar: "xvar" }, CUSTOM_VALUES_RE = /(dimension|metric)(\d+)/, CUSTOM_VALUES_TO_PARAMS_MAP = { dimension: "cd", metric: "cm" }; function buildEventUrl(a, b, c, d, e) { void 0 === d && (d = {}); void 0 === e && (e = !1); d = function objectToQueryString(a) { void 0 === a && (a = {}); return Object.keys(a).map((function(b) { return [ b, a[b] ].map(booleansToNumbers).map(encodeURIComponent).join("="); })).join("&"); }(function fieldsToParams(a) { var c, b = {}; for (c in a) { var d = a[c]; if (d) { c in FIELDS_TO_PARAMS_MAP && (b[FIELDS_TO_PARAMS_MAP[c]] = d); var e = CUSTOM_VALUES_RE.exec(c); e && (b[CUSTOM_VALUES_TO_PARAMS_MAP[e[1]] + e[2]] = d); } } return b; }(d)); return function getBaseUrl(a, b) { return "https://www.google-analytics.com/collect?v=1&ul=en-us&de=UTF-8" + getOptionalStr("dl", [ document.location.href ]) + getOptionalStr("dt", [ document.title ]) + getOptionalStr("sd", [ window.screen.colorDepth, "-bit" ]) + getOptionalStr("sr", [ window.screen.availWidth, "x", window.screen.availHeight ]) + getOptionalStr("vp", [ window.innerWidth, "x", window.innerHeight ]) + getOptionalStr("dr", [ document.referrer ]); }() + (d ? "&" + d : "") + (e ? "&aip=1" : "") + "&cid=" + c + "&tid=" + a + "&z=" + b; } function userOptedOut(a) { return !0 === window["ga-disable-" + a]; } var Tracker = function(a) { this.fields = { trackingId: a }; this.userId = function getUserId(a) { void 0 === a && (a = window ? window.localStorage : null); if (a && a.getItem("uid")) { return a.getItem("uid"); } var b = Math.random() + "." + Math.random(); a && a.setItem("uid", b); return b; }(); this._sendTo = sendTo; this._getTime = getTime; }; Tracker.prototype.send = function(a) { for (var b = [], c = arguments.length - 1; 0 < c--; ) { b[c] = arguments[c + 1]; } userOptedOut(this.fields.trackingId) || (b = _extends({}, { hitType: a }, argumentsToFields(a, b), this.fields), b = buildEventUrl(this.fields.trackingId, this._getTime(), this.userId, b), this._sendTo(b)); }; Tracker.prototype.get = function(a) { return this.fields[a]; }; Tracker.prototype.set = function(a, b) { if (a.constructor === Object) { for (var c in a) { this.fields[c] = a[c]; } } else { this.fields[a] = b; } }; function getTime() { return (new Date).getTime(); } function argumentsToFields(a, b) { void 0 === b && (b = []); var c = 1 <= b.length && b[b.length - 1].constructor === Object, d = c ? b[b.length - 1] : {}; b = c ? b.slice(0, -1) : b; switch (a) { case "pageview": return _extends({}, { page: b[0] }, d); case "event": return _extends({}, { eventCategory: b[0], eventAction: b[1], eventLabel: b[2], eventValue: b[3] }, d); case "social": return _extends({}, { socialNetwork: b[0], socialAction: b[1], socialTarget: b[2] }, d); case "timing": return _extends({}, { timingCategory: b[0], timingVar: b[1], timingValue: b[2], timingLabel: b[3] }, d); default: return d; } } var galiteCommands = { create: function create(a, b, c, d) { void 0 === c && (c = "t0"); !function addTracker(a, b) { store[a] = b; }(c, a = new Tracker(a)); return a; }, getByName: function getByName(a) { return getTracker(a); } }; function galite(a) { for (var b = [], c = arguments.length - 1; 0 < c--; ) { b[c] = arguments[c + 1]; } if (doNotTrackEnabled()) { return !1; } var d = splitTrackerCommand(a); c = d[0]; var e = !!Tracker.prototype[d = d[1]] && "constructor" !== d; if (galiteCommands[a]) { galiteCommands[a].apply(galiteCommands, b); } else if (e) { (c = getTracker(c)) && c[d].apply(c, b); } else if ("function" == typeof a) { a(b = getTracker(c)); } else { return !1; } return !0; } function splitTrackerCommand(a) { return "string" == typeof a && -1 < a.indexOf(".") ? a.split(".") : [ "t0", a ]; } Object.keys(galiteCommands).forEach((function(a) { galite[a] = galiteCommands[a]; })); exports.default = galite; //# sourceMappingURL=ga-lite.js.map