@philpl/ga-lite
Version:
Small, cacheable subset of Google Analytics JS client
242 lines (220 loc) • 5.45 kB
JavaScript
var f = {};
function g() {
return (g = 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 h(a) {
(new Image).src = a;
}
function k(a) {
if ("undefined" == typeof navigator || !navigator.sendBeacon || !navigator.sendBeacon(a)) {
try {
var b = new XMLHttpRequest;
b.timeout = 5e3;
b.ontimeout = b.onerror = h.bind(null, a);
b.open("GET", a, !0);
b.send();
} catch (c) {
fallback(a);
}
}
}
function l(a, b) {
void 0 === b && (b = []);
return !a || -1 < b.indexOf(void 0) ? "" : "&" + a + "=" + b.map(encodeURIComponent).join("");
}
function t(a) {
void 0 === a && (a = {});
return Object.keys(a).map((function(b) {
return [ b, a[b] ].map(u).map(encodeURIComponent).join("=");
})).join("&");
}
function u(a) {
return "boolean" == typeof a ? +a : a;
}
var v = {
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"
}, w = /(dimension|metric)(\d+)/, x = {
dimension: "cd",
metric: "cm"
};
function y(a) {
this.fields = {
trackingId: a
};
void 0 === (a = void 0) && (a = window ? window.localStorage : null);
if (a && a.getItem("uid")) {
a = a.getItem("uid");
} else {
var b = Math.random() + "." + Math.random();
a && a.setItem("uid", b);
a = b;
}
this.userId = a;
this._sendTo = k;
this._getTime = z;
}
y.prototype.send = function(a) {
for (var b = [], c = arguments.length - 1; 0 < c--; ) {
b[c] = arguments[c + 1];
}
if (!0 !== window["ga-disable-" + this.fields.trackingId]) {
var d = g({}, {
hitType: a
}, A(a, b), this.fields);
b = this.fields.trackingId;
c = this._getTime();
var n = this.userId, m = d;
void 0 === m && (m = {});
void 0 === (d = void 0) && (d = !1);
var p = {};
for (e in m) {
var q = m[e];
if (q) {
e in v && (p[v[e]] = q);
var r = w.exec(e);
r && (p[x[r[1]] + r[2]] = q);
}
}
var e = t(p);
e = "https://www.google-analytics.com/collect?v=1&ul=en-us&de=UTF-8" + l("dl", [ document.location.href ]) + l("dt", [ document.title ]) + l("sd", [ window.screen.colorDepth, "-bit" ]) + l("sr", [ window.screen.availWidth, "x", window.screen.availHeight ]) + l("vp", [ window.innerWidth, "x", window.innerHeight ]) + l("dr", [ document.referrer ]) + (e ? "&" + e : "") + (d ? "&aip=1" : "") + "&cid=" + n + "&tid=" + b + "&z=" + c;
this._sendTo(e);
}
};
y.prototype.get = function(a) {
return this.fields[a];
};
y.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 z() {
return (new Date).getTime();
}
function A(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 g({}, {
page: b[0]
}, d);
case "event":
return g({}, {
eventCategory: b[0],
eventAction: b[1],
eventLabel: b[2],
eventValue: b[3]
}, d);
case "social":
return g({}, {
socialNetwork: b[0],
socialAction: b[1],
socialTarget: b[2]
}, d);
case "timing":
return g({}, {
timingCategory: b[0],
timingVar: b[1],
timingValue: b[2],
timingLabel: b[3]
}, d);
default:
return d;
}
}
var B = {
create: function(a, b, c) {
void 0 === c && (c = "t0");
a = new y(a);
return f[c] = a;
},
getByName: function(a) {
return f[a];
}
};
function C(a) {
for (var b = [], c = arguments.length - 1; 0 < c--; ) {
b[c] = arguments[c + 1];
}
if (c = "undefined" == typeof navigator || "undefined" == typeof window ? !1 : 1 === parseInt(navigator.msDoNotTrack || window.doNotTrack || navigator.doNotTrack, 10)) {
return !1;
}
var d = "string" == typeof a && -1 < a.indexOf(".") ? a.split(".") : [ "t0", a ];
c = d[0];
var n = !!y.prototype[d = d[1]] && "constructor" !== d;
if (B[a]) {
B[a].apply(B, b);
} else if (n) {
(c = f[c]) && c[d].apply(c, b);
} else if ("function" == typeof a) {
a(f[c]);
} else {
return !1;
}
return !0;
}
Object.keys(B).forEach((function(a) {
C[a] = B[a];
}));
export default C;
//# sourceMappingURL=ga-lite.es.js.map