swetrix 2.2.1 → 2.3.0

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.
@@ -22,317 +22,326 @@ var __assign = function() {
22
22
  return t;
23
23
  };
24
24
  return __assign.apply(this, arguments);
25
+ };
26
+
27
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
28
+ var e = new Error(message);
29
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
25
30
  };
26
31
 
27
- var findInSearch = function (exp) {
28
- var res = location.search.match(exp);
29
- return (res && res[2]) || undefined;
30
- };
31
- var utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/;
32
- var utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/;
33
- var utmMediumRegex = /[?&](utm_medium)=([^?&]+)/;
34
- var isInBrowser = function () {
35
- return typeof window !== 'undefined';
36
- };
37
- var isLocalhost = function () {
38
- 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) === '';
39
- };
40
- var isAutomated = function () {
41
- return navigator === null || navigator === void 0 ? void 0 : navigator.webdriver;
42
- };
43
- var getLocale = function () {
44
- return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language;
45
- };
46
- var getTimezone = function () {
47
- try {
48
- return Intl.DateTimeFormat().resolvedOptions().timeZone;
49
- }
50
- catch (e) {
51
- return;
52
- }
53
- };
54
- var getReferrer = function () {
55
- return document.referrer || undefined;
56
- };
57
- var getUTMSource = function () { return findInSearch(utmSourceRegex); };
58
- var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
59
- var getUTMCampaign = function () { return findInSearch(utmCampaignRegex); };
60
- var getPath = function () {
61
- // TODO: Maybe we should also include such data as location.hash or location.search
62
- return location.pathname || '';
32
+ var findInSearch = function (exp) {
33
+ var res = location.search.match(exp);
34
+ return (res && res[2]) || undefined;
35
+ };
36
+ var utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/;
37
+ var utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/;
38
+ var utmMediumRegex = /[?&](utm_medium)=([^?&]+)/;
39
+ var isInBrowser = function () {
40
+ return typeof window !== 'undefined';
41
+ };
42
+ var isLocalhost = function () {
43
+ 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) === '';
44
+ };
45
+ var isAutomated = function () {
46
+ return navigator === null || navigator === void 0 ? void 0 : navigator.webdriver;
47
+ };
48
+ var getLocale = function () {
49
+ return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language;
50
+ };
51
+ var getTimezone = function () {
52
+ try {
53
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
54
+ }
55
+ catch (e) {
56
+ return;
57
+ }
58
+ };
59
+ var getReferrer = function () {
60
+ return document.referrer || undefined;
61
+ };
62
+ var getUTMSource = function () { return findInSearch(utmSourceRegex); };
63
+ var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
64
+ var getUTMCampaign = function () { return findInSearch(utmCampaignRegex); };
65
+ var getPath = function () {
66
+ // TODO: Maybe we should also include such data as location.hash or location.search
67
+ return location.pathname || '';
63
68
  };
64
69
 
