diff --git a/dist/esnext/Lib.d.ts b/dist/esnext/Lib.d.ts index 1ff4251..006c161 100644 --- a/dist/esnext/Lib.d.ts +++ b/dist/esnext/Lib.d.ts @@ -140,7 +140,7 @@ export declare class Lib { captureError(event: ErrorEvent): void; trackErrors(options?: ErrorOptions): ErrorActions; submitError(payload: IErrorEventPayload, evokeCallback?: boolean): void; - track(event: TrackEventOptions): void; + track(event: TrackEventOptions): Promise; trackPageViews(options?: PageViewsOptions): PageActions; getPerformanceStats(): IPerfPayload | {}; private heartbeat; diff --git a/dist/esnext/Lib.js b/dist/esnext/Lib.js index c4993bb..b11ba1b 100644 --- a/dist/esnext/Lib.js +++ b/dist/esnext/Lib.js @@ -75,7 +75,7 @@ export class Lib { Object.assign(errorPayload, privateData); this.sendRequest('error', errorPayload); } - track(event) { + async track(event) { if (!this.canTrack()) { return; } @@ -90,7 +90,7 @@ export class Lib { me: getUTMMedium(), ca: getUTMCampaign(), }; - this.sendRequest('custom', data); + await this.sendRequest('custom', data); } trackPageViews(options) { if (!this.canTrack()) { @@ -244,12 +244,15 @@ export class Lib { } return true; } - sendRequest(path, body) { + async sendRequest(path, body) { const host = this.options?.apiURL || DEFAULT_API_HOST; - const req = new XMLHttpRequest(); - req.open('POST', `${host}/${path}`, true); - req.setRequestHeader('Content-Type', 'application/json'); - req.send(JSON.stringify(body)); + await fetch(`${host}/${path}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + }); } } //# sourceMappingURL=Lib.js.map \ No newline at end of file diff --git a/dist/esnext/Lib.js.map b/dist/esnext/Lib.js.map index 16f170c..151ac6a 100644 --- a/dist/esnext/Lib.js.map +++ b/dist/esnext/Lib.js.map @@ -1 +1 @@ -{"version":3,"file":"Lib.js","sourceRoot":"","sources":["../../src/Lib.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,WAAW,EACX,WAAW,EACX,SAAS,EACT,WAAW,EACX,WAAW,EACX,cAAc,EACd,YAAY,EACZ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,OAAO,EAAE,QAAQ;YACtB,CAAC,WAAW,EAAE;YACd,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,IAAI,MAAM,CAAC,SAAS,EAAE,UAAU,KAAK,GAAG,CAAC;YAClE,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,CAAC;YACzC,WAAW,EAAE,EACb,CAAC;YACD,OAAO,KAAK,CAAA;QACd,CAAC;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAEO,KAAK,CAAC,WAAW,CAAC,IAAY,EAAE,IAAY;QAClD,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,MAAM,IAAI,gBAAgB,CAAA;QACrD,MAAM,KAAK,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,EAAE;YAC7B,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAA;IACJ,CAAC;CACF"} \ No newline at end of file diff --git a/dist/esnext/index.d.ts b/dist/esnext/index.d.ts index b6d1cfc..c963bb5 100644 --- a/dist/esnext/index.d.ts +++ b/dist/esnext/index.d.ts @@ -15,7 +15,7 @@ export declare function init(pid: string, options?: LibOptions): Lib; * * @param {TrackEventOptions} event The options related to the custom event. */ -export declare function track(event: TrackEventOptions): void; +export declare function track(event: TrackEventOptions): Promise; /** * With this function you are able to automatically track pageviews across your application. * diff --git a/dist/esnext/index.js b/dist/esnext/index.js index e513ab3..f2c68a0 100644 --- a/dist/esnext/index.js +++ b/dist/esnext/index.js @@ -20,10 +20,10 @@ export function init(pid, options) { * * @param {TrackEventOptions} event The options related to the custom event. */ -export function track(event) { +export async function track(event) { if (!LIB_INSTANCE) return; - LIB_INSTANCE.track(event); + await LIB_INSTANCE.track(event); } /** * With this function you are able to automatically track pageviews across your application. diff --git a/dist/esnext/index.js.map b/dist/esnext/index.js.map index e3c4735..b392568 100644 --- a/dist/esnext/index.js.map +++ b/dist/esnext/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAOH,cAAc,GAGf,MAAM,OAAO,CAAA;AAEd,MAAM,CAAC,IAAI,YAAY,GAAe,IAAI,CAAA;AAE1C;;;;;;GAMG;AACH,MAAM,UAAU,IAAI,CAAC,GAAW,EAAE,OAAoB;IACpD,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;IACtC,CAAC;IAED,OAAO,YAAY,CAAA;AACrB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAC,KAAwB;IAC5C,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC3B,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,OAA0B;IACnD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,OAAO,CAAC,cAAc,CAAC,CAAA;YACvB,OAAM;QACR,CAAC;QAED,8GAA8G;QAC9G,IAAI,OAAO,QAAQ,KAAK,WAAW,IAAI,QAAQ,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;YAC1E,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAA;QAC/C,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;gBACnC,aAAa;gBACb,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAA;YAC/C,CAAC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UA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AAE,EAAE,EAAE,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CAAA;AAC9E,CAAC;AAOD,MAAM,UAAU,QAAQ,CAAC,OAAyB;IAChD,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CAAA;AAC3E,CAAC"} \ No newline at end of file diff --git a/dist/swetrix.cjs.js b/dist/swetrix.cjs.js index a7c6307..af63816 100644 --- a/dist/swetrix.cjs.js +++ b/dist/swetrix.cjs.js @@ -28,6 +28,44 @@ var __assign = function() { return __assign.apply(this, arguments); }; +function __awaiter(thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +} + +function __generator(thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (g && (g = 0, op[0] && (_ = 0)), _) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +} + typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; @@ -170,11 +208,22 @@ var Lib = /** @class */ (function () { this.sendRequest('error', errorPayload); }; Lib.prototype.track = function (event) { - if (!this.canTrack()) { - return; - } - var data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }); - this.sendRequest('custom', data); + return __awaiter(this, void 0, void 0, function () { + var data; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!this.canTrack()) { + return [2 /*return*/]; + } + data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }); + return [4 /*yield*/, this.sendRequest('custom', data)]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); + }); }; Lib.prototype.trackPageViews = function (options) { if (!this.canTrack()) { @@ -326,12 +375,26 @@ var Lib = /** @class */ (function () { return true; }; Lib.prototype.sendRequest = function (path, body) { - var _a; - var host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST; - var req = new XMLHttpRequest(); - req.open('POST', "".concat(host, "/").concat(path), true); - req.setRequestHeader('Content-Type', 'application/json'); - req.send(JSON.stringify(body)); + return __awaiter(this, void 0, void 0, function () { + var host; + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST; + return [4 /*yield*/, fetch("".concat(host, "/").concat(path), { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + })]; + case 1: + _b.sent(); + return [2 /*return*/]; + } + }); + }); }; return Lib; }()); @@ -358,9 +421,19 @@ function init(pid, options) { * @param {TrackEventOptions} event The options related to the custom event. */ function track(event) { - if (!exports.LIB_INSTANCE) - return; - exports.LIB_INSTANCE.track(event); + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!exports.LIB_INSTANCE) + return [2 /*return*/]; + return [4 /*yield*/, exports.LIB_INSTANCE.track(event)]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); + }); } /** * With this function you are able to automatically track pageviews across your application. diff --git a/dist/swetrix.es5.js b/dist/swetrix.es5.js index 7e8c4ca..d47b45a 100644 --- a/dist/swetrix.es5.js +++ b/dist/swetrix.es5.js @@ -24,6 +24,44 @@ var __assign = function() { return __assign.apply(this, arguments); }; +function __awaiter(thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +} + +function __generator(thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (g && (g = 0, op[0] && (_ = 0)), _) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +} + typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; @@ -166,11 +204,22 @@ var Lib = /** @class */ (function () { this.sendRequest('error', errorPayload); }; Lib.prototype.track = function (event) { - if (!this.canTrack()) { - return; - } - var data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }); - this.sendRequest('custom', data); + return __awaiter(this, void 0, void 0, function () { + var data; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!this.canTrack()) { + return [2 /*return*/]; + } + data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }); + return [4 /*yield*/, this.sendRequest('custom', data)]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); + }); }; Lib.prototype.trackPageViews = function (options) { if (!this.canTrack()) { @@ -322,12 +371,26 @@ var Lib = /** @class */ (function () { return true; }; Lib.prototype.sendRequest = function (path, body) { - var _a; - var host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST; - var req = new XMLHttpRequest(); - req.open('POST', "".concat(host, "/").concat(path), true); - req.setRequestHeader('Content-Type', 'application/json'); - req.send(JSON.stringify(body)); + return __awaiter(this, void 0, void 0, function () { + var host; + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST; + return [4 /*yield*/, fetch("".concat(host, "/").concat(path), { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + })]; + case 1: + _b.sent(); + return [2 /*return*/]; + } + }); + }); }; return Lib; }()); @@ -354,9 +417,19 @@ function init(pid, options) { * @param {TrackEventOptions} event The options related to the custom event. */ function track(event) { - if (!LIB_INSTANCE) - return; - LIB_INSTANCE.track(event); + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!LIB_INSTANCE) + return [2 /*return*/]; + return [4 /*yield*/, LIB_INSTANCE.track(event)]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); + }); } /** * With this function you are able to automatically track pageviews across your application. diff --git a/dist/swetrix.js b/dist/swetrix.js index 612d4cb..fff24a2 100644 --- a/dist/swetrix.js +++ b/dist/swetrix.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).swetrix={})}(this,function(o){"use strict";function t(t){return(t=location.search.match(t))&&t[2]||void 0}function r(){return void 0!==navigator.languages?navigator.languages[0]:navigator.language}function a(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){}}function s(){return document.referrer||void 0}function c(){return t(e)}function u(){return t(p)}function l(){return t(n)}function i(t){var e,n=location.pathname||"";return t.hash&&(n+=-1<(e=location.hash.indexOf("?"))?location.hash.substring(0,e):location.hash),t.search&&(e=location.hash.indexOf("?"),n+=location.search||(-1Math.random()||this.submitError({filename:t.filename,lineno:t.lineno,colno:t.colno,name:(null==(e=t.error)?void 0:e.name)||"Error",message:(null==(e=t.error)?void 0:e.message)||t.message},!0)},g.prototype.trackErrors=function(t){var e=this;return this.errorListenerExists||!this.canTrack()?d:(this.errorsOptions=t,window.addEventListener("error",this.captureError),this.errorListenerExists=!0,{stop:function(){window.removeEventListener("error",e.captureError)}})},g.prototype.submitError=function(t,e){var n={pid:this.projectID},o=h({pg:this.activePage||i({hash:null==(o=this.pageViewsOptions)?void 0:o.hash,search:null==(o=this.pageViewsOptions)?void 0:o.search}),lc:r(),tz:a()},t);if(e&&null!=(t=this.errorsOptions)&&t.callback){e=this.errorsOptions.callback(o);if(!1===e)return;e&&"object"==typeof e&&Object.assign(o,e)}Object.assign(o,n),this.sendRequest("error",o)},g.prototype.track=function(t){this.canTrack()&&(t=h(h({},t),{pid:this.projectID,pg:this.activePage,lc:r(),tz:a(),ref:s(),so:c(),me:u(),ca:l()}),this.sendRequest("custom",t))},g.prototype.trackPageViews=function(t){var e,n,o;return this.canTrack()?(this.pageData||(null!=(this.pageViewsOptions=t)&&t.unique||(e=setInterval(this.trackPathChange,2e3)),setTimeout(this.heartbeat,3e3),n=setInterval(this.heartbeat,28e3),o=i({hash:null==t?void 0:t.hash,search:null==t?void 0:t.search}),this.pageData={path:o,actions:{stop:function(){clearInterval(e),clearInterval(n)}}},this.trackPage(o,null==t?void 0:t.unique)),this.pageData.actions):d},g.prototype.getPerformanceStats=function(){var t;return this.canTrack()&&!this.perfStatsCollected&&null!=(t=window.performance)&&t.getEntriesByType&&(t=window.performance.getEntriesByType("navigation")[0])?(this.perfStatsCollected=!0,{dns:t.domainLookupEnd-t.domainLookupStart,tls:t.secureConnectionStart?t.requestStart-t.secureConnectionStart:0,conn:t.secureConnectionStart?t.secureConnectionStart-t.connectStart:t.connectEnd-t.connectStart,response:t.responseEnd-t.responseStart,render:t.domComplete-t.domContentLoadedEventEnd,dom_load:t.domContentLoadedEventEnd-t.responseEnd,page_load:t.loadEventStart,ttfb:t.responseStart-t.requestStart}):{}},g.prototype.heartbeat=function(){var t;(null!=(t=this.pageViewsOptions)&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},g.prototype.trackPathChange=function(){var t;this.pageData&&(t=i({hash:null==(t=this.pageViewsOptions)?void 0:t.hash,search:null==(t=this.pageViewsOptions)?void 0:t.search}),this.pageData.path!==t)&&this.trackPage(t,!1)},g.prototype.getPreviousPage=function(){if(this.activePage)return this.activePage;if("function"==typeof URL){var t=s();if(!t)return null;var e=location.host;try{var n=new URL(t),o=n.host,i=n.pathname;return e!==o?null:i}catch(t){}}return null},g.prototype.trackPage=function(t,e){var n;void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,n=this.getPerformanceStats(),this.activePage=t,this.submitPageView({pg:t},e,n,!0))},g.prototype.submitPageView=function(t,e,n,o){var i=this.getPreviousPage(),n={pid:this.projectID,perf:n,unique:e},e=h({lc:r(),tz:a(),ref:s(),so:c(),me:u(),ca:l(),prev:i},t);if(o&&null!=(i=this.pageViewsOptions)&&i.callback){t=this.pageViewsOptions.callback(e);if(!1===t)return;t&&"object"==typeof t&&Object.assign(e,t)}Object.assign(e,n),this.sendRequest("",e)},g.prototype.canTrack=function(){var t;return!(null!=(t=this.options)&&t.disabled||"undefined"==typeof window||null!=(t=this.options)&&t.respectDNT&&"1"===(null==(t=window.navigator)?void 0:t.doNotTrack)||(null==(t=this.options)||!t.devMode)&&("localhost"===(null===location||void 0===location?void 0:location.hostname)||"127.0.0.1"===(null===location||void 0===location?void 0:location.hostname)||""===(null===location||void 0===location?void 0:location.hostname))||null!==navigator&&void 0!==navigator&&navigator.webdriver)},g.prototype.sendRequest=function(t,e){var n=(null==(n=this.options)?void 0:n.apiURL)||"https://api.swetrix.com/log",o=new XMLHttpRequest;o.open("POST","".concat(n,"/").concat(t),!0),o.setRequestHeader("Content-Type","application/json"),o.send(JSON.stringify(e))},g);function g(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.errorsOptions=null,this.perfStatsCollected=!1,this.activePage=null,this.errorListenerExists=!1,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this),this.captureError=this.captureError.bind(this)}o.LIB_INSTANCE=null,o.init=function(t,e){return o.LIB_INSTANCE||(o.LIB_INSTANCE=new f(t,e)),o.LIB_INSTANCE},o.pageview=function(t){o.LIB_INSTANCE&&o.LIB_INSTANCE.submitPageView(t.payload,Boolean(t.unique),{})},o.track=function(t){o.LIB_INSTANCE&&o.LIB_INSTANCE.track(t)},o.trackError=function(t){o.LIB_INSTANCE&&o.LIB_INSTANCE.submitError(t,!1)},o.trackErrors=function(t){return o.LIB_INSTANCE?o.LIB_INSTANCE.trackErrors(t):d},o.trackPageview=function(t,e,n){o.LIB_INSTANCE&&o.LIB_INSTANCE.submitPageView({pg:t,prev:e||null},Boolean(n),{})},o.trackViews=function(e){return new Promise(function(t){o.LIB_INSTANCE?"undefined"==typeof document||"complete"===document.readyState?t(o.LIB_INSTANCE.trackPageViews(e)):window.addEventListener("load",function(){t(o.LIB_INSTANCE.trackPageViews(e))}):t(d)})},Object.defineProperty(o,"__esModule",{value:!0})}); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).swetrix={})}(this,function(r){"use strict";var i=function(){return(i=Object.assign||function(t){for(var e,n=1,r=arguments.length;ns[0]&&e[1]Math.random()||this.submitError({filename:t.filename,lineno:t.lineno,colno:t.colno,name:(null==(e=t.error)?void 0:e.name)||"Error",message:(null==(e=t.error)?void 0:e.message)||t.message},!0)},m.prototype.trackErrors=function(t){var e=this;return this.errorListenerExists||!this.canTrack()?v:(this.errorsOptions=t,window.addEventListener("error",this.captureError),this.errorListenerExists=!0,{stop:function(){window.removeEventListener("error",e.captureError)}})},m.prototype.submitError=function(t,e){var n={pid:this.projectID},r=i({pg:this.activePage||p({hash:null==(r=this.pageViewsOptions)?void 0:r.hash,search:null==(r=this.pageViewsOptions)?void 0:r.search}),lc:a(),tz:s()},t);if(e&&null!=(t=this.errorsOptions)&&t.callback){e=this.errorsOptions.callback(r);if(!1===e)return;e&&"object"==typeof e&&Object.assign(r,e)}Object.assign(r,n),this.sendRequest("error",r)},m.prototype.track=function(n){return t(this,void 0,void 0,function(){var e;return o(this,function(t){switch(t.label){case 0:return this.canTrack()?(e=i(i({},n),{pid:this.projectID,pg:this.activePage,lc:a(),tz:s(),ref:c(),so:u(),me:l(),ca:h()}),[4,this.sendRequest("custom",e)]):[2];case 1:return t.sent(),[2]}})})},m.prototype.trackPageViews=function(t){var e,n,r;return this.canTrack()?(this.pageData||(null!=(this.pageViewsOptions=t)&&t.unique||(e=setInterval(this.trackPathChange,2e3)),setTimeout(this.heartbeat,3e3),n=setInterval(this.heartbeat,28e3),r=p({hash:null==t?void 0:t.hash,search:null==t?void 0:t.search}),this.pageData={path:r,actions:{stop:function(){clearInterval(e),clearInterval(n)}}},this.trackPage(r,null==t?void 0:t.unique)),this.pageData.actions):v},m.prototype.getPerformanceStats=function(){var t;return this.canTrack()&&!this.perfStatsCollected&&null!=(t=window.performance)&&t.getEntriesByType&&(t=window.performance.getEntriesByType("navigation")[0])?(this.perfStatsCollected=!0,{dns:t.domainLookupEnd-t.domainLookupStart,tls:t.secureConnectionStart?t.requestStart-t.secureConnectionStart:0,conn:t.secureConnectionStart?t.secureConnectionStart-t.connectStart:t.connectEnd-t.connectStart,response:t.responseEnd-t.responseStart,render:t.domComplete-t.domContentLoadedEventEnd,dom_load:t.domContentLoadedEventEnd-t.responseEnd,page_load:t.loadEventStart,ttfb:t.responseStart-t.requestStart}):{}},m.prototype.heartbeat=function(){var t;(null!=(t=this.pageViewsOptions)&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},m.prototype.trackPathChange=function(){var t;this.pageData&&(t=p({hash:null==(t=this.pageViewsOptions)?void 0:t.hash,search:null==(t=this.pageViewsOptions)?void 0:t.search}),this.pageData.path!==t)&&this.trackPage(t,!1)},m.prototype.getPreviousPage=function(){if(this.activePage)return this.activePage;if("function"==typeof URL){var t=c();if(!t)return null;var e=location.host;try{var n=new URL(t),r=n.host,o=n.pathname;return e!==r?null:o}catch(t){}}return null},m.prototype.trackPage=function(t,e){var n;void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,n=this.getPerformanceStats(),this.activePage=t,this.submitPageView({pg:t},e,n,!0))},m.prototype.submitPageView=function(t,e,n,r){var o=this.getPreviousPage(),n={pid:this.projectID,perf:n,unique:e},e=i({lc:a(),tz:s(),ref:c(),so:u(),me:l(),ca:h(),prev:o},t);if(r&&null!=(o=this.pageViewsOptions)&&o.callback){t=this.pageViewsOptions.callback(e);if(!1===t)return;t&&"object"==typeof t&&Object.assign(e,t)}Object.assign(e,n),this.sendRequest("",e)},m.prototype.canTrack=function(){var t;return!(null!=(t=this.options)&&t.disabled||"undefined"==typeof window||null!=(t=this.options)&&t.respectDNT&&"1"===(null==(t=window.navigator)?void 0:t.doNotTrack)||(null==(t=this.options)||!t.devMode)&&("localhost"===(null===location||void 0===location?void 0:location.hostname)||"127.0.0.1"===(null===location||void 0===location?void 0:location.hostname)||""===(null===location||void 0===location?void 0:location.hostname))||null!==navigator&&void 0!==navigator&&navigator.webdriver)},m.prototype.sendRequest=function(n,r){return t(this,void 0,void 0,function(){var e;return o(this,function(t){switch(t.label){case 0:return e=(null==(e=this.options)?void 0:e.apiURL)||"https://api.swetrix.com/log",[4,fetch("".concat(e,"/").concat(n),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})];case 1:return t.sent(),[2]}})})},m);function m(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.errorsOptions=null,this.perfStatsCollected=!1,this.activePage=null,this.errorListenerExists=!1,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this),this.captureError=this.captureError.bind(this)}r.LIB_INSTANCE=null,r.init=function(t,e){return r.LIB_INSTANCE||(r.LIB_INSTANCE=new g(t,e)),r.LIB_INSTANCE},r.pageview=function(t){r.LIB_INSTANCE&&r.LIB_INSTANCE.submitPageView(t.payload,Boolean(t.unique),{})},r.track=function(e){return t(this,void 0,void 0,function(){return o(this,function(t){switch(t.label){case 0:return r.LIB_INSTANCE?[4,r.LIB_INSTANCE.track(e)]:[2];case 1:return t.sent(),[2]}})})},r.trackError=function(t){r.LIB_INSTANCE&&r.LIB_INSTANCE.submitError(t,!1)},r.trackErrors=function(t){return r.LIB_INSTANCE?r.LIB_INSTANCE.trackErrors(t):v},r.trackPageview=function(t,e,n){r.LIB_INSTANCE&&r.LIB_INSTANCE.submitPageView({pg:t,prev:e||null},Boolean(n),{})},r.trackViews=function(e){return new Promise(function(t){r.LIB_INSTANCE?"undefined"==typeof document||"complete"===document.readyState?t(r.LIB_INSTANCE.trackPageViews(e)):window.addEventListener("load",function(){t(r.LIB_INSTANCE.trackPageViews(e))}):t(v)})},Object.defineProperty(r,"__esModule",{value:!0})}); diff --git a/dist/swetrix.orig.js b/dist/swetrix.orig.js index 2e05297..aded7aa 100644 --- a/dist/swetrix.orig.js +++ b/dist/swetrix.orig.js @@ -30,6 +30,44 @@ return __assign.apply(this, arguments); }; + function __awaiter(thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); + } + + function __generator(thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (g && (g = 0, op[0] && (_ = 0)), _) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } + } + typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; @@ -172,11 +210,22 @@ this.sendRequest('error', errorPayload); }; Lib.prototype.track = function (event) { - if (!this.canTrack()) { - return; - } - var data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }); - this.sendRequest('custom', data); + return __awaiter(this, void 0, void 0, function () { + var data; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!this.canTrack()) { + return [2 /*return*/]; + } + data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }); + return [4 /*yield*/, this.sendRequest('custom', data)]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); + }); }; Lib.prototype.trackPageViews = function (options) { if (!this.canTrack()) { @@ -328,12 +377,26 @@ return true; }; Lib.prototype.sendRequest = function (path, body) { - var _a; - var host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST; - var req = new XMLHttpRequest(); - req.open('POST', "".concat(host, "/").concat(path), true); - req.setRequestHeader('Content-Type', 'application/json'); - req.send(JSON.stringify(body)); + return __awaiter(this, void 0, void 0, function () { + var host; + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST; + return [4 /*yield*/, fetch("".concat(host, "/").concat(path), { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + })]; + case 1: + _b.sent(); + return [2 /*return*/]; + } + }); + }); }; return Lib; }()); @@ -360,9 +423,19 @@ * @param {TrackEventOptions} event The options related to the custom event. */ function track(event) { - if (!exports.LIB_INSTANCE) - return; - exports.LIB_INSTANCE.track(event); + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!exports.LIB_INSTANCE) + return [2 /*return*/]; + return [4 /*yield*/, exports.LIB_INSTANCE.track(event)]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); + }); } /** * With this function you are able to automatically track pageviews across your application. diff --git a/src/Lib.ts b/src/Lib.ts index 420d757..69a717a 100644 --- a/src/Lib.ts +++ b/src/Lib.ts @@ -261,7 +261,7 @@ export class Lib { this.sendRequest('error', errorPayload) } - track(event: TrackEventOptions): void { + async track(event: TrackEventOptions): Promise { if (!this.canTrack()) { return } @@ -277,7 +277,7 @@ export class Lib { me: getUTMMedium(), ca: getUTMCampaign(), } - this.sendRequest('custom', data) + await this.sendRequest('custom', data) } trackPageViews(options?: PageViewsOptions): PageActions { @@ -476,11 +476,14 @@ export class Lib { return true } - private sendRequest(path: string, body: object): void { + private async sendRequest(path: string, body: object): Promise { const host = this.options?.apiURL || DEFAULT_API_HOST - const req = new XMLHttpRequest() - req.open('POST', `${host}/${path}`, true) - req.setRequestHeader('Content-Type', 'application/json') - req.send(JSON.stringify(body)) + await fetch(`${host}/${path}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + }) } } diff --git a/src/index.ts b/src/index.ts index f4b0193..0d9bd70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,10 +35,10 @@ export function init(pid: string, options?: LibOptions): Lib { * * @param {TrackEventOptions} event The options related to the custom event. */ -export function track(event: TrackEventOptions): void { +export async function track(event: TrackEventOptions): Promise { if (!LIB_INSTANCE) return - LIB_INSTANCE.track(event) + await LIB_INSTANCE.track(event) } /**