swetrix 1.3.1 → 1.3.2

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.
@@ -0,0 +1 @@
1
+ ko_fi: andriir
package/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  <img src="https://swetrix.com/assets/logo_blue.svg" alt="" height="100" />
2
2
 
3
+ [![JSDelivr hits](https://data.jsdelivr.com/v1/package/gh/Swetrix/swetrix-js/badge?style=rounded)](https://data.jsdelivr.com/v1/package/gh/Swetrix/swetrix-js/stats)
4
+ [![Package size](https://img.shields.io/bundlephobia/minzip/swetrix)](https://bundlephobia.com/api/size?package=swetrix)
5
+ [![Contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/swetrix/swetrix-js/issues)
6
+
3
7
  # Swetrix Tracking Script
4
8
 
5
9
  This repository contains the analytics script which is used at https://swetrix.com \
@@ -8,5 +12,11 @@ You can find the detailed documentation and use cases at our [docs page](https:/
8
12
  Feel free to contribute to the source code by opening a pull requests. \
9
13
  For any questions, you can open an issue ticket, refer to our [FAQs](https://swetrix.com/#faq) page or reach us at contact@swetrix.com
10
14
 
15
+ The latest live versions of the script are located at [jsDelivr](https://swetrix.org/swetrix.js) and [NPM](https://www.npmjs.com/package/swetrix).
16
+
11
17
  # Selfhosted API
12
18
  If you are selfhosting the [Swetrix-API](https://github.com/Swetrix/swetrix-api), be sure to point the `apiUrl` parameter to: `https://yourapiinstance.com/log`
19
+
20
+ # Donate
21
+ You can support the project by donating us at https://ko-fi.com/andriir \
22
+ We can only run our services by once again asking for your financial support!
@@ -1,23 +1,55 @@
1
1
  export interface LibOptions {
2
+ /**
3
+ * When set to `true`, all tracking logs will be printed to console and localhost events will be sent to server.
4
+ */
2
5
  debug?: boolean;
6
+ /**
7
+ * When set to `true`, the tracking library won't send any data to server.
8
+ * Useful for development purposes when this value is set based on `.env` var.
9
+ */
3
10
  disabled?: boolean;
11
+ /**
12
+ * By setting this flag to `true`, we will not collect ANY kind of data about the user with the DNT setting.
13
+ */
4
14
  respectDNT?: boolean;
15
+ /** Set a custom URL of the API server (for selfhosted variants of Swetrix). */
5
16
  apiURL?: string;
6
17
  }
7
18
  export interface TrackEventOptions {
19
+ /** The custom event name. */
8
20
  ev: string;
21
+ /** If set to `true`, only 1 event with the same ID will be saved per user session. */
9
22
  unique?: boolean;
10
23
  }
24
+ /**
25
+ * The object returned by `trackPageViews()`, used to stop tracking pages.
26
+ */
27
+ export interface PageActions {
28
+ /** Stops the tracking of pages. */
29
+ stop: () => void;
30
+ }
11
31
  export interface PageData {
32
+ /** Current URL path. */
12
33
  path: string;
13
- actions: object;
34
+ /** The object returned by `trackPageViews()`, used to stop tracking pages. */
35
+ actions: PageActions;
14
36
  }
15
37
  export interface PageViewsOptions {
38
+ /**
39
+ * If set to `true`, only unique events will be saved.
40
+ * This param is useful when tracking single-page landing websites.
41
+ */
16
42
  unique?: boolean;
17
- ignore?: Array<any>;
43
+ /** A list of Regular Expressions or string pathes to ignore. */
44
+ ignore?: Array<string | RegExp>;
45
+ /** Do not send Heartbeat requests to the server. */
18
46
  noHeartbeat?: boolean;
47
+ /** Send Heartbeat requests when the website tab is not active in the browser. */
19
48
  heartbeatOnBackground?: boolean;
20
49
  }
50
+ export declare const defaultPageActions: {
51
+ stop(): void;
52
+ };
21
53
  export declare class Lib {
22
54
  private projectID;
23
55
  private options?;
@@ -25,7 +57,7 @@ export declare class Lib {
25
57
  private pageViewsOptions;
26
58
  constructor(projectID: string, options?: LibOptions | undefined);
27
59
  track(event: TrackEventOptions): void;
28
- trackPageViews(options?: PageViewsOptions): void | object;
60
+ trackPageViews(options?: PageViewsOptions): PageActions;
29
61
  private heartbeat;
30
62
  private checkIgnore;
31
63
  private trackPathChange;
@@ -1,4 +1,7 @@
1
1
  import { isInBrowser, isLocalhost, isAutomated, getLocale, getTimezone, getReferrer, getUTMCampaign, getUTMMedium, getUTMSource, getPath, } from './utils';
2
+ export const defaultPageActions = {
3
+ stop() { },
4
+ };
2
5
  const DEFAULT_API_HOST = 'https://api.swetrix.com/log';
3
6
  export class Lib {
4
7
  constructor(projectID, options) {
@@ -21,7 +24,7 @@ export class Lib {
21
24
  }
22
25
  trackPageViews(options) {
23
26
  if (!this.canTrack()) {
24
- return;
27
+ return defaultPageActions;
25
28
  }
26
29
  if (this.pageData) {
27
30
  return this.pageData.actions;
@@ -63,6 +66,7 @@ export class Lib {
63
66
  for (let i = 0; i < ignore.length; ++i) {
64
67
  if (ignore[i] === path)
65
68
  return true;
69
+ // @ts-ignore
66
70
  if (ignore[i] instanceof RegExp && ignore[i].test(path))
67
71
  return true;
68
72
  }
@@ -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;AAkDhB,MAAM,gBAAgB,GAAG,6BAA6B,CAAA;AAEtD,MAAM,OAAO,GAAG;IAId,YAAoB,SAAiB,EAAU,OAAoB;QAA/C,cAAS,GAAT,SAAS,CAAQ;QAAU,YAAO,GAAP,OAAO,CAAa;QAH3D,aAAQ,GAAoB,IAAI,CAAA;QAChC,qBAAgB,GAAwC,IAAI,CAAA;QAGlE,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,GAAG,KAAK;SACT,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C,CAAA;IAChC,CAAC;CACF"}
@@ -1,5 +1,27 @@
1
- import { Lib, LibOptions, TrackEventOptions, PageViewsOptions } from './Lib';
1
+ import { Lib, LibOptions, TrackEventOptions, PageViewsOptions, PageActions } from './Lib';
2
2
  export declare let LIB_INSTANCE: Lib | null;
3
+ /**
4
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
5
+ *
6
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
7
+ * @param {LibOptions} options Options related to the tracking.
8
+ * @returns {Lib} Instance of the Swetrix.js.
9
+ */
3
10
  export declare function init(pid: string, options?: LibOptions): Lib;
11
+ /**
12
+ * With this function you are able to track any custom events you want.
13
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
14
+ * The total number of track calls and their conversion rate will be saved.
15
+ *
16
+ * @param {TrackEventOptions} event The options related to the custom event.
17
+ */
4
18
  export declare function track(event: TrackEventOptions): void;
5
- export declare function trackViews(options?: PageViewsOptions): void;
19
+ /**
20
+ * With this function you are able to track any custom events you want.
21
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
22
+ * The total number of track calls and their conversion rate will be saved.
23
+ *
24
+ * @param {PageViewsOptions} options The options related to the custom event.
25
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
26
+ */
27
+ export declare function trackViews(options?: PageViewsOptions): PageActions;
@@ -1,22 +1,41 @@
1
- import { Lib } from './Lib';
1
+ import { Lib, defaultPageActions, } from './Lib';
2
2
  export let LIB_INSTANCE = null;
3
- // Initialise the tracking library instance (other methods won't work if the library
4
- // is not initialised)
3
+ /**
4
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
5
+ *
6
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
7
+ * @param {LibOptions} options Options related to the tracking.
8
+ * @returns {Lib} Instance of the Swetrix.js.
9
+ */
5
10
  export function init(pid, options) {
6
11
  if (!LIB_INSTANCE) {
7
12
  LIB_INSTANCE = new Lib(pid, options);
8
13
  }
9
14
  return LIB_INSTANCE;
10
15
  }
11
- // Tracks custom events
16
+ /**
17
+ * With this function you are able to track any custom events you want.
18
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
19
+ * The total number of track calls and their conversion rate will be saved.
20
+ *
21
+ * @param {TrackEventOptions} event The options related to the custom event.
22
+ */
12
23
  export function track(event) {
13
24
  if (!LIB_INSTANCE)
14
25
  return;
15
26
  LIB_INSTANCE.track(event);
16
27
  }
28
+ /**
29
+ * With this function you are able to track any custom events you want.
30
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
31
+ * The total number of track calls and their conversion rate will be saved.
32
+ *
33
+ * @param {PageViewsOptions} options The options related to the custom event.
34
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
35
+ */
17
36
  export function trackViews(options) {
18
37
  if (!LIB_INSTANCE)
19
- return;
20
- LIB_INSTANCE.trackPageViews(options);
38
+ return defaultPageActions;
39
+ return LIB_INSTANCE.trackPageViews(options);
21
40
  }
22
41
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAmD,MAAM,OAAO,CAAA;AAE5E,MAAM,CAAC,IAAI,YAAY,GAAe,IAAI,CAAA;AAE1C,oFAAoF;AACpF,sBAAsB;AACtB,MAAM,UAAU,IAAI,CAAE,GAAW,EAAE,OAAoB;IACrD,IAAI,CAAC,YAAY,EAAE;QACjB,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;KACrC;IAED,OAAO,YAAY,CAAA;AACrB,CAAC;AAED,uBAAuB;AACvB,MAAM,UAAU,KAAK,CAAE,KAAwB;IAC7C,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC3B,CAAC;AAED,MAAM,UAAU,UAAU,CAAE,OAA0B;IACpD,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;AACtC,CAAC"}
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,CAAE,GAAW,EAAE,OAAoB;IACrD,IAAI,CAAC,YAAY,EAAE;QACjB,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;KACrC;IAED,OAAO,YAAY,CAAA;AACrB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAE,KAAwB;IAC7C,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC3B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAE,OAA0B;IACpD,IAAI,CAAC,YAAY;QAAE,OAAO,kBAAkB,CAAA;IAE5C,OAAO,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;AAC7C,CAAC"}
@@ -66,6 +66,9 @@ var getPath = function () {
66
66
  return location.pathname || '';
67
67
  };
68
68
 
69
+ var defaultPageActions = {
70
+ stop: function () { },
71
+ };
69
72
  var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
70
73
  var Lib = /** @class */ (function () {
71
74
  function Lib(projectID, options) {
@@ -85,7 +88,7 @@ var Lib = /** @class */ (function () {
85
88
  };
86
89
  Lib.prototype.trackPageViews = function (options) {
87
90
  if (!this.canTrack()) {
88
- return;
91
+ return defaultPageActions;
89
92
  }
90
93
  if (this.pageData) {
91
94
  return this.pageData.actions;
@@ -129,6 +132,7 @@ var Lib = /** @class */ (function () {
129
132
  for (var i = 0; i < ignore.length; ++i) {
130
133
  if (ignore[i] === path)
131
134
  return true;
135
+ // @ts-ignore
132
136
  if (ignore[i] instanceof RegExp && ignore[i].test(path))
133
137
  return true;
134
138
  }
@@ -206,24 +210,43 @@ var Lib = /** @class */ (function () {
206
210
  }());
207
211
 
208
212
  exports.LIB_INSTANCE = null;
209
- // Initialise the tracking library instance (other methods won't work if the library
210
- // is not initialised)
213
+ /**
214
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
215
+ *
216
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
217
+ * @param {LibOptions} options Options related to the tracking.
218
+ * @returns {Lib} Instance of the Swetrix.js.
219
+ */
211
220
  function init(pid, options) {
212
221
  if (!exports.LIB_INSTANCE) {
213
222
  exports.LIB_INSTANCE = new Lib(pid, options);
214
223
  }
215
224
  return exports.LIB_INSTANCE;
216
225
  }
217
- // Tracks custom events
226
+ /**
227
+ * With this function you are able to track any custom events you want.
228
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
229
+ * The total number of track calls and their conversion rate will be saved.
230
+ *
231
+ * @param {TrackEventOptions} event The options related to the custom event.
232
+ */
218
233
  function track(event) {
219
234
  if (!exports.LIB_INSTANCE)
220
235
  return;
221
236
  exports.LIB_INSTANCE.track(event);
222
237
  }
238
+ /**
239
+ * With this function you are able to track any custom events you want.
240
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
241
+ * The total number of track calls and their conversion rate will be saved.
242
+ *
243
+ * @param {PageViewsOptions} options The options related to the custom event.
244
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
245
+ */
223
246
  function trackViews(options) {
224
247
  if (!exports.LIB_INSTANCE)
225
- return;
226
- exports.LIB_INSTANCE.trackPageViews(options);
248
+ return defaultPageActions;
249
+ return exports.LIB_INSTANCE.trackPageViews(options);
227
250
  }
228
251
 
229
252
  exports.init = init;
@@ -62,6 +62,9 @@ var getPath = function () {
62
62
  return location.pathname || '';
63
63
  };
64
64
 
65
+ var defaultPageActions = {
66
+ stop: function () { },
67
+ };
65
68
  var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
66
69
  var Lib = /** @class */ (function () {
67
70
  function Lib(projectID, options) {
@@ -81,7 +84,7 @@ var Lib = /** @class */ (function () {
81
84
  };
82
85
  Lib.prototype.trackPageViews = function (options) {
83
86
  if (!this.canTrack()) {
84
- return;
87
+ return defaultPageActions;
85
88
  }
86
89
  if (this.pageData) {
87
90
  return this.pageData.actions;
@@ -125,6 +128,7 @@ var Lib = /** @class */ (function () {
125
128
  for (var i = 0; i < ignore.length; ++i) {
126
129
  if (ignore[i] === path)
127
130
  return true;
131
+ // @ts-ignore
128
132
  if (ignore[i] instanceof RegExp && ignore[i].test(path))
129
133
  return true;
130
134
  }
@@ -202,24 +206,43 @@ var Lib = /** @class */ (function () {
202
206
  }());
203
207
 
204
208
  var LIB_INSTANCE = null;
205
- // Initialise the tracking library instance (other methods won't work if the library
206
- // is not initialised)
209
+ /**
210
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
211
+ *
212
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
213
+ * @param {LibOptions} options Options related to the tracking.
214
+ * @returns {Lib} Instance of the Swetrix.js.
215
+ */
207
216
  function init(pid, options) {
208
217
  if (!LIB_INSTANCE) {
209
218
  LIB_INSTANCE = new Lib(pid, options);
210
219
  }
211
220
  return LIB_INSTANCE;
212
221
  }
213
- // Tracks custom events
222
+ /**
223
+ * With this function you are able to track any custom events you want.
224
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
225
+ * The total number of track calls and their conversion rate will be saved.
226
+ *
227
+ * @param {TrackEventOptions} event The options related to the custom event.
228
+ */
214
229
  function track(event) {
215
230
  if (!LIB_INSTANCE)
216
231
  return;
217
232
  LIB_INSTANCE.track(event);
218
233
  }
234
+ /**
235
+ * With this function you are able to track any custom events you want.
236
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
237
+ * The total number of track calls and their conversion rate will be saved.
238
+ *
239
+ * @param {PageViewsOptions} options The options related to the custom event.
240
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
241
+ */
219
242
  function trackViews(options) {
220
243
  if (!LIB_INSTANCE)
221
- return;
222
- LIB_INSTANCE.trackPageViews(options);
244
+ return defaultPageActions;
245
+ return LIB_INSTANCE.trackPageViews(options);
223
246
  }
224
247
 
225
248
  export { LIB_INSTANCE, init, track, 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(i){"use strict";function n(t){return(t=location.search.match(t))&&t[2]||void 0}function a(){return location.pathname||""}var e=function(){return(e=Object.assign||function(t){for(var e,i=1,n=arguments.length;i<n;i++)for(var a in e=arguments[i])Object.prototype.hasOwnProperty.call(e,a)&&(t[a]=e[a]);return t}).apply(this,arguments)},o=/[?&](ref|source|utm_source)=([^?&]+)/,r=/[?&](utm_campaign)=([^?&]+)/,s=/[?&](utm_medium)=([^?&]+)/,c=(t.prototype.track=function(t){this.canTrack()&&(t=e({pid:this.projectID},t),this.sendRequest("custom",t))},t.prototype.trackPageViews=function(t){if(this.canTrack()){if(this.pageData)return this.pageData.actions;var e,i;null!=(this.pageViewsOptions=t)&&t.unique||(i=setInterval(this.trackPathChange,2e3)),null!=t&&t.noHeartbeat||(setTimeout(this.heartbeat,3e3),e=setInterval(this.heartbeat,28e3));var n=a();return this.pageData={path:n,actions:{stop:function(){clearInterval(i),clearInterval(e)}}},this.trackPage(n,null==t?void 0:t.unique),this.pageData.actions}},t.prototype.heartbeat=function(){var t;(null!==(t=this.pageViewsOptions)&&void 0!==t&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},t.prototype.checkIgnore=function(t){var e,i=null===(e=this.pageViewsOptions)||void 0===e?void 0:e.ignore;if(Array.isArray(i))for(var n=0;n<i.length;++n){if(i[n]===t)return!0;if(i[n]instanceof RegExp&&i[n].test(t))return!0}return!1},t.prototype.trackPathChange=function(){var t;this.pageData&&(t=a(),this.pageData.path!==t&&this.trackPage(t,!1))},t.prototype.trackPage=function(t,e){void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,this.checkIgnore(t)||(t={pid:this.projectID,lc:void 0!==navigator.languages?navigator.languages[0]:navigator.language,tz:function(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){return}}(),ref:document.referrer||void 0,so:n(o),me:n(s),ca:n(r),unique:e,pg:t},this.sendRequest("",t)))},t.prototype.debug=function(t){var e;null!==(e=this.options)&&void 0!==e&&e.debug&&console.log("[Swetrix]",t)},t.prototype.canTrack=function(){var t,e;return null!==(t=this.options)&&void 0!==t&&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)&&void 0!==t&&t.respectDNT&&"1"===(null===(e=window.navigator)||void 0===e?void 0:e.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!!(null!==(e=this.options)&&void 0!==e&&e.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))},t.prototype.sendRequest=function(t,e){var i=(null===(n=this.options)||void 0===n?void 0:n.apiURL)||"https://api.swetrix.com/log",n=new XMLHttpRequest;n.open("POST","".concat(i,"/").concat(t),!0),n.setRequestHeader("Content-Type","application/json"),n.send(JSON.stringify(e))},t);function t(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this)}i.LIB_INSTANCE=null,i.init=function(t,e){return i.LIB_INSTANCE||(i.LIB_INSTANCE=new c(t,e)),i.LIB_INSTANCE},i.track=function(t){i.LIB_INSTANCE&&i.LIB_INSTANCE.track(t)},i.trackViews=function(t){i.LIB_INSTANCE&&i.LIB_INSTANCE.trackPageViews(t)},Object.defineProperty(i,"__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(i){"use strict";function n(t){return(t=location.search.match(t))&&t[2]||void 0}function o(){return location.pathname||""}var e=function(){return(e=Object.assign||function(t){for(var e,i=1,n=arguments.length;i<n;i++)for(var o in e=arguments[i])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)},a=/[?&](ref|source|utm_source)=([^?&]+)/,r=/[?&](utm_campaign)=([^?&]+)/,s=/[?&](utm_medium)=([^?&]+)/,c={stop:function(){}},u=(t.prototype.track=function(t){this.canTrack()&&(t=e({pid:this.projectID},t),this.sendRequest("custom",t))},t.prototype.trackPageViews=function(t){if(!this.canTrack())return c;if(this.pageData)return this.pageData.actions;var e,i;null!=(this.pageViewsOptions=t)&&t.unique||(i=setInterval(this.trackPathChange,2e3)),null!=t&&t.noHeartbeat||(setTimeout(this.heartbeat,3e3),e=setInterval(this.heartbeat,28e3));var n=o();return this.pageData={path:n,actions:{stop:function(){clearInterval(i),clearInterval(e)}}},this.trackPage(n,null==t?void 0:t.unique),this.pageData.actions},t.prototype.heartbeat=function(){var t;(null!==(t=this.pageViewsOptions)&&void 0!==t&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},t.prototype.checkIgnore=function(t){var e,i=null===(e=this.pageViewsOptions)||void 0===e?void 0:e.ignore;if(Array.isArray(i))for(var n=0;n<i.length;++n){if(i[n]===t)return!0;if(i[n]instanceof RegExp&&i[n].test(t))return!0}return!1},t.prototype.trackPathChange=function(){var t;this.pageData&&(t=o(),this.pageData.path!==t&&this.trackPage(t,!1))},t.prototype.trackPage=function(t,e){void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,this.checkIgnore(t)||(t={pid:this.projectID,lc:void 0!==navigator.languages?navigator.languages[0]:navigator.language,tz:function(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){return}}(),ref:document.referrer||void 0,so:n(a),me:n(s),ca:n(r),unique:e,pg:t},this.sendRequest("",t)))},t.prototype.debug=function(t){var e;null!==(e=this.options)&&void 0!==e&&e.debug&&console.log("[Swetrix]",t)},t.prototype.canTrack=function(){var t,e;return null!==(t=this.options)&&void 0!==t&&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)&&void 0!==t&&t.respectDNT&&"1"===(null===(e=window.navigator)||void 0===e?void 0:e.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!!(null!==(e=this.options)&&void 0!==e&&e.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))},t.prototype.sendRequest=function(t,e){var i=(null===(n=this.options)||void 0===n?void 0:n.apiURL)||"https://api.swetrix.com/log",n=new XMLHttpRequest;n.open("POST","".concat(i,"/").concat(t),!0),n.setRequestHeader("Content-Type","application/json"),n.send(JSON.stringify(e))},t);function t(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this)}i.LIB_INSTANCE=null,i.init=function(t,e){return i.LIB_INSTANCE||(i.LIB_INSTANCE=new u(t,e)),i.LIB_INSTANCE},i.track=function(t){i.LIB_INSTANCE&&i.LIB_INSTANCE.track(t)},i.trackViews=function(t){return i.LIB_INSTANCE?i.LIB_INSTANCE.trackPageViews(t):c},Object.defineProperty(i,"__esModule",{value:!0})});
package/package.json CHANGED
@@ -1,24 +1,25 @@
1
1
  {
2
2
  "name": "swetrix",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "The JavaScript analytics client for Swetrix Analytics",
5
5
  "main": "dist/swetrix.cjs.js",
6
6
  "module": "dist/swetrix.es5.js",
7
7
  "browser": "dist/swetrix.js",
8
8
  "esnext": "dist/esnext/index.js",
9
9
  "typings": "dist/esnext/index.d.ts",
10
- "scripts": {
11
- "prebuild": "rimraf dist",
12
- "prepublish": "npm run build",
13
- "build": "rollup -c && tsc -p tsconfig.esnext.json",
14
- "start": "rollup -c -w",
15
- "tsc": "tsc -p tsconfig.esnext.json"
16
- },
10
+ "keywords": [
11
+ "swetrix",
12
+ "analytics",
13
+ "monitoring",
14
+ "metrics",
15
+ "privacy"
16
+ ],
17
17
  "repository": {
18
18
  "type": "git",
19
19
  "url": "git+https://github.com/Swetrix/swetrix-js.git"
20
20
  },
21
21
  "author": "Andrii R. <contact@swetrix.com>",
22
+ "funding": "https://ko-fi.com/andriir",
22
23
  "license": "MIT",
23
24
  "bugs": {
24
25
  "url": "https://github.com/Swetrix/swetrix-js/issues"
@@ -37,5 +38,12 @@
37
38
  "rollup-plugin-typescript2": "^0.30.0",
38
39
  "rollup-plugin-uglify": "^6.0.4",
39
40
  "typescript": "^4.5.3"
41
+ },
42
+ "scripts": {
43
+ "prebuild": "rimraf dist",
44
+ "prepublish": "npm run build",
45
+ "build": "rollup -c && tsc -p tsconfig.esnext.json",
46
+ "start": "rollup -c -w",
47
+ "tsc": "tsc -p tsconfig.esnext.json"
40
48
  }
41
- }
49
+ }
package/src/Lib.ts CHANGED
@@ -4,53 +4,71 @@ import {
4
4
  } from './utils'
5
5
 
6
6
  export interface LibOptions {
7
- // When set to `true`, all tracking logs will be
8
- // printed to console and localhost events will be sent to server.
7
+ /**
8
+ * When set to `true`, all tracking logs will be printed to console and localhost events will be sent to server.
9
+ */
9
10
  debug?: boolean
10
11
 
11
- // When set to `true`, the tracking library won't send any data to server.
12
- // Useful for development purposes when this value is set based on `.env` var.
12
+ /**
13
+ * When set to `true`, the tracking library won't send any data to server.
14
+ * Useful for development purposes when this value is set based on `.env` var.
15
+ */
13
16
  disabled?: boolean
14
17
 
15
- // By setting this flag to `true`, we will not collect ANY kind of data about the user
16
- // with the DNT setting.
18
+ /**
19
+ * By setting this flag to `true`, we will not collect ANY kind of data about the user with the DNT setting.
20
+ */
17
21
  respectDNT?: boolean
18
22
 
19
- // Set a custom URL of the API server (for selfhosted variants of Swetrix)
23
+ /** Set a custom URL of the API server (for selfhosted variants of Swetrix). */
20
24
  apiURL?: string
21
25
  }
22
26
 
23
27
  export interface TrackEventOptions {
24
- // The event name
28
+ /** The custom event name. */
25
29
  ev: string
26
30
 
27
- // If true, only 1 event with the same ID will be saved per user session
31
+ /** If set to `true`, only 1 event with the same ID will be saved per user session. */
28
32
  unique?: boolean
29
33
  }
30
34
 
35
+ /**
36
+ * The object returned by `trackPageViews()`, used to stop tracking pages.
37
+ */
38
+ export interface PageActions {
39
+ /** Stops the tracking of pages. */
40
+ stop: () => void
41
+ }
42
+
31
43
  export interface PageData {
32
- // Current URL path
44
+ /** Current URL path. */
33
45
  path: string
34
46
 
35
- // Object with actions related to tracking page views which are abailable to end user
36
- actions: object
47
+ /** The object returned by `trackPageViews()`, used to stop tracking pages. */
48
+ actions: PageActions
37
49
  }
38
50
 
39
51
  export interface PageViewsOptions {
40
- // If true, only unique events will be saved
41
- // This param is useful when tracking single-page landing websites
52
+ /**
53
+ * If set to `true`, only unique events will be saved.
54
+ * This param is useful when tracking single-page landing websites.
55
+ */
42
56
  unique?: boolean
43
57
 
44
- // A list of Regular Expressions or string pathes to ignore
45
- ignore?: Array<any>
58
+ /** A list of Regular Expressions or string pathes to ignore. */
59
+ ignore?: Array<string | RegExp>
46
60
 
47
- // Do not send Heartbeat requests to the server
61
+ /** Do not send Heartbeat requests to the server. */
48
62
  noHeartbeat?: boolean
49
63
 
50
- // Send Heartbeat requests when the website tab is not active in the browser
64
+ /** Send Heartbeat requests when the website tab is not active in the browser. */
51
65
  heartbeatOnBackground?: boolean
52
66
  }
53
67
 
68
+ export const defaultPageActions = {
69
+ stop() {},
70
+ }
71
+
54
72
  const DEFAULT_API_HOST = 'https://api.swetrix.com/log'
55
73
 
56
74
  export class Lib {
@@ -74,9 +92,9 @@ export class Lib {
74
92
  this.sendRequest('custom', data)
75
93
  }
76
94
 
77
- trackPageViews(options?: PageViewsOptions): void | object {
95
+ trackPageViews(options?: PageViewsOptions): PageActions {
78
96
  if (!this.canTrack()) {
79
- return
97
+ return defaultPageActions
80
98
  }
81
99
 
82
100
  if (this.pageData) {
@@ -128,6 +146,7 @@ export class Lib {
128
146
  if (Array.isArray(ignore)) {
129
147
  for (let i = 0; i < ignore.length; ++i) {
130
148
  if (ignore[i] === path) return true
149
+ // @ts-ignore
131
150
  if (ignore[i] instanceof RegExp && ignore[i].test(path)) return true
132
151
  }
133
152
  }
package/src/index.ts CHANGED
@@ -1,9 +1,16 @@
1
- import { Lib, LibOptions, TrackEventOptions, PageViewsOptions } from './Lib'
1
+ import {
2
+ Lib, LibOptions, TrackEventOptions, PageViewsOptions, PageActions, defaultPageActions,
3
+ } from './Lib'
2
4
 
3
5
  export let LIB_INSTANCE: Lib | null = null
4
6
 
5
- // Initialise the tracking library instance (other methods won't work if the library
6
- // is not initialised)
7
+ /**
8
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
9
+ *
10
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
11
+ * @param {LibOptions} options Options related to the tracking.
12
+ * @returns {Lib} Instance of the Swetrix.js.
13
+ */
7
14
  export function init (pid: string, options?: LibOptions): Lib {
8
15
  if (!LIB_INSTANCE) {
9
16
  LIB_INSTANCE = new Lib(pid, options)
@@ -12,15 +19,29 @@ export function init (pid: string, options?: LibOptions): Lib {
12
19
  return LIB_INSTANCE
13
20
  }
14
21
 
15
- // Tracks custom events
22
+ /**
23
+ * With this function you are able to track any custom events you want.
24
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
25
+ * The total number of track calls and their conversion rate will be saved.
26
+ *
27
+ * @param {TrackEventOptions} event The options related to the custom event.
28
+ */
16
29
  export function track (event: TrackEventOptions): void {
17
30
  if (!LIB_INSTANCE) return
18
31
 
19
32
  LIB_INSTANCE.track(event)
20
33
  }
21
34
 
22
- export function trackViews (options?: PageViewsOptions): void {
23
- if (!LIB_INSTANCE) return
35
+ /**
36
+ * With this function you are able to track any custom events you want.
37
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
38
+ * The total number of track calls and their conversion rate will be saved.
39
+ *
40
+ * @param {PageViewsOptions} options The options related to the custom event.
41
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
42
+ */
43
+ export function trackViews (options?: PageViewsOptions): PageActions {
44
+ if (!LIB_INSTANCE) return defaultPageActions
24
45
 
25
- LIB_INSTANCE.trackPageViews(options)
26
- }
46
+ return LIB_INSTANCE.trackPageViews(options)
47
+ }