65
- var defaultPageActions = {
66
- stop: function () { },
67
- };
68
- var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
69
- var Lib = /** @class */ (function () {
70
- function Lib(projectID, options) {
71
- this.projectID = projectID;
72
- this.options = options;
73
- this.pageData = null;
74
- this.pageViewsOptions = null;
75
- this.perfStatsCollected = false;
76
- this.activePage = null;
77
- this.trackPathChange = this.trackPathChange.bind(this);
78
- this.heartbeat = this.heartbeat.bind(this);
79
- }
80
- Lib.prototype.track = function (event) {
81
- if (!this.canTrack()) {
82
- return;
83
- }
84
- var data = __assign({ pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }, event);
85
- this.sendRequest('custom', data);
86
- };
87
- Lib.prototype.trackPageViews = function (options) {
88
- if (!this.canTrack()) {
89
- return defaultPageActions;
90
- }
91
- if (this.pageData) {
92
- return this.pageData.actions;
93
- }
94
- this.pageViewsOptions = options;
95
- var hbInterval, interval;
96
- if (!(options === null || options === void 0 ? void 0 : options.unique)) {
97
- interval = setInterval(this.trackPathChange, 2000);
98
- }
99
- if (!(options === null || options === void 0 ? void 0 : options.noHeartbeat)) {
100
- setTimeout(this.heartbeat, 3000);
101
- hbInterval = setInterval(this.heartbeat, 28000);
102
- }
103
- var path = getPath();
104
- this.pageData = {
105
- path: path,
106
- actions: {
107
- stop: function () {
108
- clearInterval(interval);
109
- clearInterval(hbInterval);
110
- },
111
- },
112
- };
113
- this.trackPage(path, options === null || options === void 0 ? void 0 : options.unique);
114
- return this.pageData.actions;
115
- };
116
- Lib.prototype.getPerformanceStats = function () {
117
- var _a;
118
- if (!this.canTrack() || this.perfStatsCollected || !((_a = window.performance) === null || _a === void 0 ? void 0 : _a.getEntriesByType)) {
119
- return {};
120
- }
121
- var perf = window.performance.getEntriesByType('navigation')[0];
122
- if (!perf) {
123
- return {};
124
- }
125
- this.perfStatsCollected = true;
126
- return {
127
- // Network
128
- // @ts-ignore
129
- dns: perf.domainLookupEnd - perf.domainLookupStart,
130
- // @ts-ignore
131
- tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0,
132
- // @ts-ignore
133
- conn: perf.secureConnectionStart ? perf.secureConnectionStart - perf.connectStart : perf.connectEnd - perf.connectStart,
134
- // @ts-ignore
135
- response: perf.responseEnd - perf.responseStart,
136
- // Frontend
137
- // @ts-ignore
138
- render: perf.domComplete - perf.domContentLoadedEventEnd,
139
- // @ts-ignore
140
- dom_load: perf.domContentLoadedEventEnd - perf.responseEnd,
141
- // @ts-ignore
142
- page_load: perf.loadEventStart,
143
- // Backend
144
- // @ts-ignore
145
- ttfb: perf.responseStart - perf.requestStart,
146
- };
147
- };
148
- Lib.prototype.heartbeat = function () {
149
- var _a;
150
- if (!((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.heartbeatOnBackground) && document.visibilityState === 'hidden') {
151
- return;
152
- }
153
- var data = {
154
- pid: this.projectID,
155
- };
156
- this.sendRequest('hb', data);
157
- };
158
- Lib.prototype.checkIgnore = function (path) {
159
- var _a;
160
- var ignore = (_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.ignore;
161
- if (Array.isArray(ignore)) {
162
- for (var i = 0; i < ignore.length; ++i) {
163
- if (ignore[i] === path)
164
- return true;
165
- // @ts-ignore
166
- if (ignore[i] instanceof RegExp && ignore[i].test(path))
167
- return true;
168
- }
169
- }
170
- return false;
171
- };
172
- // Tracking path changes. If path changes -> calling this.trackPage method
173
- Lib.prototype.trackPathChange = function () {
174
- if (!this.pageData)
175
- return;
176
- var newPath = getPath();
177
- var path = this.pageData.path;
178
- if (path !== newPath) {
179
- this.trackPage(newPath, false);
180
- }
181
- };
182
- Lib.prototype.getPreviousPage = function () {
183
- // Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
184
- // That method of getting previous page works for SPA websites
185
- if (this.activePage) {
186
- if (this.checkIgnore(this.activePage)) {
187
- return null;
188
- }
189
- return this.activePage;
190
- }
191
- // Checking if URL is supported by the browser (for example, IE11 does not support it)
192
- if (typeof URL === 'function') {
193
- // That method of getting previous page works for websites with page reloads
194
- var referrer = getReferrer();
195
- if (!referrer) {
196
- return null;
197
- }
198
- var host = location.host;
199
- try {
200
- var url = new URL(referrer);
201
- var refHost = url.host, pathname = url.pathname;
202
- if (host !== refHost) {
203
- return null;
204
- }
205
- if (this.checkIgnore(pathname)) {
206
- return null;
207
- }
208
- return pathname;
209
- }
210
- catch (_a) {
211
- return null;
212
- }
213
- }
214
- return null;
215
- };
216
- Lib.prototype.trackPage = function (pg, unique) {
217
- var _a;
218
- if (unique === void 0) { unique = false; }
219
- if (!this.pageData)
220
- return;
221
- this.pageData.path = pg;
222
- if (this.checkIgnore(pg))
223
- return;
224
- var perf = this.getPerformanceStats();
225
- var prev;
226
- if (!((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.noUserFlow)) {
227
- prev = this.getPreviousPage();
228
- }
229
- var data = {
230
- pid: this.projectID,
231
- lc: getLocale(),
232
- tz: getTimezone(),
233
- ref: getReferrer(),
234
- so: getUTMSource(),
235
- me: getUTMMedium(),
236
- ca: getUTMCampaign(),
237
- unique: unique,
238
- pg: pg,
239
- perf: perf,
240
- prev: prev,
241
- };
242
- this.activePage = pg;
243
- this.sendRequest('', data);
244
- };
245
- Lib.prototype.debug = function (message) {
246
- var _a;
247
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.debug) {
248
- console.log('[Swetrix]', message);
249
- }
250
- };
251
- Lib.prototype.canTrack = function () {
252
- var _a, _b, _c, _d;
253
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) {
254
- this.debug('Tracking disabled: the \'disabled\' setting is set to true.');
255
- return false;
256
- }
257
- if (!isInBrowser()) {
258
- this.debug('Tracking disabled: script does not run in browser environment.');
259
- return false;
260
- }
261
- if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') {
262
- this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
263
- return false;
264
- }
265
- if (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.debug) && isLocalhost()) {
266
- return false;
267
- }
268
- if (isAutomated()) {
269
- this.debug('Tracking disabled: navigation is automated by WebDriver.');
270
- return false;
271
- }
272
- return true;
273
- };
274
- Lib.prototype.sendRequest = function (path, body) {
275
- var _a;
276
- var host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST;
277
- var req = new XMLHttpRequest();
278
- req.open('POST', "".concat(host, "/").concat(path), true);
279
- req.setRequestHeader('Content-Type', 'application/json');
280
- req.send(JSON.stringify(body));
281
- };
282
- return Lib;
70
+ var defaultPageActions = {
71
+ stop: function () { },
72
+ };
73
+ var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
74
+ var Lib = /** @class */ (function () {
75
+ function Lib(projectID, options) {
76
+ this.projectID = projectID;
77
+ this.options = options;
78
+ this.pageData = null;
79
+ this.pageViewsOptions = null;
80
+ this.perfStatsCollected = false;
81
+ this.activePage = null;
82
+ this.trackPathChange = this.trackPathChange.bind(this);
83
+ this.heartbeat = this.heartbeat.bind(this);
84
+ }
85
+ Lib.prototype.track = function (event) {
86
+ if (!this.canTrack()) {
87
+ return;
88
+ }
89
+ var data = __assign({ pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }, event);
90
+ this.sendRequest('custom', data);
91
+ };
92
+ Lib.prototype.trackPageViews = function (options) {
93
+ if (!this.canTrack()) {
94
+ return defaultPageActions;
95
+ }
96
+ if (this.pageData) {
97
+ return this.pageData.actions;
98
+ }
99
+ this.pageViewsOptions = options;
100
+ var hbInterval, interval;
101
+ if (!(options === null || options === void 0 ? void 0 : options.unique)) {
102
+ interval = setInterval(this.trackPathChange, 2000);
103
+ }
104
+ if (!(options === null || options === void 0 ? void 0 : options.noHeartbeat)) {
105
+ setTimeout(this.heartbeat, 3000);
106
+ hbInterval = setInterval(this.heartbeat, 28000);
107
+ }
108
+ var path = getPath();
109
+ this.pageData = {
110
+ path: path,
111
+ actions: {
112
+ stop: function () {
113
+ clearInterval(interval);
114
+ clearInterval(hbInterval);
115
+ },
116
+ },
117
+ };
118
+ this.trackPage(path, options === null || options === void 0 ? void 0 : options.unique);
119
+ return this.pageData.actions;
120
+ };
121
+ Lib.prototype.getPerformanceStats = function () {
122
+ var _a;
123
+ if (!this.canTrack() || this.perfStatsCollected || !((_a = window.performance) === null || _a === void 0 ? void 0 : _a.getEntriesByType)) {
124
+ return {};
125
+ }
126
+ var perf = window.performance.getEntriesByType('navigation')[0];
127
+ if (!perf) {
128
+ return {};
129
+ }
130
+ this.perfStatsCollected = true;
131
+ return {
132
+ // Network
133
+ // @ts-ignore
134
+ dns: perf.domainLookupEnd - perf.domainLookupStart,
135
+ // @ts-ignore
136
+ tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0,
137
+ // @ts-ignore
138
+ conn: perf.secureConnectionStart ? perf.secureConnectionStart - perf.connectStart : perf.connectEnd - perf.connectStart,
139
+ // @ts-ignore
140
+ response: perf.responseEnd - perf.responseStart,
141
+ // Frontend
142
+ // @ts-ignore
143
+ render: perf.domComplete - perf.domContentLoadedEventEnd,
144
+ // @ts-ignore
145
+ dom_load: perf.domContentLoadedEventEnd - perf.responseEnd,
146
+ // @ts-ignore
147
+ page_load: perf.loadEventStart,
148
+ // Backend
149
+ // @ts-ignore
150
+ ttfb: perf.responseStart - perf.requestStart,
151
+ };
152
+ };
153
+ Lib.prototype.heartbeat = function () {
154
+ var _a;
155
+ if (!((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.heartbeatOnBackground) && document.visibilityState === 'hidden') {
156
+ return;
157
+ }
158
+ var data = {
159
+ pid: this.projectID,
160
+ };
161
+ this.sendRequest('hb', data);
162
+ };
163
+ Lib.prototype.checkIgnore = function (path) {
164
+ var _a;
165
+ var ignore = (_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.ignore;
166
+ if (Array.isArray(ignore)) {
167
+ for (var i = 0; i < ignore.length; ++i) {
168
+ if (ignore[i] === path)
169
+ return true;
170
+ // @ts-ignore
171
+ if (ignore[i] instanceof RegExp && ignore[i].test(path))
172
+ return true;
173
+ }
174
+ }
175
+ return false;
176
+ };
177
+ // Tracking path changes. If path changes -> calling this.trackPage method
178
+ Lib.prototype.trackPathChange = function () {
179
+ if (!this.pageData)
180
+ return;
181
+ var newPath = getPath();
182
+ var path = this.pageData.path;
183
+ if (path !== newPath) {
184
+ this.trackPage(newPath, false);
185
+ }
186
+ };
187
+ Lib.prototype.getPreviousPage = function () {
188
+ var _a, _b;
189
+ // Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
190
+ // That method of getting previous page works for SPA websites
191
+ if (this.activePage) {
192
+ var shouldIgnore = this.checkIgnore(this.activePage);
193
+ if (shouldIgnore && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.doNotAnonymise)) {
194
+ return null;
195
+ }
196
+ return shouldIgnore ? null : this.activePage;
197
+ }
198
+ // Checking if URL is supported by the browser (for example, IE11 does not support it)
199
+ if (typeof URL === 'function') {
200
+ // That method of getting previous page works for websites with page reloads
201
+ var referrer = getReferrer();
202
+ if (!referrer) {
203
+ return null;
204
+ }
205
+ var host = location.host;
206
+ try {
207
+ var url = new URL(referrer);
208
+ var refHost = url.host, pathname = url.pathname;
209
+ if (host !== refHost) {
210
+ return null;
211
+ }
212
+ var shouldIgnore = this.checkIgnore(pathname);
213
+ if (shouldIgnore && ((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.doNotAnonymise)) {
214
+ return null;
215
+ }
216
+ return shouldIgnore ? null : pathname;
217
+ }
218
+ catch (_c) {
219
+ return null;
220
+ }
221
+ }
222
+ return null;
223
+ };
224
+ Lib.prototype.trackPage = function (pg, unique) {
225
+ var _a, _b;
226
+ if (unique === void 0) { unique = false; }
227
+ if (!this.pageData)
228
+ return;
229
+ this.pageData.path = pg;
230
+ var shouldIgnore = this.checkIgnore(pg);
231
+ if (shouldIgnore && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.doNotAnonymise))
232
+ return;
233
+ var perf = this.getPerformanceStats();
234
+ var prev;
235
+ if (!((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.noUserFlow)) {
236
+ prev = this.getPreviousPage();
237
+ }
238
+ var data = {
239
+ pid: this.projectID,
240
+ lc: getLocale(),
241
+ tz: getTimezone(),
242
+ ref: getReferrer(),
243
+ so: getUTMSource(),
244
+ me: getUTMMedium(),
245
+ ca: getUTMCampaign(),
246
+ unique: unique,
247
+ pg: shouldIgnore ? null : pg,
248
+ perf: perf,
249
+ prev: prev,
250
+ };
251
+ this.activePage = pg;
252
+ this.sendRequest('', data);
253
+ };
254
+ Lib.prototype.debug = function (message) {
255
+ var _a;
256
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.debug) {
257
+ console.log('[Swetrix]', message);
258
+ }
259
+ };
260
+ Lib.prototype.canTrack = function () {
261
+ var _a, _b, _c, _d;
262
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) {
263
+ this.debug('Tracking disabled: the \'disabled\' setting is set to true.');
264
+ return false;
265
+ }
266
+ if (!isInBrowser()) {
267
+ this.debug('Tracking disabled: script does not run in browser environment.');
268
+ return false;
269
+ }
270
+ if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') {
271
+ this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
272
+ return false;
273
+ }
274
+ if (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.debug) && isLocalhost()) {
275
+ return false;
276
+ }
277
+ if (isAutomated()) {
278
+ this.debug('Tracking disabled: navigation is automated by WebDriver.');
279
+ return false;
280
+ }
281
+ return true;
282
+ };
283
+ Lib.prototype.sendRequest = function (path, body) {
284
+ var _a;
285
+ var host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST;
286
+ var req = new XMLHttpRequest();
287
+ req.open('POST', "".concat(host, "/").concat(path), true);
288
+ req.setRequestHeader('Content-Type', 'application/json');
289
+ req.send(JSON.stringify(body));
290
+ };
291
+ return Lib;
283
292
  }());
