swetrix 2.0.1 → 2.2.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.
- package/.github/funding.yml +1 -0
- package/dist/esnext/Lib.d.ts +4 -0
- package/dist/esnext/Lib.js +42 -0
- package/dist/esnext/Lib.js.map +1 -1
- package/dist/swetrix.cjs.js +37 -1
- package/dist/swetrix.es5.js +37 -1
- package/dist/swetrix.js +1 -1
- package/dist/swetrix.orig.js +37 -1
- package/package.json +6 -6
- package/src/Lib.ts +54 -0
package/.github/funding.yml
CHANGED
package/dist/esnext/Lib.d.ts
CHANGED
|
@@ -46,6 +46,8 @@ export interface PageViewsOptions {
|
|
|
46
46
|
noHeartbeat?: boolean;
|
|
47
47
|
/** Send Heartbeat requests when the website tab is not active in the browser. */
|
|
48
48
|
heartbeatOnBackground?: boolean;
|
|
49
|
+
/** Disable user-flow */
|
|
50
|
+
noUserFlow?: boolean;
|
|
49
51
|
}
|
|
50
52
|
export declare const defaultPageActions: {
|
|
51
53
|
stop(): void;
|
|
@@ -56,6 +58,7 @@ export declare class Lib {
|
|
|
56
58
|
private pageData;
|
|
57
59
|
private pageViewsOptions;
|
|
58
60
|
private perfStatsCollected;
|
|
61
|
+
private activePage;
|
|
59
62
|
constructor(projectID: string, options?: LibOptions | undefined);
|
|
60
63
|
track(event: TrackEventOptions): void;
|
|
61
64
|
trackPageViews(options?: PageViewsOptions): PageActions;
|
|
@@ -63,6 +66,7 @@ export declare class Lib {
|
|
|
63
66
|
private heartbeat;
|
|
64
67
|
private checkIgnore;
|
|
65
68
|
private trackPathChange;
|
|
69
|
+
private getPreviousPage;
|
|
66
70
|
private trackPage;
|
|
67
71
|
private debug;
|
|
68
72
|
private canTrack;
|
package/dist/esnext/Lib.js
CHANGED
|
@@ -10,6 +10,7 @@ export class Lib {
|
|
|
10
10
|
this.pageData = null;
|
|
11
11
|
this.pageViewsOptions = null;
|
|
12
12
|
this.perfStatsCollected = false;
|
|
13
|
+
this.activePage = null;
|
|
13
14
|
this.trackPathChange = this.trackPathChange.bind(this);
|
|
14
15
|
this.heartbeat = this.heartbeat.bind(this);
|
|
15
16
|
}
|
|
@@ -19,6 +20,13 @@ export class Lib {
|
|
|
19
20
|
}
|
|
20
21
|
const data = {
|
|
21
22
|
pid: this.projectID,
|
|
23
|
+
pg: this.activePage,
|
|
24
|
+
lc: getLocale(),
|
|
25
|
+
tz: getTimezone(),
|
|
26
|
+
ref: getReferrer(),
|
|
27
|
+
so: getUTMSource(),
|
|
28
|
+
me: getUTMMedium(),
|
|
29
|
+
ca: getUTMCampaign(),
|
|
22
30
|
...event,
|
|
23
31
|
};
|
|
24
32
|
this.sendRequest('custom', data);
|
|
@@ -115,6 +123,34 @@ export class Lib {
|
|
|
115
123
|
this.trackPage(newPath, false);
|
|
116
124
|
}
|
|
117
125
|
}
|
|
126
|
+
getPreviousPage() {
|
|
127
|
+
// Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
|
|
128
|
+
// That method of getting previous page works for SPA websites
|
|
129
|
+
if (this.activePage) {
|
|
130
|
+
return this.activePage;
|
|
131
|
+
}
|
|
132
|
+
// Checking if URL is supported by the browser (for example, IE11 does not support it)
|
|
133
|
+
if (typeof URL === 'function') {
|
|
134
|
+
// That method of getting previous page works for websites with page reloads
|
|
135
|
+
const referrer = getReferrer();
|
|
136
|
+
if (!referrer) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const { host } = location;
|
|
140
|
+
try {
|
|
141
|
+
const url = new URL(referrer);
|
|
142
|
+
const { host: refHost, pathname } = url;
|
|
143
|
+
if (host !== refHost) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
return pathname;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
118
154
|
trackPage(pg, unique = false) {
|
|
119
155
|
if (!this.pageData)
|
|
120
156
|
return;
|
|
@@ -122,6 +158,10 @@ export class Lib {
|
|
|
122
158
|
if (this.checkIgnore(pg))
|
|
123
159
|
return;
|
|
124
160
|
const perf = this.getPerformanceStats();
|
|
161
|
+
let prev;
|
|
162
|
+
if (!this.pageViewsOptions?.noUserFlow) {
|
|
163
|
+
prev = this.getPreviousPage();
|
|
164
|
+
}
|
|
125
165
|
const data = {
|
|
126
166
|
pid: this.projectID,
|
|
127
167
|
lc: getLocale(),
|
|
@@ -133,7 +173,9 @@ export class Lib {
|
|
|
133
173
|
unique,
|
|
134
174
|
pg,
|
|
135
175
|
perf,
|
|
176
|
+
prev,
|
|
136
177
|
};
|
|
178
|
+
this.activePage = pg;
|
|
137
179
|
this.sendRequest('', data);
|
|
138
180
|
}
|
|
139
181
|
debug(message) {
|
package/dist/esnext/Lib.js.map
CHANGED
|
@@ -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;
|
|
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;AAmEhB,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,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,EAAE;YACjB,IAAI,CAAC,KAAK,CAAC,0DAA0D,CAAC,CAAA;YACtE,OAAO,KAAK,CAAA;SACb;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAEO,WAAW,CAAC,IAAY,EAAE,IAAY;QAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,MAAM,IAAI,gBAAgB,CAAA;QACrD,MAAM,GAAG,GAAG,IAAI,cAAc,EAAE,CAAA;QAChC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,IAAI,IAAI,EAAE,EAAE,IAAI,CAAC,CAAA;QACzC,GAAG,CAAC,gBAAgB,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAA;QACxD,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAA;IAChC,CAAC;CACF"}
|
package/dist/swetrix.cjs.js
CHANGED
|
@@ -77,6 +77,7 @@ var Lib = /** @class */ (function () {
|
|
|
77
77
|
this.pageData = null;
|
|
78
78
|
this.pageViewsOptions = null;
|
|
79
79
|
this.perfStatsCollected = false;
|
|
80
|
+
this.activePage = null;
|
|
80
81
|
this.trackPathChange = this.trackPathChange.bind(this);
|
|
81
82
|
this.heartbeat = this.heartbeat.bind(this);
|
|
82
83
|
}
|
|
@@ -84,7 +85,7 @@ var Lib = /** @class */ (function () {
|
|
|
84
85
|
if (!this.canTrack()) {
|
|
85
86
|
return;
|
|
86
87
|
}
|
|
87
|
-
var data = __assign({ pid: this.projectID }, event);
|
|
88
|
+
var data = __assign({ pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }, event);
|
|
88
89
|
this.sendRequest('custom', data);
|
|
89
90
|
};
|
|
90
91
|
Lib.prototype.trackPageViews = function (options) {
|
|
@@ -182,7 +183,36 @@ var Lib = /** @class */ (function () {
|
|
|
182
183
|
this.trackPage(newPath, false);
|
|
183
184
|
}
|
|
184
185
|
};
|
|
186
|
+
Lib.prototype.getPreviousPage = function () {
|
|
187
|
+
// Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
|
|
188
|
+
// That method of getting previous page works for SPA websites
|
|
189
|
+
if (this.activePage) {
|
|
190
|
+
return this.activePage;
|
|
191
|
+
}
|
|
192
|
+
// Checking if URL is supported by the browser (for example, IE11 does not support it)
|
|
193
|
+
if (typeof URL === 'function') {
|
|
194
|
+
// That method of getting previous page works for websites with page reloads
|
|
195
|
+
var referrer = getReferrer();
|
|
196
|
+
if (!referrer) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
var host = location.host;
|
|
200
|
+
try {
|
|
201
|
+
var url = new URL(referrer);
|
|
202
|
+
var refHost = url.host, pathname = url.pathname;
|
|
203
|
+
if (host !== refHost) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
return pathname;
|
|
207
|
+
}
|
|
208
|
+
catch (_a) {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return null;
|
|
213
|
+
};
|
|
185
214
|
Lib.prototype.trackPage = function (pg, unique) {
|
|
215
|
+
var _a;
|
|
186
216
|
if (unique === void 0) { unique = false; }
|
|
187
217
|
if (!this.pageData)
|
|
188
218
|
return;
|
|
@@ -190,6 +220,10 @@ var Lib = /** @class */ (function () {
|
|
|
190
220
|
if (this.checkIgnore(pg))
|
|
191
221
|
return;
|
|
192
222
|
var perf = this.getPerformanceStats();
|
|
223
|
+
var prev;
|
|
224
|
+
if (!((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.noUserFlow)) {
|
|
225
|
+
prev = this.getPreviousPage();
|
|
226
|
+
}
|
|
193
227
|
var data = {
|
|
194
228
|
pid: this.projectID,
|
|
195
229
|
lc: getLocale(),
|
|
@@ -201,7 +235,9 @@ var Lib = /** @class */ (function () {
|
|
|
201
235
|
unique: unique,
|
|
202
236
|
pg: pg,
|
|
203
237
|
perf: perf,
|
|
238
|
+
prev: prev,
|
|
204
239
|
};
|
|
240
|
+
this.activePage = pg;
|
|
205
241
|
this.sendRequest('', data);
|
|
206
242
|
};
|
|
207
243
|
Lib.prototype.debug = function (message) {
|
package/dist/swetrix.es5.js
CHANGED
|
@@ -73,6 +73,7 @@ var Lib = /** @class */ (function () {
|
|
|
73
73
|
this.pageData = null;
|
|
74
74
|
this.pageViewsOptions = null;
|
|
75
75
|
this.perfStatsCollected = false;
|
|
76
|
+
this.activePage = null;
|
|
76
77
|
this.trackPathChange = this.trackPathChange.bind(this);
|
|
77
78
|
this.heartbeat = this.heartbeat.bind(this);
|
|
78
79
|
}
|
|
@@ -80,7 +81,7 @@ var Lib = /** @class */ (function () {
|
|
|
80
81
|
if (!this.canTrack()) {
|
|
81
82
|
return;
|
|
82
83
|
}
|
|
83
|
-
var data = __assign({ pid: this.projectID }, event);
|
|
84
|
+
var data = __assign({ pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }, event);
|
|
84
85
|
this.sendRequest('custom', data);
|
|
85
86
|
};
|
|
86
87
|
Lib.prototype.trackPageViews = function (options) {
|
|
@@ -178,7 +179,36 @@ var Lib = /** @class */ (function () {
|
|
|
178
179
|
this.trackPage(newPath, false);
|
|
179
180
|
}
|
|
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
|
+
return this.activePage;
|
|
187
|
+
}
|
|
188
|
+
// Checking if URL is supported by the browser (for example, IE11 does not support it)
|
|
189
|
+
if (typeof URL === 'function') {
|
|
190
|
+
// That method of getting previous page works for websites with page reloads
|
|
191
|
+
var referrer = getReferrer();
|
|
192
|
+
if (!referrer) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
var host = location.host;
|
|
196
|
+
try {
|
|
197
|
+
var url = new URL(referrer);
|
|
198
|
+
var refHost = url.host, pathname = url.pathname;
|
|
199
|
+
if (host !== refHost) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
return pathname;
|
|
203
|
+
}
|
|
204
|
+
catch (_a) {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
};
|
|
181
210
|
Lib.prototype.trackPage = function (pg, unique) {
|
|
211
|
+
var _a;
|
|
182
212
|
if (unique === void 0) { unique = false; }
|
|
183
213
|
if (!this.pageData)
|
|
184
214
|
return;
|
|
@@ -186,6 +216,10 @@ var Lib = /** @class */ (function () {
|
|
|
186
216
|
if (this.checkIgnore(pg))
|
|
187
217
|
return;
|
|
188
218
|
var perf = this.getPerformanceStats();
|
|
219
|
+
var prev;
|
|
220
|
+
if (!((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.noUserFlow)) {
|
|
221
|
+
prev = this.getPreviousPage();
|
|
222
|
+
}
|
|
189
223
|
var data = {
|
|
190
224
|
pid: this.projectID,
|
|
191
225
|
lc: getLocale(),
|
|
@@ -197,7 +231,9 @@ var Lib = /** @class */ (function () {
|
|
|
197
231
|
unique: unique,
|
|
198
232
|
pg: pg,
|
|
199
233
|
perf: perf,
|
|
234
|
+
prev: prev,
|
|
200
235
|
};
|
|
236
|
+
this.activePage = pg;
|
|
201
237
|
this.sendRequest('', data);
|
|
202
238
|
};
|
|
203
239
|
Lib.prototype.debug = function (message) {
|
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
|
|
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 a(){return void 0!==navigator.languages?navigator.languages[0]:navigator.language}function r(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){}}function s(){return document.referrer||void 0}function c(){return t(o)}function u(){return t(d)}function l(){return t(p)}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)=([^?&]+)/,p=/[?&](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:a(),tz:r(),ref:s(),so:c(),me:u(),ca:l()},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(){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},f.prototype.trackPage=function(t,e){var n,o,i;void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,this.checkIgnore(t)||(n=this.getPerformanceStats(),null!=(i=this.pageViewsOptions)&&i.noUserFlow||(o=this.getPreviousPage()),i={pid:this.projectID,lc:a(),tz:r(),ref:s(),so:c(),me:u(),ca:l(),unique:e,pg:t,perf:n,prev:o},this.activePage=t,this.sendRequest("",i)))},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})});
|
package/dist/swetrix.orig.js
CHANGED
|
@@ -79,6 +79,7 @@
|
|
|
79
79
|
this.pageData = null;
|
|
80
80
|
this.pageViewsOptions = null;
|
|
81
81
|
this.perfStatsCollected = false;
|
|
82
|
+
this.activePage = null;
|
|
82
83
|
this.trackPathChange = this.trackPathChange.bind(this);
|
|
83
84
|
this.heartbeat = this.heartbeat.bind(this);
|
|
84
85
|
}
|
|
@@ -86,7 +87,7 @@
|
|
|
86
87
|
if (!this.canTrack()) {
|
|
87
88
|
return;
|
|
88
89
|
}
|
|
89
|
-
var data = __assign({ pid: this.projectID }, event);
|
|
90
|
+
var data = __assign({ pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }, event);
|
|
90
91
|
this.sendRequest('custom', data);
|
|
91
92
|
};
|
|
92
93
|
Lib.prototype.trackPageViews = function (options) {
|
|
@@ -184,7 +185,36 @@
|
|
|
184
185
|
this.trackPage(newPath, false);
|
|
185
186
|
}
|
|
186
187
|
};
|
|
188
|
+
Lib.prototype.getPreviousPage = function () {
|
|
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
|
+
return this.activePage;
|
|
193
|
+
}
|
|
194
|
+
// Checking if URL is supported by the browser (for example, IE11 does not support it)
|
|
195
|
+
if (typeof URL === 'function') {
|
|
196
|
+
// That method of getting previous page works for websites with page reloads
|
|
197
|
+
var referrer = getReferrer();
|
|
198
|
+
if (!referrer) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
var host = location.host;
|
|
202
|
+
try {
|
|
203
|
+
var url = new URL(referrer);
|
|
204
|
+
var refHost = url.host, pathname = url.pathname;
|
|
205
|
+
if (host !== refHost) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
return pathname;
|
|
209
|
+
}
|
|
210
|
+
catch (_a) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return null;
|
|
215
|
+
};
|
|
187
216
|
Lib.prototype.trackPage = function (pg, unique) {
|
|
217
|
+
var _a;
|
|
188
218
|
if (unique === void 0) { unique = false; }
|
|
189
219
|
if (!this.pageData)
|
|
190
220
|
return;
|
|
@@ -192,6 +222,10 @@
|
|
|
192
222
|
if (this.checkIgnore(pg))
|
|
193
223
|
return;
|
|
194
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
|
+
}
|
|
195
229
|
var data = {
|
|
196
230
|
pid: this.projectID,
|
|
197
231
|
lc: getLocale(),
|
|
@@ -203,7 +237,9 @@
|
|
|
203
237
|
unique: unique,
|
|
204
238
|
pg: pg,
|
|
205
239
|
perf: perf,
|
|
240
|
+
prev: prev,
|
|
206
241
|
};
|
|
242
|
+
this.activePage = pg;
|
|
207
243
|
this.sendRequest('', data);
|
|
208
244
|
};
|
|
209
245
|
Lib.prototype.debug = function (message) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swetrix",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.2.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",
|
|
@@ -27,19 +27,19 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://swetrix.com/docs",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@types/node": "^18.
|
|
30
|
+
"@types/node": "^18.16.1",
|
|
31
31
|
"tslib": "^2.5.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@rollup/plugin-commonjs": "^24.0
|
|
35
|
-
"@rollup/plugin-node-resolve": "^15.0.
|
|
36
|
-
"rimraf": "^4.1
|
|
34
|
+
"@rollup/plugin-commonjs": "^24.1.0",
|
|
35
|
+
"@rollup/plugin-node-resolve": "^15.0.2",
|
|
36
|
+
"rimraf": "^4.4.1",
|
|
37
37
|
"rollup": "^2.79.1",
|
|
38
38
|
"rollup-plugin-node-resolve": "^5.2.0",
|
|
39
39
|
"rollup-plugin-sourcemaps": "^0.6.3",
|
|
40
40
|
"rollup-plugin-typescript2": "^0.34.1",
|
|
41
41
|
"rollup-plugin-uglify": "^6.0.4",
|
|
42
|
-
"typescript": "^
|
|
42
|
+
"typescript": "^5.0.4"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"prebuild": "rimraf dist",
|
package/src/Lib.ts
CHANGED
|
@@ -63,6 +63,9 @@ export interface PageViewsOptions {
|
|
|
63
63
|
|
|
64
64
|
/** Send Heartbeat requests when the website tab is not active in the browser. */
|
|
65
65
|
heartbeatOnBackground?: boolean
|
|
66
|
+
|
|
67
|
+
/** Disable user-flow */
|
|
68
|
+
noUserFlow?: boolean
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
export const defaultPageActions = {
|
|
@@ -75,6 +78,7 @@ export class Lib {
|
|
|
75
78
|
private pageData: PageData | null = null
|
|
76
79
|
private pageViewsOptions: PageViewsOptions | null | undefined = null
|
|
77
80
|
private perfStatsCollected: Boolean = false
|
|
81
|
+
private activePage: string | null = null
|
|
78
82
|
|
|
79
83
|
constructor(private projectID: string, private options?: LibOptions) {
|
|
80
84
|
this.trackPathChange = this.trackPathChange.bind(this)
|
|
@@ -88,6 +92,13 @@ export class Lib {
|
|
|
88
92
|
|
|
89
93
|
const data = {
|
|
90
94
|
pid: this.projectID,
|
|
95
|
+
pg: this.activePage,
|
|
96
|
+
lc: getLocale(),
|
|
97
|
+
tz: getTimezone(),
|
|
98
|
+
ref: getReferrer(),
|
|
99
|
+
so: getUTMSource(),
|
|
100
|
+
me: getUTMMedium(),
|
|
101
|
+
ca: getUTMCampaign(),
|
|
91
102
|
...event,
|
|
92
103
|
}
|
|
93
104
|
this.sendRequest('custom', data)
|
|
@@ -204,6 +215,41 @@ export class Lib {
|
|
|
204
215
|
}
|
|
205
216
|
}
|
|
206
217
|
|
|
218
|
+
private getPreviousPage(): string | null {
|
|
219
|
+
// Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
|
|
220
|
+
// That method of getting previous page works for SPA websites
|
|
221
|
+
if (this.activePage) {
|
|
222
|
+
return this.activePage
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Checking if URL is supported by the browser (for example, IE11 does not support it)
|
|
226
|
+
if (typeof URL === 'function') {
|
|
227
|
+
// That method of getting previous page works for websites with page reloads
|
|
228
|
+
const referrer = getReferrer()
|
|
229
|
+
|
|
230
|
+
if (!referrer) {
|
|
231
|
+
return null
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const { host } = location
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
const url = new URL(referrer)
|
|
238
|
+
const { host: refHost, pathname } = url
|
|
239
|
+
|
|
240
|
+
if (host !== refHost) {
|
|
241
|
+
return null
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return pathname
|
|
245
|
+
} catch {
|
|
246
|
+
return null
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return null
|
|
251
|
+
}
|
|
252
|
+
|
|
207
253
|
private trackPage(pg: string, unique: boolean = false): void {
|
|
208
254
|
if (!this.pageData) return
|
|
209
255
|
this.pageData.path = pg
|
|
@@ -212,6 +258,12 @@ export class Lib {
|
|
|
212
258
|
|
|
213
259
|
const perf = this.getPerformanceStats()
|
|
214
260
|
|
|
261
|
+
let prev
|
|
262
|
+
|
|
263
|
+
if (!this.pageViewsOptions?.noUserFlow) {
|
|
264
|
+
prev = this.getPreviousPage()
|
|
265
|
+
}
|
|
266
|
+
|
|
215
267
|
const data = {
|
|
216
268
|
pid: this.projectID,
|
|
217
269
|
lc: getLocale(),
|
|
@@ -223,8 +275,10 @@ export class Lib {
|
|
|
223
275
|
unique,
|
|
224
276
|
pg,
|
|
225
277
|
perf,
|
|
278
|
+
prev,
|
|
226
279
|
}
|
|
227
280
|
|
|
281
|
+
this.activePage = pg
|
|
228
282
|
this.sendRequest('', data)
|
|
229
283
|
}
|
|
230
284
|
|