swetrix 3.4.1 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,523 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.swetrix = {}));
5
- })(this, (function (exports) { 'use strict';
6
-
7
- /******************************************************************************
8
- Copyright (c) Microsoft Corporation.
9
-
10
- Permission to use, copy, modify, and/or distribute this software for any
11
- purpose with or without fee is hereby granted.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
- PERFORMANCE OF THIS SOFTWARE.
20
- ***************************************************************************** */
21
-
22
- var __assign = function() {
23
- __assign = Object.assign || function __assign(t) {
24
- for (var s, i = 1, n = arguments.length; i < n; i++) {
25
- s = arguments[i];
26
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
27
- }
28
- return t;
29
- };
30
- return __assign.apply(this, arguments);
31
- };
32
-
33
- function __awaiter(thisArg, _arguments, P, generator) {
34
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
35
- return new (P || (P = Promise))(function (resolve, reject) {
36
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
37
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
38
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
39
- step((generator = generator.apply(thisArg, _arguments || [])).next());
40
- });
41
- }
42
-
43
- function __generator(thisArg, body) {
44
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
45
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
46
- function verb(n) { return function (v) { return step([n, v]); }; }
47
- function step(op) {
48
- if (f) throw new TypeError("Generator is already executing.");
49
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
50
- 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;
51
- if (y = 0, t) op = [op[0] & 2, t.value];
52
- switch (op[0]) {
53
- case 0: case 1: t = op; break;
54
- case 4: _.label++; return { value: op[1], done: false };
55
- case 5: _.label++; y = op[1]; op = [0]; continue;
56
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
57
- default:
58
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
59
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
60
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
61
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
62
- if (t[2]) _.ops.pop();
63
- _.trys.pop(); continue;
64
- }
65
- op = body.call(thisArg, _);
66
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
67
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
68
- }
69
- }
70
-
71
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
72
- var e = new Error(message);
73
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
74
- };
75
-
76
- var findInSearch = function (exp) {
77
- var res = location.search.match(exp);
78
- return (res && res[2]) || undefined;
79
- };
80
- var utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/;
81
- var utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/;
82
- var utmMediumRegex = /[?&](utm_medium)=([^?&]+)/;
83
- var utmTermRegex = /[?&](utm_term)=([^?&]+)/;
84
- var utmContentRegex = /[?&](utm_content)=([^?&]+)/;
85
- var isInBrowser = function () {
86
- return typeof window !== 'undefined';
87
- };
88
- var isLocalhost = function () {
89
- return (location === null || location === void 0 ? void 0 : location.hostname) === 'localhost' || (location === null || location === void 0 ? void 0 : location.hostname) === '127.0.0.1' || (location === null || location === void 0 ? void 0 : location.hostname) === '';
90
- };
91
- var isAutomated = function () {
92
- return navigator === null || navigator === void 0 ? void 0 : navigator.webdriver;
93
- };
94
- var getLocale = function () {
95
- return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language;
96
- };
97
- var getTimezone = function () {
98
- try {
99
- return Intl.DateTimeFormat().resolvedOptions().timeZone;
100
- }
101
- catch (e) {
102
- return;
103
- }
104
- };
105
- var getReferrer = function () {
106
- return document.referrer || undefined;
107
- };
108
- var getUTMSource = function () { return findInSearch(utmSourceRegex); };
109
- var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
110
- var getUTMCampaign = function () { return findInSearch(utmCampaignRegex); };
111
- var getUTMTerm = function () { return findInSearch(utmTermRegex); };
112
- var getUTMContent = function () { return findInSearch(utmContentRegex); };
113
- /**
114
- * Function used to track the current page (path) of the application.
115
- * Will work in cases where the path looks like:
116
- * - /path
117
- * - /#/path
118
- * - /path?search
119
- * - /path?search#hash
120
- * - /path#hash?search
121
- *
122
- * @param options - Options for the function.
123
- * @param options.hash - Whether to trigger on hash change.
124
- * @param options.search - Whether to trigger on search change.
125
- * @returns The path of the current page.
126
- */
127
- var getPath = function (options) {
128
- var result = location.pathname || '';
129
- if (options.hash) {
130
- var hashIndex = location.hash.indexOf('?');
131
- var hashString = hashIndex > -1 ? location.hash.substring(0, hashIndex) : location.hash;
132
- result += hashString;
133
- }
134
- if (options.search) {
135
- var hashIndex = location.hash.indexOf('?');
136
- var searchString = location.search || (hashIndex > -1 ? location.hash.substring(hashIndex) : '');
137
- result += searchString;
138
- }
139
- return result;
140
- };
141
-
142
- var defaultActions = {
143
- stop: function () { },
144
- };
145
- var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
146
- var Lib = /** @class */ (function () {
147
- function Lib(projectID, options) {
148
- this.projectID = projectID;
149
- this.options = options;
150
- this.pageData = null;
151
- this.pageViewsOptions = null;
152
- this.errorsOptions = null;
153
- this.perfStatsCollected = false;
154
- this.activePage = null;
155
- this.errorListenerExists = false;
156
- this.trackPathChange = this.trackPathChange.bind(this);
157
- this.heartbeat = this.heartbeat.bind(this);
158
- this.captureError = this.captureError.bind(this);
159
- }
160
- Lib.prototype.captureError = function (event) {
161
- var _a, _b, _c;
162
- if (typeof ((_a = this.errorsOptions) === null || _a === void 0 ? void 0 : _a.sampleRate) === 'number' && this.errorsOptions.sampleRate > Math.random()) {
163
- return;
164
- }
165
- this.submitError({
166
- // The file in which error occured.
167
- filename: event.filename,
168
- // The line of code error occured on.
169
- lineno: event.lineno,
170
- // The column of code error occured on.
171
- colno: event.colno,
172
- // Name of the error, if not exists (i.e. it's a custom thrown error). The initial value of name is "Error", but just in case lets explicitly set it here too.
173
- name: ((_b = event.error) === null || _b === void 0 ? void 0 : _b.name) || 'Error',
174
- // Description of the error. By default, we use message from Error object, is it does not contain the error name
175
- // (we want to split error name and message so we could group them together later in dashboard).
176
- // If message in error object does not exist - lets use a message from the Error event itself.
177
- message: ((_c = event.error) === null || _c === void 0 ? void 0 : _c.message) || event.message,
178
- }, true);
179
- };
180
- Lib.prototype.trackErrors = function (options) {
181
- var _this = this;
182
- if (this.errorListenerExists || !this.canTrack()) {
183
- return defaultActions;
184
- }
185
- this.errorsOptions = options;
186
- window.addEventListener('error', this.captureError);
187
- this.errorListenerExists = true;
188
- return {
189
- stop: function () {
190
- window.removeEventListener('error', _this.captureError);
191
- },
192
- };
193
- };
194
- Lib.prototype.submitError = function (payload, evokeCallback) {
195
- var _a, _b, _c;
196
- var privateData = {
197
- pid: this.projectID,
198
- };
199
- var errorPayload = __assign({ pg: this.activePage ||
200
- getPath({
201
- hash: (_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.hash,
202
- search: (_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.search,
203
- }), lc: getLocale(), tz: getTimezone() }, payload);
204
- if (evokeCallback && ((_c = this.errorsOptions) === null || _c === void 0 ? void 0 : _c.callback)) {
205
- var callbackResult = this.errorsOptions.callback(errorPayload);
206
- if (callbackResult === false) {
207
- return;
208
- }
209
- if (callbackResult && typeof callbackResult === 'object') {
210
- Object.assign(errorPayload, callbackResult);
211
- }
212
- }
213
- Object.assign(errorPayload, privateData);
214
- this.sendRequest('error', errorPayload);
215
- };
216
- Lib.prototype.track = function (event) {
217
- return __awaiter(this, void 0, void 0, function () {
218
- var data;
219
- return __generator(this, function (_a) {
220
- switch (_a.label) {
221
- case 0:
222
- if (!this.canTrack()) {
223
- return [2 /*return*/];
224
- }
225
- data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign(), te: getUTMTerm(), co: getUTMContent() });
226
- return [4 /*yield*/, this.sendRequest('custom', data)];
227
- case 1:
228
- _a.sent();
229
- return [2 /*return*/];
230
- }
231
- });
232
- });
233
- };
234
- Lib.prototype.trackPageViews = function (options) {
235
- if (!this.canTrack()) {
236
- return defaultActions;
237
- }
238
- if (this.pageData) {
239
- return this.pageData.actions;
240
- }
241
- this.pageViewsOptions = options;
242
- var interval;
243
- if (!(options === null || options === void 0 ? void 0 : options.unique)) {
244
- interval = setInterval(this.trackPathChange, 2000);
245
- }
246
- setTimeout(this.heartbeat, 3000);
247
- var hbInterval = setInterval(this.heartbeat, 28000);
248
- var path = getPath({
249
- hash: options === null || options === void 0 ? void 0 : options.hash,
250
- search: options === null || options === void 0 ? void 0 : options.search,
251
- });
252
- this.pageData = {
253
- path: path,
254
- actions: {
255
- stop: function () {
256
- clearInterval(interval);
257
- clearInterval(hbInterval);
258
- },
259
- },
260
- };
261
- this.trackPage(path, options === null || options === void 0 ? void 0 : options.unique);
262
- return this.pageData.actions;
263
- };
264
- Lib.prototype.getPerformanceStats = function () {
265
- var _a;
266
- if (!this.canTrack() || this.perfStatsCollected || !((_a = window.performance) === null || _a === void 0 ? void 0 : _a.getEntriesByType)) {
267
- return {};
268
- }
269
- var perf = window.performance.getEntriesByType('navigation')[0];
270
- if (!perf) {
271
- return {};
272
- }
273
- this.perfStatsCollected = true;
274
- return {
275
- // Network
276
- dns: perf.domainLookupEnd - perf.domainLookupStart, // DNS Resolution
277
- tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0, // TLS Setup; checking if secureConnectionStart is not 0 (it's 0 for non-https websites)
278
- conn: perf.secureConnectionStart
279
- ? perf.secureConnectionStart - perf.connectStart
280
- : perf.connectEnd - perf.connectStart, // Connection time
281
- response: perf.responseEnd - perf.responseStart, // Response Time (Download)
282
- // Frontend
283
- render: perf.domComplete - perf.domContentLoadedEventEnd, // Browser rendering the HTML time
284
- dom_load: perf.domContentLoadedEventEnd - perf.responseEnd, // DOM loading timing
285
- page_load: perf.loadEventStart, // Page load time
286
- // Backend
287
- ttfb: perf.responseStart - perf.requestStart,
288
- };
289
- };
290
- Lib.prototype.heartbeat = function () {
291
- var _a;
292
- if (!((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.heartbeatOnBackground) && document.visibilityState === 'hidden') {
293
- return;
294
- }
295
- var data = {
296
- pid: this.projectID,
297
- };
298
- this.sendRequest('hb', data);
299
- };
300
- // Tracking path changes. If path changes -> calling this.trackPage method
301
- Lib.prototype.trackPathChange = function () {
302
- var _a, _b;
303
- if (!this.pageData)
304
- return;
305
- var newPath = getPath({
306
- hash: (_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.hash,
307
- search: (_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.search,
308
- });
309
- var path = this.pageData.path;
310
- if (path !== newPath) {
311
- this.trackPage(newPath, false);
312
- }
313
- };
314
- Lib.prototype.getPreviousPage = function () {
315
- // Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
316
- // That method of getting previous page works for SPA websites
317
- if (this.activePage) {
318
- return this.activePage;
319
- }
320
- // Checking if URL is supported by the browser (for example, IE11 does not support it)
321
- if (typeof URL === 'function') {
322
- // That method of getting previous page works for websites with page reloads
323
- var referrer = getReferrer();
324
- if (!referrer) {
325
- return null;
326
- }
327
- var host = location.host;
328
- try {
329
- var url = new URL(referrer);
330
- var refHost = url.host, pathname = url.pathname;
331
- if (host !== refHost) {
332
- return null;
333
- }
334
- return pathname;
335
- }
336
- catch (_a) {
337
- return null;
338
- }
339
- }
340
- return null;
341
- };
342
- Lib.prototype.trackPage = function (pg, unique) {
343
- if (unique === void 0) { unique = false; }
344
- if (!this.pageData)
345
- return;
346
- this.pageData.path = pg;
347
- var perf = this.getPerformanceStats();
348
- this.activePage = pg;
349
- this.submitPageView({ pg: pg }, unique, perf, true);
350
- };
351
- Lib.prototype.submitPageView = function (payload, unique, perf, evokeCallback) {
352
- var _a;
353
- var prev = this.getPreviousPage();
354
- var privateData = {
355
- pid: this.projectID,
356
- perf: perf,
357
- unique: unique,
358
- };
359
- var pvPayload = __assign({ lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign(), te: getUTMTerm(), co: getUTMContent(), prev: prev }, payload);
360
- if (evokeCallback && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.callback)) {
361
- var callbackResult = this.pageViewsOptions.callback(pvPayload);
362
- if (callbackResult === false) {
363
- return;
364
- }
365
- if (callbackResult && typeof callbackResult === 'object') {
366
- Object.assign(pvPayload, callbackResult);
367
- }
368
- }
369
- Object.assign(pvPayload, privateData);
370
- this.sendRequest('', pvPayload);
371
- };
372
- Lib.prototype.canTrack = function () {
373
- var _a, _b, _c, _d;
374
- if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) ||
375
- !isInBrowser() ||
376
- (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') ||
377
- (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.devMode) && isLocalhost()) ||
378
- isAutomated()) {
379
- return false;
380
- }
381
- return true;
382
- };
383
- Lib.prototype.sendRequest = function (path, body) {
384
- return __awaiter(this, void 0, void 0, function () {
385
- var host;
386
- var _a;
387
- return __generator(this, function (_b) {
388
- switch (_b.label) {
389
- case 0:
390
- host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST;
391
- return [4 /*yield*/, fetch("".concat(host, "/").concat(path), {
392
- method: 'POST',
393
- headers: {
394
- 'Content-Type': 'application/json',
395
- },
396
- body: JSON.stringify(body),
397
- })];
398
- case 1:
399
- _b.sent();
400
- return [2 /*return*/];
401
- }
402
- });
403
- });
404
- };
405
- return Lib;
406
- }());
407
-
408
- exports.LIB_INSTANCE = null;
409
- /**
410
- * Initialise the tracking library instance (other methods won't work if the library is not initialised).
411
- *
412
- * @param {string} pid The Project ID to link the instance of Swetrix.js to.
413
- * @param {LibOptions} options Options related to the tracking.
414
- * @returns {Lib} Instance of the Swetrix.js.
415
- */
416
- function init(pid, options) {
417
- if (!exports.LIB_INSTANCE) {
418
- exports.LIB_INSTANCE = new Lib(pid, options);
419
- }
420
- return exports.LIB_INSTANCE;
421
- }
422
- /**
423
- * With this function you are able to track any custom events you want.
424
- * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
425
- * The total number of track calls and their conversion rate will be saved.
426
- *
427
- * @param {TrackEventOptions} event The options related to the custom event.
428
- */
429
- function track(event) {
430
- return __awaiter(this, void 0, void 0, function () {
431
- return __generator(this, function (_a) {
432
- switch (_a.label) {
433
- case 0:
434
- if (!exports.LIB_INSTANCE)
435
- return [2 /*return*/];
436
- return [4 /*yield*/, exports.LIB_INSTANCE.track(event)];
437
- case 1:
438
- _a.sent();
439
- return [2 /*return*/];
440
- }
441
- });
442
- });
443
- }
444
- /**
445
- * With this function you are able to automatically track pageviews across your application.
446
- *
447
- * @param {PageViewsOptions} options Pageviews tracking options.
448
- * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
449
- */
450
- function trackViews(options) {
451
- return new Promise(function (resolve) {
452
- if (!exports.LIB_INSTANCE) {
453
- resolve(defaultActions);
454
- return;
455
- }
456
- // We need to verify that document.readyState is complete for the performance stats to be collected correctly.
457
- if (typeof document === 'undefined' || document.readyState === 'complete') {
458
- resolve(exports.LIB_INSTANCE.trackPageViews(options));
459
- }
460
- else {
461
- window.addEventListener('load', function () {
462
- // @ts-ignore
463
- resolve(exports.LIB_INSTANCE.trackPageViews(options));
464
- });
465
- }
466
- });
467
- }
468
- /**
469
- * This function is used to set up automatic error events tracking.
470
- * It set's up an error listener, and whenever an error happens, it gets tracked.
471
- *
472
- * @returns {ErrorActions} The actions related to the tracking. Used to stop tracking errors.
473
- */
474
- function trackErrors(options) {
475
- if (!exports.LIB_INSTANCE) {
476
- return defaultActions;
477
- }
478
- return exports.LIB_INSTANCE.trackErrors(options);
479
- }
480
- /**
481
- * This function is used to manually track an error event.
482
- * It's useful if you want to track specific errors in your application.
483
- *
484
- * @param payload Swetrix error object to send.
485
- * @returns void
486
- */
487
- function trackError(payload) {
488
- if (!exports.LIB_INSTANCE)
489
- return;
490
- exports.LIB_INSTANCE.submitError(payload, false);
491
- }
492
- /**
493
- * This function is used to manually track a page view event.
494
- * It's useful if your application uses esoteric routing which is not supported by Swetrix by default.
495
- *
496
- * @deprecated This function is deprecated and will be removed soon, please use the `pageview` instead.
497
- * @param pg Path of the page to track (this will be sent to the Swetrix API and displayed in the dashboard).
498
- * @param prev Path of the previous page.
499
- * @param unique If set to `true`, only 1 event with the same ID will be saved per user session.
500
- * @returns void
501
- */
502
- function trackPageview(pg, prev, unique) {
503
- if (!exports.LIB_INSTANCE)
504
- return;
505
- exports.LIB_INSTANCE.submitPageView({ pg: pg, prev: prev || null }, Boolean(unique), {});
506
- }
507
- function pageview(options) {
508
- if (!exports.LIB_INSTANCE)
509
- return;
510
- exports.LIB_INSTANCE.submitPageView(options.payload, Boolean(options.unique), {});
511
- }
512
-
513
- exports.init = init;
514
- exports.pageview = pageview;
515
- exports.track = track;
516
- exports.trackError = trackError;
517
- exports.trackErrors = trackErrors;
518
- exports.trackPageview = trackPageview;
519
- exports.trackViews = trackViews;
520
-
521
- Object.defineProperty(exports, '__esModule', { value: true });
522
-
523
- }));