284
293
 
285
- var LIB_INSTANCE = null;
286
- /**
287
- * Initialise the tracking library instance (other methods won't work if the library is not initialised).
288
- *
289
- * @param {string} pid The Project ID to link the instance of Swetrix.js to.
290
- * @param {LibOptions} options Options related to the tracking.
291
- * @returns {Lib} Instance of the Swetrix.js.
292
- */
293
- function init(pid, options) {
294
- if (!LIB_INSTANCE) {
295
- LIB_INSTANCE = new Lib(pid, options);
296
- }
297
- return LIB_INSTANCE;
298
- }
299
- /**
300
- * With this function you are able to track any custom events you want.
301
- * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
302
- * The total number of track calls and their conversion rate will be saved.
303
- *
304
- * @param {TrackEventOptions} event The options related to the custom event.
305
- */
306
- function track(event) {
307
- if (!LIB_INSTANCE)
308
- return;
309
- LIB_INSTANCE.track(event);
310
- }
311
- /**
312
- * With this function you are able to track any custom events you want.
313
- * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
314
- * The total number of track calls and their conversion rate will be saved.
315
- *
316
- * @param {PageViewsOptions} options The options related to the custom event.
317
- * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
318
- */
319
- function trackViews(options) {
320
- return new Promise(function (resolve) {
321
- if (!LIB_INSTANCE) {
322
- resolve(defaultPageActions);
323
- return;
324
- }
325
- // We need to verify that document.readyState is complete for the performance stats to be collected correctly.
326
- if (typeof document === 'undefined' || document.readyState === 'complete') {
327
- resolve(LIB_INSTANCE.trackPageViews(options));
328
- }
329
- else {
330
- window.addEventListener('load', function () {
331
- // @ts-ignore
332
- resolve(LIB_INSTANCE.trackPageViews(options));
333
- });
334
- }
335
- });
294
+ var LIB_INSTANCE = null;
295
+ /**
296
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
297
+ *
298
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
299
+ * @param {LibOptions} options Options related to the tracking.
300
+ * @returns {Lib} Instance of the Swetrix.js.
301
+ */
302
+ function init(pid, options) {
303
+ if (!LIB_INSTANCE) {
304
+ LIB_INSTANCE = new Lib(pid, options);
305
+ }
306
+ return LIB_INSTANCE;
307
+ }
308
+ /**
309
+ * With this function you are able to track any custom events you want.
310
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
311
+ * The total number of track calls and their conversion rate will be saved.
312
+ *
313
+ * @param {TrackEventOptions} event The options related to the custom event.
314
+ */
315
+ function track(event) {
316
+ if (!LIB_INSTANCE)
317
+ return;
318
+ LIB_INSTANCE.track(event);
319
+ }
320
+ /**
321
+ * With this function you are able to track any custom events you want.
322
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
323
+ * The total number of track calls and their conversion rate will be saved.
324
+ *
325
+ * @param {PageViewsOptions} options The options related to the custom event.
326
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
327
+ */
328
+ function trackViews(options) {
329
+ return new Promise(function (resolve) {
330
+ if (!LIB_INSTANCE) {
331
+ resolve(defaultPageActions);
332
+ return;
333
+ }
334
+ // We need to verify that document.readyState is complete for the performance stats to be collected correctly.
335
+ if (typeof document === 'undefined' || document.readyState === 'complete') {
336
+ resolve(LIB_INSTANCE.trackPageViews(options));
337
+ }
338
+ else {
339
+ window.addEventListener('load', function () {
340
+ // @ts-ignore
341
+ resolve(LIB_INSTANCE.trackPageViews(options));
342
+ });
343
+ }
344
+ });
336
345
  }
337
346
 
338
347
  export { LIB_INSTANCE, init, track, trackViews };