swetrix 2.3.1 → 2.4.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.
@@ -54,6 +54,16 @@ export interface PageViewsOptions {
54
54
  heartbeatOnBackground?: boolean;
55
55
  /** Disable user-flow */
56
56
  noUserFlow?: boolean;
57
+ /**
58
+ * Set to `true` to enable hash-based routing.
59
+ * For example if you have pages like /#/path or want to track pages like /path#hash
60
+ */
61
+ hash?: boolean;
62
+ /**
63
+ * Set to `true` to enable search-based routing.
64
+ * For example if you have pages like /path?search
65
+ */
66
+ search?: boolean;
57
67
  }
58
68
  export declare const defaultPageActions: {
59
69
  stop(): void;
@@ -74,6 +84,7 @@ export declare class Lib {
74
84
  private trackPathChange;
75
85
  private getPreviousPage;
76
86
  private trackPage;
87
+ submitPageView(pg: null | string, prev: string | null | undefined, unique: boolean, perf: any): void;
77
88
  private debug;
78
89
  private canTrack;
79
90
  private sendRequest;
@@ -47,7 +47,10 @@ export class Lib {
47
47
  setTimeout(this.heartbeat, 3000);
48
48
  hbInterval = setInterval(this.heartbeat, 28000);
49
49
  }
50
- const path = getPath();
50
+ const path = getPath({
51
+ hash: options?.hash,
52
+ search: options?.search,
53
+ });
51
54
  this.pageData = {
52
55
  path,
53
56
  actions: {
@@ -117,7 +120,10 @@ export class Lib {
117
120
  trackPathChange() {
118
121
  if (!this.pageData)
119
122
  return;
120
- const newPath = getPath();
123
+ const newPath = getPath({
124
+ hash: this.pageViewsOptions?.hash,
125
+ search: this.pageViewsOptions?.search,
126
+ });
121
127
  const { path } = this.pageData;
122
128
  if (path !== newPath) {
123
129
  this.trackPage(newPath, false);
@@ -171,6 +177,10 @@ export class Lib {
171
177
  if (!this.pageViewsOptions?.noUserFlow) {
172
178
  prev = this.getPreviousPage();
173
179
  }
180
+ this.activePage = pg;
181
+ this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf);
182
+ }
183
+ submitPageView(pg, prev, unique, perf) {
174
184
  const data = {
175
185
  pid: this.projectID,
176
186
  lc: getLocale(),
@@ -180,11 +190,10 @@ export class Lib {
180
190
  me: getUTMMedium(),
181
191
  ca: getUTMCampaign(),
182
192
  unique,
183
- pg: shouldIgnore ? null : pg,
193
+ pg,
184
194
  perf,
185
195
  prev,
186
196
  };
187
- this.activePage = pg;
188
197
  this.sendRequest('', data);
189
198
  }
190
199
  debug(message) {
@@ -1 +1 @@
1
- {"version":3,"file":"Lib.js","sourceRoot":"","sources":["../../src/Lib.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAC1E,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,OAAO,GACpD,MAAM,SAAS,CAAA;AA2EhB,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,IAAI,KAAI,CAAC;CACV,CAAA;AAED,MAAM,gBAAgB,GAAG,6BAA6B,CAAA;AAEtD,MAAM,OAAO,GAAG;IAMd,YAAoB,SAAiB,EAAU,OAAoB;QAA/C,cAAS,GAAT,SAAS,CAAQ;QAAU,YAAO,GAAP,OAAO,CAAa;QAL3D,aAAQ,GAAoB,IAAI,CAAA;QAChC,qBAAgB,GAAwC,IAAI,CAAA;QAC5D,uBAAkB,GAAY,KAAK,CAAA;QACnC,eAAU,GAAkB,IAAI,CAAA;QAGtC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACtD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC5C,CAAC;IAED,KAAK,CAAC,KAAwB;QAC5B,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE;YACpB,OAAM;SACP;QAED,MAAM,IAAI,GAAG;YACX,GAAG,EAAE,IAAI,CAAC,SAAS;YACnB,EAAE,EAAE,IAAI,CAAC,UAAU;YACnB,EAAE,EAAE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AAC,CAAA;IAChC,CAAC;CACF"}
@@ -25,3 +25,13 @@ export declare function track(event: TrackEventOptions): void;
25
25
  * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
26
26
  */
27
27
  export declare function trackViews(options?: PageViewsOptions): Promise<PageActions>;
28
+ /**
29
+ * This function is used to manually track a page view event.
30
+ * It's useful if your application uses esoteric routing which is not supported by Swetrix by default.
31
+ *
32
+ * @param path Path of the page to track (this will be sent to the Swetrix API and displayed in the dashboard).
33
+ * @param prev Path of the previous page.
34
+ * @param unique If set to `true`, only 1 event with the same ID will be saved per user session.
35
+ * @returns void
36
+ */
37
+ export declare function trackPageview(path: string, prev?: string, unique?: boolean): void;
@@ -51,4 +51,18 @@ export function trackViews(options) {
51
51
  }
52
52
  });
53
53
  }
54
+ /**
55
+ * This function is used to manually track a page view event.
56
+ * It's useful if your application uses esoteric routing which is not supported by Swetrix by default.
57
+ *
58
+ * @param path Path of the page to track (this will be sent to the Swetrix API and displayed in the dashboard).
59
+ * @param prev Path of the previous page.
60
+ * @param unique If set to `true`, only 1 event with the same ID will be saved per user session.
61
+ * @returns void
62
+ */
63
+ export function trackPageview(path, prev, unique) {
64
+ if (!LIB_INSTANCE)
65
+ return;
66
+ LIB_INSTANCE.submitPageView(path, prev || null, Boolean(unique), {});
67
+ }
54
68
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAAgE,kBAAkB,GACtF,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;QACjB,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;KACrC;IAED,OAAO,YAAY,CAAA;AACrB,CAAC;AAED;;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AAA;AAC3B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAC,OAA0B;IACnD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,IAAI,CAAC,YAAY,EAAE;YACjB,OAAO,CAAC,kBAAkB,CAAC,CAAA;YAC3B,OAAM;SACP;QAED,8GAA8G;QAC9G,IAAI,OAAO,QAAQ,KAAK,WAAW,IAAI,QAAQ,CAAC,UAAU,KAAK,UAAU,EAAE;YACzE,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAA;SAC9C;aAAM;YACL,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;SACH;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY,EAAE,IAAa,EAAE,MAAgB;IACzE,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,cAAc,CAAC,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CAAA;AACtE,CAAC"}
@@ -1,3 +1,7 @@
1
+ interface IGetPath {
2
+ hash?: boolean;
3
+ search?: boolean;
4
+ }
1
5
  export declare const isInBrowser: () => boolean;
2
6
  export declare const isLocalhost: () => boolean;
3
7
  export declare const isAutomated: () => boolean;
@@ -7,4 +11,19 @@ export declare const getReferrer: () => string | undefined;
7
11
  export declare const getUTMSource: () => string | undefined;
8
12
  export declare const getUTMMedium: () => string | undefined;
9
13
  export declare const getUTMCampaign: () => string | undefined;
10
- export declare const getPath: () => string;
14
+ /**
15
+ * Function used to track the current page (path) of the application.
16
+ * Will work in cases where the path looks like:
17
+ * - /path
18
+ * - /#/path
19
+ * - /path?search
20
+ * - /path?search#hash
21
+ * - /path#hash?search
22
+ *
23
+ * @param options - Options for the function.
24
+ * @param options.hash - Whether to trigger on hash change.
25
+ * @param options.search - Whether to trigger on search change.
26
+ * @returns The path of the current page.
27
+ */
28
+ export declare const getPath: (options: IGetPath) => string;
29
+ export {};
@@ -31,8 +31,32 @@ export const getReferrer = () => {
31
31
  export const getUTMSource = () => findInSearch(utmSourceRegex);
32
32
  export const getUTMMedium = () => findInSearch(utmMediumRegex);
33
33
  export const getUTMCampaign = () => findInSearch(utmCampaignRegex);
34
- export const getPath = () => {
35
- // TODO: Maybe we should also include such data as location.hash or location.search
36
- return location.pathname || '';
34
+ /**
35
+ * Function used to track the current page (path) of the application.
36
+ * Will work in cases where the path looks like:
37
+ * - /path
38
+ * - /#/path
39
+ * - /path?search
40
+ * - /path?search#hash
41
+ * - /path#hash?search
42
+ *
43
+ * @param options - Options for the function.
44
+ * @param options.hash - Whether to trigger on hash change.
45
+ * @param options.search - Whether to trigger on search change.
46
+ * @returns The path of the current page.
47
+ */
48
+ export const getPath = (options) => {
49
+ let result = location.pathname || '';
50
+ if (options.hash) {
51
+ const hashIndex = location.hash.indexOf('?');
52
+ const hashString = hashIndex > -1 ? location.hash.substring(0, hashIndex) : location.hash;
53
+ result += hashString;
54
+ }
55
+ if (options.search) {
56
+ const hashIndex = location.hash.indexOf('?');
57
+ const searchString = location.search || (hashIndex > -1 ? location.hash.substring(hashIndex) : '');
58
+ result += searchString;
59
+ }
60
+ return result;
37
61
  };
38
62
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,CAAC,GAAW,EAAsB,EAAE;IACvD,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACtC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IA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AAM,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;QAClG,MAAM,IAAI,YAAY,CAAA;KACvB;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
@@ -66,9 +66,33 @@ var getReferrer = function () {
66
66
  var getUTMSource = function () { return findInSearch(utmSourceRegex); };
67
67
  var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
68
68
  var getUTMCampaign = function () { return findInSearch(utmCampaignRegex); };
69
- var getPath = function () {
70
- // TODO: Maybe we should also include such data as location.hash or location.search
71
- return location.pathname || '';
69
+ /**
70
+ * Function used to track the current page (path) of the application.
71
+ * Will work in cases where the path looks like:
72
+ * - /path
73
+ * - /#/path
74
+ * - /path?search
75
+ * - /path?search#hash
76
+ * - /path#hash?search
77
+ *
78
+ * @param options - Options for the function.
79
+ * @param options.hash - Whether to trigger on hash change.
80
+ * @param options.search - Whether to trigger on search change.
81
+ * @returns The path of the current page.
82
+ */
83
+ var getPath = function (options) {
84
+ var result = location.pathname || '';
85
+ if (options.hash) {
86
+ var hashIndex = location.hash.indexOf('?');
87
+ var hashString = hashIndex > -1 ? location.hash.substring(0, hashIndex) : location.hash;
88
+ result += hashString;
89
+ }
90
+ if (options.search) {
91
+ var hashIndex = location.hash.indexOf('?');
92
+ var searchString = location.search || (hashIndex > -1 ? location.hash.substring(hashIndex) : '');
93
+ result += searchString;
94
+ }
95
+ return result;
72
96
  };
73
97
 
74
98
  var defaultPageActions = {
@@ -109,7 +133,10 @@ var Lib = /** @class */ (function () {
109
133
  setTimeout(this.heartbeat, 3000);
110
134
  hbInterval = setInterval(this.heartbeat, 28000);
111
135
  }
112
- var path = getPath();
136
+ var path = getPath({
137
+ hash: options === null || options === void 0 ? void 0 : options.hash,
138
+ search: options === null || options === void 0 ? void 0 : options.search,
139
+ });
113
140
  this.pageData = {
114
141
  path: path,
115
142
  actions: {
@@ -180,9 +207,13 @@ var Lib = /** @class */ (function () {
180
207
  };
181
208
  // Tracking path changes. If path changes -> calling this.trackPage method
182
209
  Lib.prototype.trackPathChange = function () {
210
+ var _a, _b;
183
211
  if (!this.pageData)
184
212
  return;
185
- var newPath = getPath();
213
+ var newPath = getPath({
214
+ hash: (_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.hash,
215
+ search: (_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.search,
216
+ });
186
217
  var path = this.pageData.path;
187
218
  if (path !== newPath) {
188
219
  this.trackPage(newPath, false);
@@ -239,6 +270,10 @@ var Lib = /** @class */ (function () {
239
270
  if (!((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.noUserFlow)) {
240
271
  prev = this.getPreviousPage();
241
272
  }
273
+ this.activePage = pg;
274
+ this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf);
275
+ };
276
+ Lib.prototype.submitPageView = function (pg, prev, unique, perf) {
242
277
  var data = {
243
278
  pid: this.projectID,
244
279
  lc: getLocale(),
@@ -248,11 +283,10 @@ var Lib = /** @class */ (function () {
248
283
  me: getUTMMedium(),
249
284
  ca: getUTMCampaign(),
250
285
  unique: unique,
251
- pg: shouldIgnore ? null : pg,
286
+ pg: pg,
252
287
  perf: perf,
253
288
  prev: prev,
254
289
  };
255
- this.activePage = pg;
256
290
  this.sendRequest('', data);
257
291
  };
258
292
  Lib.prototype.debug = function (message) {
@@ -347,7 +381,22 @@ function trackViews(options) {
347
381
  }
348
382
  });
349
383
  }
384
+ /**
385
+ * This function is used to manually track a page view event.
386
+ * It's useful if your application uses esoteric routing which is not supported by Swetrix by default.
387
+ *
388
+ * @param path Path of the page to track (this will be sent to the Swetrix API and displayed in the dashboard).
389
+ * @param prev Path of the previous page.
390
+ * @param unique If set to `true`, only 1 event with the same ID will be saved per user session.
391
+ * @returns void
392
+ */
393
+ function trackPageview(path, prev, unique) {
394
+ if (!exports.LIB_INSTANCE)
395
+ return;
396
+ exports.LIB_INSTANCE.submitPageView(path, prev || null, Boolean(unique), {});
397
+ }
350
398
 
351
399
  exports.init = init;
352
400
  exports.track = track;
401
+ exports.trackPageview = trackPageview;
353
402
  exports.trackViews = trackViews;
@@ -62,9 +62,33 @@ var getReferrer = function () {
62
62
  var getUTMSource = function () { return findInSearch(utmSourceRegex); };
63
63
  var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
64
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 || '';
65
+ /**
66
+ * Function used to track the current page (path) of the application.
67
+ * Will work in cases where the path looks like:
68
+ * - /path
69
+ * - /#/path
70
+ * - /path?search
71
+ * - /path?search#hash
72
+ * - /path#hash?search
73
+ *
74
+ * @param options - Options for the function.
75
+ * @param options.hash - Whether to trigger on hash change.
76
+ * @param options.search - Whether to trigger on search change.
77
+ * @returns The path of the current page.
78
+ */
79
+ var getPath = function (options) {
80
+ var result = location.pathname || '';
81
+ if (options.hash) {
82
+ var hashIndex = location.hash.indexOf('?');
83
+ var hashString = hashIndex > -1 ? location.hash.substring(0, hashIndex) : location.hash;
84
+ result += hashString;
85
+ }
86
+ if (options.search) {
87
+ var hashIndex = location.hash.indexOf('?');
88
+ var searchString = location.search || (hashIndex > -1 ? location.hash.substring(hashIndex) : '');
89
+ result += searchString;
90
+ }
91
+ return result;
68
92
  };
69
93
 
70
94
  var defaultPageActions = {
@@ -105,7 +129,10 @@ var Lib = /** @class */ (function () {
105
129
  setTimeout(this.heartbeat, 3000);
106
130
  hbInterval = setInterval(this.heartbeat, 28000);
107
131
  }
108
- var path = getPath();
132
+ var path = getPath({
133
+ hash: options === null || options === void 0 ? void 0 : options.hash,
134
+ search: options === null || options === void 0 ? void 0 : options.search,
135
+ });
109
136
  this.pageData = {
110
137
  path: path,
111
138
  actions: {
@@ -176,9 +203,13 @@ var Lib = /** @class */ (function () {
176
203
  };
177
204
  // Tracking path changes. If path changes -> calling this.trackPage method
178
205
  Lib.prototype.trackPathChange = function () {
206
+ var _a, _b;
179
207
  if (!this.pageData)
180
208
  return;
181
- var newPath = getPath();
209
+ var newPath = getPath({
210
+ hash: (_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.hash,
211
+ search: (_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.search,
212
+ });
182
213
  var path = this.pageData.path;
183
214
  if (path !== newPath) {
184
215
  this.trackPage(newPath, false);
@@ -235,6 +266,10 @@ var Lib = /** @class */ (function () {
235
266
  if (!((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.noUserFlow)) {
236
267
  prev = this.getPreviousPage();
237
268
  }
269
+ this.activePage = pg;
270
+ this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf);
271
+ };
272
+ Lib.prototype.submitPageView = function (pg, prev, unique, perf) {
238
273
  var data = {
239
274
  pid: this.projectID,
240
275
  lc: getLocale(),
@@ -244,11 +279,10 @@ var Lib = /** @class */ (function () {
244
279
  me: getUTMMedium(),
245
280
  ca: getUTMCampaign(),
246
281
  unique: unique,
247
- pg: shouldIgnore ? null : pg,
282
+ pg: pg,
248
283
  perf: perf,
249
284
  prev: prev,
250
285
  };
251
- this.activePage = pg;
252
286
  this.sendRequest('', data);
253
287
  };
254
288
  Lib.prototype.debug = function (message) {
@@ -343,5 +377,19 @@ function trackViews(options) {
343
377
  }
344
378
  });
345
379
  }
380
+ /**
381
+ * This function is used to manually track a page view event.
382
+ * It's useful if your application uses esoteric routing which is not supported by Swetrix by default.
383
+ *
384
+ * @param path Path of the page to track (this will be sent to the Swetrix API and displayed in the dashboard).
385
+ * @param prev Path of the previous page.
386
+ * @param unique If set to `true`, only 1 event with the same ID will be saved per user session.
387
+ * @returns void
388
+ */
389
+ function trackPageview(path, prev, unique) {
390
+ if (!LIB_INSTANCE)
391
+ return;
392
+ LIB_INSTANCE.submitPageView(path, prev || null, Boolean(unique), {});
393
+ }
346
394
 
347
- export { LIB_INSTANCE, init, track, trackViews };
395
+ export { LIB_INSTANCE, init, track, trackPageview, trackViews };
package/dist/swetrix.js CHANGED
@@ -1 +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(n){"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 s(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){}}function c(){return document.referrer||void 0}function u(){return t(o)}function l(){return t(d)}function p(){return t(a)}function i(){return location.pathname||""}var e=function(){return(e=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)},o=/[?&](ref|source|utm_source)=([^?&]+)/,a=/[?&](utm_campaign)=([^?&]+)/,d=/[?&](utm_medium)=([^?&]+)/,h={stop:function(){}},g=(f.prototype.track=function(t){this.canTrack()&&(t=e({pid:this.projectID,pg:this.activePage,lc:r(),tz:s(),ref:c(),so:u(),me:l(),ca:p()},t),this.sendRequest("custom",t))},f.prototype.trackPageViews=function(t){var e,n,o;return this.canTrack()?(this.pageData||(null!=(this.pageViewsOptions=t)&&t.unique||(n=setInterval(this.trackPathChange,2e3)),null!=t&&t.noHeartbeat||(setTimeout(this.heartbeat,3e3),e=setInterval(this.heartbeat,28e3)),o=i(),this.pageData={path:o,actions:{stop:function(){clearInterval(n),clearInterval(e)}}},this.trackPage(o,null==t?void 0:t.unique)),this.pageData.actions):h},f.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}):{}},f.prototype.heartbeat=function(){var t;(null!=(t=this.pageViewsOptions)&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},f.prototype.checkIgnore=function(t){var e,n=null==(e=this.pageViewsOptions)?void 0:e.ignore;if(Array.isArray(n))for(var o=0;o<n.length;++o){if(n[o]===t)return!0;if(n[o]instanceof RegExp&&n[o].test(t))return!0}return!1},f.prototype.trackPathChange=function(){var t;this.pageData&&(t=i(),this.pageData.path!==t)&&this.trackPage(t,!1)},f.prototype.getPreviousPage=function(){var t;if(this.activePage)return(o=this.checkIgnore(this.activePage))&&null!=(e=this.pageViewsOptions)&&e.doNotAnonymise||o?null:this.activePage;if("function"==typeof URL){var e=c();if(!e)return null;var n=location.host;try{var o,i=new URL(e),a=i.host,r=i.pathname;return n!==a?null:(o=this.checkIgnore(r))&&null!=(t=this.pageViewsOptions)&&t.doNotAnonymise||o?null:r}catch(t){}}return null},f.prototype.trackPage=function(t,e){var n,o,i,a;void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,(n=this.checkIgnore(t))&&null!=(o=this.pageViewsOptions)&&o.doNotAnonymise||(o=this.getPerformanceStats(),null!=(a=this.pageViewsOptions)&&a.noUserFlow||(i=this.getPreviousPage()),a={pid:this.projectID,lc:r(),tz:s(),ref:c(),so:u(),me:l(),ca:p(),unique:e,pg:n?null:t,perf:o,prev:i},this.activePage=t,this.sendRequest("",a)))},f.prototype.debug=function(t){var e;null!=(e=this.options)&&e.debug&&console.log("[Swetrix]",t)},f.prototype.canTrack=function(){var t;return null!=(t=this.options)&&t.disabled?(this.debug("Tracking disabled: the 'disabled' setting is set to true."),!1):"undefined"==typeof window?(this.debug("Tracking disabled: script does not run in browser environment."),!1):null!=(t=this.options)&&t.respectDNT&&"1"===(null==(t=window.navigator)?void 0:t.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!(!(null!=(t=this.options)&&t.debug||"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&&(this.debug("Tracking disabled: navigation is automated by WebDriver."),1))},f.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))},f);function f(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.perfStatsCollected=!1,this.activePage=null,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this)}n.LIB_INSTANCE=null,n.init=function(t,e){return n.LIB_INSTANCE||(n.LIB_INSTANCE=new g(t,e)),n.LIB_INSTANCE},n.track=function(t){n.LIB_INSTANCE&&n.LIB_INSTANCE.track(t)},n.trackViews=function(e){return new Promise(function(t){n.LIB_INSTANCE?"undefined"==typeof document||"complete"===document.readyState?t(n.LIB_INSTANCE.trackPageViews(e)):window.addEventListener("load",function(){t(n.LIB_INSTANCE.trackPageViews(e))}):t(h)})},Object.defineProperty(n,"__esModule",{value:!0})});
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 i(){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 r(){return t(n)}function c(){return t(p)}function u(){return t(h)}function l(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||(-1<e?location.hash.substring(e):"")),n}var e=function(){return(e=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)},n=/[?&](ref|source|utm_source)=([^?&]+)/,h=/[?&](utm_campaign)=([^?&]+)/,p=/[?&](utm_medium)=([^?&]+)/,d={stop:function(){}},g=(f.prototype.track=function(t){this.canTrack()&&(t=e({pid:this.projectID,pg:this.activePage,lc:i(),tz:a(),ref:s(),so:r(),me:c(),ca:u()},t),this.sendRequest("custom",t))},f.prototype.trackPageViews=function(t){var e,n,o;return this.canTrack()?(this.pageData||(null!=(this.pageViewsOptions=t)&&t.unique||(n=setInterval(this.trackPathChange,2e3)),null!=t&&t.noHeartbeat||(setTimeout(this.heartbeat,3e3),e=setInterval(this.heartbeat,28e3)),o=l({hash:null==t?void 0:t.hash,search:null==t?void 0:t.search}),this.pageData={path:o,actions:{stop:function(){clearInterval(n),clearInterval(e)}}},this.trackPage(o,null==t?void 0:t.unique)),this.pageData.actions):d},f.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}):{}},f.prototype.heartbeat=function(){var t;(null!=(t=this.pageViewsOptions)&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},f.prototype.checkIgnore=function(t){var e,n=null==(e=this.pageViewsOptions)?void 0:e.ignore;if(Array.isArray(n))for(var o=0;o<n.length;++o){if(n[o]===t)return!0;if(n[o]instanceof RegExp&&n[o].test(t))return!0}return!1},f.prototype.trackPathChange=function(){var t;this.pageData&&(t=l({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)},f.prototype.getPreviousPage=function(){var t;if(this.activePage)return(o=this.checkIgnore(this.activePage))&&null!=(e=this.pageViewsOptions)&&e.doNotAnonymise||o?null:this.activePage;if("function"==typeof URL){var e=s();if(!e)return null;var n=location.host;try{var o,i=new URL(e),a=i.host,r=i.pathname;return n!==a?null:(o=this.checkIgnore(r))&&null!=(t=this.pageViewsOptions)&&t.doNotAnonymise||o?null:r}catch(t){}}return null},f.prototype.trackPage=function(t,e){var n,o,i,a;void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,(o=this.checkIgnore(t))&&null!=(i=this.pageViewsOptions)&&i.doNotAnonymise||(i=this.getPerformanceStats(),null!=(n=this.pageViewsOptions)&&n.noUserFlow||(a=this.getPreviousPage()),this.activePage=t,this.submitPageView(o?null:t,a,e,i)))},f.prototype.submitPageView=function(t,e,n,o){n={pid:this.projectID,lc:i(),tz:a(),ref:s(),so:r(),me:c(),ca:u(),unique:n,pg:t,perf:o,prev:e};this.sendRequest("",n)},f.prototype.debug=function(t){var e;null!=(e=this.options)&&e.debug&&console.log("[Swetrix]",t)},f.prototype.canTrack=function(){var t;return null!=(t=this.options)&&t.disabled?(this.debug("Tracking disabled: the 'disabled' setting is set to true."),!1):"undefined"==typeof window?(this.debug("Tracking disabled: script does not run in browser environment."),!1):null!=(t=this.options)&&t.respectDNT&&"1"===(null==(t=window.navigator)?void 0:t.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!(!(null!=(t=this.options)&&t.debug||"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&&(this.debug("Tracking disabled: navigation is automated by WebDriver."),1))},f.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))},f);function f(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.perfStatsCollected=!1,this.activePage=null,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this)}o.LIB_INSTANCE=null,o.init=function(t,e){return o.LIB_INSTANCE||(o.LIB_INSTANCE=new g(t,e)),o.LIB_INSTANCE},o.track=function(t){o.LIB_INSTANCE&&o.LIB_INSTANCE.track(t)},o.trackPageview=function(t,e,n){o.LIB_INSTANCE&&o.LIB_INSTANCE.submitPageView(t,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})});
@@ -68,9 +68,33 @@
68
68
  var getUTMSource = function () { return findInSearch(utmSourceRegex); };
69
69
  var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
70
70
  var getUTMCampaign = function () { return findInSearch(utmCampaignRegex); };
71
- var getPath = function () {
72
- // TODO: Maybe we should also include such data as location.hash or location.search
73
- return location.pathname || '';
71
+ /**
72
+ * Function used to track the current page (path) of the application.
73
+ * Will work in cases where the path looks like:
74
+ * - /path
75
+ * - /#/path
76
+ * - /path?search
77
+ * - /path?search#hash
78
+ * - /path#hash?search
79
+ *
80
+ * @param options - Options for the function.
81
+ * @param options.hash - Whether to trigger on hash change.
82
+ * @param options.search - Whether to trigger on search change.
83
+ * @returns The path of the current page.
84
+ */
85
+ var getPath = function (options) {
86
+ var result = location.pathname || '';
87
+ if (options.hash) {
88
+ var hashIndex = location.hash.indexOf('?');
89
+ var hashString = hashIndex > -1 ? location.hash.substring(0, hashIndex) : location.hash;
90
+ result += hashString;
91
+ }
92
+ if (options.search) {
93
+ var hashIndex = location.hash.indexOf('?');
94
+ var searchString = location.search || (hashIndex > -1 ? location.hash.substring(hashIndex) : '');
95
+ result += searchString;
96
+ }
97
+ return result;
74
98
  };
75
99
 
76
100
  var defaultPageActions = {
@@ -111,7 +135,10 @@
111
135
  setTimeout(this.heartbeat, 3000);
112
136
  hbInterval = setInterval(this.heartbeat, 28000);
113
137
  }
114
- var path = getPath();
138
+ var path = getPath({
139
+ hash: options === null || options === void 0 ? void 0 : options.hash,
140
+ search: options === null || options === void 0 ? void 0 : options.search,
141
+ });
115
142
  this.pageData = {
116
143
  path: path,
117
144
  actions: {
@@ -182,9 +209,13 @@
182
209
  };
183
210
  // Tracking path changes. If path changes -> calling this.trackPage method
184
211
  Lib.prototype.trackPathChange = function () {
212
+ var _a, _b;
185
213
  if (!this.pageData)
186
214
  return;
187
- var newPath = getPath();
215
+ var newPath = getPath({
216
+ hash: (_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.hash,
217
+ search: (_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.search,
218
+ });
188
219
  var path = this.pageData.path;
189
220
  if (path !== newPath) {
190
221
  this.trackPage(newPath, false);
@@ -241,6 +272,10 @@
241
272
  if (!((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.noUserFlow)) {
242
273
  prev = this.getPreviousPage();
243
274
  }
275
+ this.activePage = pg;
276
+ this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf);
277
+ };
278
+ Lib.prototype.submitPageView = function (pg, prev, unique, perf) {
244
279
  var data = {
245
280
  pid: this.projectID,
246
281
  lc: getLocale(),
@@ -250,11 +285,10 @@
250
285
  me: getUTMMedium(),
251
286
  ca: getUTMCampaign(),
252
287
  unique: unique,
253
- pg: shouldIgnore ? null : pg,
288
+ pg: pg,
254
289
  perf: perf,
255
290
  prev: prev,
256
291
  };
257
- this.activePage = pg;
258
292
  this.sendRequest('', data);
259
293
  };
260
294
  Lib.prototype.debug = function (message) {
@@ -349,9 +383,24 @@
349
383
  }
350
384
  });
351
385
  }
386
+ /**
387
+ * This function is used to manually track a page view event.
388
+ * It's useful if your application uses esoteric routing which is not supported by Swetrix by default.
389
+ *
390
+ * @param path Path of the page to track (this will be sent to the Swetrix API and displayed in the dashboard).
391
+ * @param prev Path of the previous page.
392
+ * @param unique If set to `true`, only 1 event with the same ID will be saved per user session.
393
+ * @returns void
394
+ */
395
+ function trackPageview(path, prev, unique) {
396
+ if (!exports.LIB_INSTANCE)
397
+ return;
398
+ exports.LIB_INSTANCE.submitPageView(path, prev || null, Boolean(unique), {});
399
+ }
352
400
 
353
401
  exports.init = init;
354
402
  exports.track = track;
403
+ exports.trackPageview = trackPageview;
355
404
  exports.trackViews = trackViews;
356
405
 
357
406
  Object.defineProperty(exports, '__esModule', { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swetrix",
3
- "version": "2.3.1",
3
+ "version": "2.4.0",
4
4
  "description": "The JavaScript analytics client for Swetrix Analytics",
5
5
  "main": "dist/swetrix.cjs.js",
6
6
  "module": "dist/swetrix.es5.js",
package/src/Lib.ts CHANGED
@@ -74,6 +74,18 @@ export interface PageViewsOptions {
74
74
 
75
75
  /** Disable user-flow */
76
76
  noUserFlow?: boolean
77
+
78
+ /**
79
+ * Set to `true` to enable hash-based routing.
80
+ * For example if you have pages like /#/path or want to track pages like /path#hash
81
+ */
82
+ hash?: boolean
83
+
84
+ /**
85
+ * Set to `true` to enable search-based routing.
86
+ * For example if you have pages like /path?search
87
+ */
88
+ search?: boolean
77
89
  }
78
90
 
79
91
  export const defaultPageActions = {
@@ -132,7 +144,10 @@ export class Lib {
132
144
  hbInterval = setInterval(this.heartbeat, 28000)
133
145
  }
134
146
 
135
- const path = getPath()
147
+ const path = getPath({
148
+ hash: options?.hash,
149
+ search: options?.search,
150
+ })
136
151
 
137
152
  this.pageData = {
138
153
  path,
@@ -214,7 +229,10 @@ export class Lib {
214
229
  // Tracking path changes. If path changes -> calling this.trackPage method
215
230
  private trackPathChange(): void {
216
231
  if (!this.pageData) return
217
- const newPath = getPath()
232
+ const newPath = getPath({
233
+ hash: this.pageViewsOptions?.hash,
234
+ search: this.pageViewsOptions?.search,
235
+ })
218
236
  const { path } = this.pageData
219
237
 
220
238
  if (path !== newPath) {
@@ -285,6 +303,11 @@ export class Lib {
285
303
  prev = this.getPreviousPage()
286
304
  }
287
305
 
306
+ this.activePage = pg
307
+ this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf)
308
+ }
309
+
310
+ submitPageView(pg: null | string, prev: string | null | undefined, unique: boolean, perf: any): void {
288
311
  const data = {
289
312
  pid: this.projectID,
290
313
  lc: getLocale(),
@@ -294,12 +317,11 @@ export class Lib {
294
317
  me: getUTMMedium(),
295
318
  ca: getUTMCampaign(),
296
319
  unique,
297
- pg: shouldIgnore ? null : pg,
320
+ pg,
298
321
  perf,
299
322
  prev,
300
323
  }
301
324
 
302
- this.activePage = pg
303
325
  this.sendRequest('', data)
304
326
  }
305
327
 
package/src/index.ts CHANGED
@@ -58,3 +58,18 @@ export function trackViews(options?: PageViewsOptions): Promise<PageActions> {
58
58
  }
59
59
  })
60
60
  }
61
+
62
+ /**
63
+ * This function is used to manually track a page view event.
64
+ * It's useful if your application uses esoteric routing which is not supported by Swetrix by default.
65
+ *
66
+ * @param path Path of the page to track (this will be sent to the Swetrix API and displayed in the dashboard).
67
+ * @param prev Path of the previous page.
68
+ * @param unique If set to `true`, only 1 event with the same ID will be saved per user session.
69
+ * @returns void
70
+ */
71
+ export function trackPageview(path: string, prev?: string, unique?: boolean): void {
72
+ if (!LIB_INSTANCE) return
73
+
74
+ LIB_INSTANCE.submitPageView(path, prev || null, Boolean(unique), {})
75
+ }
package/src/utils.ts CHANGED
@@ -1,3 +1,8 @@
1
+ interface IGetPath {
2
+ hash?: boolean
3
+ search?: boolean
4
+ }
5
+
1
6
  const findInSearch = (exp: RegExp): string | undefined => {
2
7
  const res = location.search.match(exp)
3
8
  return (res && res[2]) || undefined
@@ -41,7 +46,34 @@ export const getUTMMedium = () => findInSearch(utmMediumRegex)
41
46
 
42
47
  export const getUTMCampaign = () => findInSearch(utmCampaignRegex)
43
48
 
44
- export const getPath = (): string => {
45
- // TODO: Maybe we should also include such data as location.hash or location.search
46
- return location.pathname || ''
49
+ /**
50
+ * Function used to track the current page (path) of the application.
51
+ * Will work in cases where the path looks like:
52
+ * - /path
53
+ * - /#/path
54
+ * - /path?search
55
+ * - /path?search#hash
56
+ * - /path#hash?search
57
+ *
58
+ * @param options - Options for the function.
59
+ * @param options.hash - Whether to trigger on hash change.
60
+ * @param options.search - Whether to trigger on search change.
61
+ * @returns The path of the current page.
62
+ */
63
+ export const getPath = (options: IGetPath): string => {
64
+ let result = location.pathname || ''
65
+
66
+ if (options.hash) {
67
+ const hashIndex = location.hash.indexOf('?')
68
+ const hashString = hashIndex > -1 ? location.hash.substring(0, hashIndex) : location.hash
69
+ result += hashString
70
+ }
71
+
72
+ if (options.search) {
73
+ const hashIndex = location.hash.indexOf('?')
74
+ const searchString = location.search || (hashIndex > -1 ? location.hash.substring(hashIndex) : '')
75
+ result += searchString
76
+ }
77
+
78
+ return result
47
79
  }