swetrix 1.2.0 → 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/LICENSE CHANGED
@@ -1,201 +1,21 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the purposes
44
- of this License, Derivative Works shall not include works that remain
45
- separable from, or merely link (or bind by name) to the interfaces of,
46
- the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including
49
- the original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright owner
52
- or by an individual or Legal Entity authorized to submit on behalf of
53
- the copyright owner. For the purposes of this definition, "submitted"
54
- means any form of electronic, verbal, or written communication sent
55
- to the Licensor or its representatives, including but not limited to
56
- communication on electronic mailing lists, source code control systems,
57
- and issue tracking systems that are managed by, or on behalf of, the
58
- Licensor for the purpose of discussing and improving the Work, but
59
- excluding communication that is conspicuously marked or otherwise
60
- designated in writing by the copyright owner as "Not a Contribution."
61
-
62
- "Contributor" shall mean Licensor and any individual or Legal Entity
63
- on behalf of whom a Contribution has been received by Licensor and
64
- subsequently incorporated within the Work.
65
-
66
- 2. Grant of Copyright License. Subject to the terms and conditions of
67
- this License, each Contributor hereby grants to You a perpetual,
68
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
- copyright license to reproduce, prepare Derivative Works of,
70
- publicly display, publicly perform, sublicense, and distribute the
71
- Work and such Derivative Works in Source or Object form.
72
-
73
- 3. Grant of Patent License. Subject to the terms and conditions of
74
- this License, each Contributor hereby grants to You a perpetual,
75
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
- (except as stated in this section) patent license to make, have made,
77
- use, offer to sell, sell, import, and otherwise transfer the Work,
78
- where such license applies only to those patent claims licensable
79
- by such Contributor that are necessarily infringed by their
80
- Contribution(s) alone or by combination of their Contribution(s)
81
- with the Work to which such Contribution(s) was submitted. If You
82
- institute patent litigation against any entity (including a
83
- cross-claim or counterclaim in a lawsuit) alleging that the Work
84
- or a Contribution incorporated within the Work constitutes direct
85
- or contributory patent infringement, then any patent licenses
86
- granted to You under this License for that Work shall terminate
87
- as of the date such litigation is filed.
88
-
89
- 4. Redistribution. You may reproduce and distribute copies of the
90
- Work or Derivative Works thereof in any medium, with or without
91
- modifications, and in Source or Object form, provided that You
92
- meet the following conditions:
93
-
94
- (a) You must give any other recipients of the Work or
95
- Derivative Works a copy of this License; and
96
-
97
- (b) You must cause any modified files to carry prominent notices
98
- stating that You changed the files; and
99
-
100
- (c) You must retain, in the Source form of any Derivative Works
101
- that You distribute, all copyright, patent, trademark, and
102
- attribution notices from the Source form of the Work,
103
- excluding those notices that do not pertain to any part of
104
- the Derivative Works; and
105
-
106
- (d) If the Work includes a "NOTICE" text file as part of its
107
- distribution, then any Derivative Works that You distribute must
108
- include a readable copy of the attribution notices contained
109
- within such NOTICE file, excluding those notices that do not
110
- pertain to any part of the Derivative Works, in at least one
111
- of the following places: within a NOTICE text file distributed
112
- as part of the Derivative Works; within the Source form or
113
- documentation, if provided along with the Derivative Works; or,
114
- within a display generated by the Derivative Works, if and
115
- wherever such third-party notices normally appear. The contents
116
- of the NOTICE file are for informational purposes only and
117
- do not modify the License. You may add Your own attribution
118
- notices within Derivative Works that You distribute, alongside
119
- or as an addendum to the NOTICE text from the Work, provided
120
- that such additional attribution notices cannot be construed
121
- as modifying the License.
122
-
123
- You may add Your own copyright statement to Your modifications and
124
- may provide additional or different license terms and conditions
125
- for use, reproduction, or distribution of Your modifications, or
126
- for any such Derivative Works as a whole, provided Your use,
127
- reproduction, and distribution of the Work otherwise complies with
128
- the conditions stated in this License.
129
-
130
- 5. Submission of Contributions. Unless You explicitly state otherwise,
131
- any Contribution intentionally submitted for inclusion in the Work
132
- by You to the Licensor shall be under the terms and conditions of
133
- this License, without any additional terms or conditions.
134
- Notwithstanding the above, nothing herein shall supersede or modify
135
- the terms of any separate license agreement you may have executed
136
- with Licensor regarding such Contributions.
137
-
138
- 6. Trademarks. This License does not grant permission to use the trade
139
- names, trademarks, service marks, or product names of the Licensor,
140
- except as required for reasonable and customary use in describing the
141
- origin of the Work and reproducing the content of the NOTICE file.
142
-
143
- 7. Disclaimer of Warranty. Unless required by applicable law or
144
- agreed to in writing, Licensor provides the Work (and each
145
- Contributor provides its Contributions) on an "AS IS" BASIS,
146
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
- implied, including, without limitation, any warranties or conditions
148
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
- PARTICULAR PURPOSE. You are solely responsible for determining the
150
- appropriateness of using or redistributing the Work and assume any
151
- risks associated with Your exercise of permissions under this License.
152
-
153
- 8. Limitation of Liability. In no event and under no legal theory,
154
- whether in tort (including negligence), contract, or otherwise,
155
- unless required by applicable law (such as deliberate and grossly
156
- negligent acts) or agreed to in writing, shall any Contributor be
157
- liable to You for damages, including any direct, indirect, special,
158
- incidental, or consequential damages of any character arising as a
159
- result of this License or out of the use or inability to use the
160
- Work (including but not limited to damages for loss of goodwill,
161
- work stoppage, computer failure or malfunction, or any and all
162
- other commercial damages or losses), even if such Contributor
163
- has been advised of the possibility of such damages.
164
-
165
- 9. Accepting Warranty or Additional Liability. While redistributing
166
- the Work or Derivative Works thereof, You may choose to offer,
167
- and charge a fee for, acceptance of support, warranty, indemnity,
168
- or other liability obligations and/or rights consistent with this
169
- License. However, in accepting such obligations, You may act only
170
- on Your own behalf and on Your sole responsibility, not on behalf
171
- of any other Contributor, and only if You agree to indemnify,
172
- defend, and hold each Contributor harmless for any liability
173
- incurred by, or claims asserted against, such Contributor by reason
174
- of your accepting any such warranty or additional liability.
175
-
176
- END OF TERMS AND CONDITIONS
177
-
178
- APPENDIX: How to apply the Apache License to your work.
179
-
180
- To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "[]"
182
- replaced with your own identifying information. (Don't include
183
- the brackets!) The text should be enclosed in the appropriate
184
- comment syntax for the file format. We also recommend that a
185
- file or class name and description of purpose be included on the
186
- same "printed page" as the copyright notice for easier
187
- identification within third-party archives.
188
-
189
- Copyright [yyyy] [name of copyright owner]
190
-
191
- Licensed under the Apache License, Version 2.0 (the "License");
192
- you may not use this file except in compliance with the License.
193
- You may obtain a copy of the License at
194
-
195
- http://www.apache.org/licenses/LICENSE-2.0
196
-
197
- Unless required by applicable law or agreed to in writing, software
198
- distributed under the License is distributed on an "AS IS" BASIS,
199
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
- See the License for the specific language governing permissions and
201
- limitations under the License.
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Andrii Romasiun
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,9 @@
1
+ <img src="https://swetrix.com/assets/logo_blue.svg" alt="" height="100" />
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
+
1
7
  # Swetrix Tracking Script
2
8
 
3
9
  This repository contains the analytics script which is used at https://swetrix.com \
@@ -5,3 +11,12 @@ You can find the detailed documentation and use cases at our [docs page](https:/
5
11
 
6
12
  Feel free to contribute to the source code by opening a pull requests. \
7
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
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
+
17
+ # Selfhosted API
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,22 +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). */
16
+ apiURL?: string;
5
17
  }
6
18
  export interface TrackEventOptions {
19
+ /** The custom event name. */
7
20
  ev: string;
21
+ /** If set to `true`, only 1 event with the same ID will be saved per user session. */
8
22
  unique?: boolean;
9
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
+ }
10
31
  export interface PageData {
32
+ /** Current URL path. */
11
33
  path: string;
12
- actions: object;
34
+ /** The object returned by `trackPageViews()`, used to stop tracking pages. */
35
+ actions: PageActions;
13
36
  }
14
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
+ */
15
42
  unique?: boolean;
16
- 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. */
17
46
  noHeartbeat?: boolean;
47
+ /** Send Heartbeat requests when the website tab is not active in the browser. */
18
48
  heartbeatOnBackground?: boolean;
19
49
  }
50
+ export declare const defaultPageActions: {
51
+ stop(): void;
52
+ };
20
53
  export declare class Lib {
21
54
  private projectID;
22
55
  private options?;
@@ -24,7 +57,7 @@ export declare class Lib {
24
57
  private pageViewsOptions;
25
58
  constructor(projectID: string, options?: LibOptions | undefined);
26
59
  track(event: TrackEventOptions): void;
27
- trackPageViews(options?: PageViewsOptions): void | object;
60
+ trackPageViews(options?: PageViewsOptions): PageActions;
28
61
  private heartbeat;
29
62
  private checkIgnore;
30
63
  private trackPathChange;
@@ -1,5 +1,8 @@
1
1
  import { isInBrowser, isLocalhost, isAutomated, getLocale, getTimezone, getReferrer, getUTMCampaign, getUTMMedium, getUTMSource, getPath, } from './utils';
2
- const host = 'https://api.swetrix.com/log';
2
+ export const defaultPageActions = {
3
+ stop() { },
4
+ };
5
+ const DEFAULT_API_HOST = 'https://api.swetrix.com/log';
3
6
  export class Lib {
4
7
  constructor(projectID, options) {
5
8
  this.projectID = projectID;
@@ -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
  }
@@ -104,6 +108,10 @@ export class Lib {
104
108
  }
105
109
  }
106
110
  canTrack() {
111
+ if (this.options?.disabled) {
112
+ this.debug('Tracking disabled: the \'disabled\' setting is set to true.');
113
+ return false;
114
+ }
107
115
  if (!isInBrowser()) {
108
116
  this.debug('Tracking disabled: script does not run in browser environment.');
109
117
  return false;
@@ -122,6 +130,7 @@ export class Lib {
122
130
  return true;
123
131
  }
124
132
  sendRequest(path, body) {
133
+ const host = this.options?.apiURL || DEFAULT_API_HOST;
125
134
  const req = new XMLHttpRequest();
126
135
  req.open('POST', `${host}/${path}`, true);
127
136
  req.setRequestHeader('Content-Type', 'application/json');
@@ -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;AA+ChB,MAAM,IAAI,GAAG,6BAA6B,CAAA;AAE1C,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,CAAA;QACD,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;IAClC,CAAC;IAED,cAAc,CAAC,OAA0B;QACvC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE;YACpB,OAAM;SACP;QAED,IAAI,IAAI,C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AAA;YAC5E,OAAO,KAAK,CAAA;SACb;QAED,IAAI,IAAI,CAAC,OAAO,EAAE,UAAU,IAAI,MAAM,CAAC,SAAS,EAAE,UAAU,KAAK,GAAG,EAAE;YACpE,IAAI,CAAC,KAAK,CAAC,oEAAoE,CAAC,CAAA;YAChF,OAAO,KAAK,CAAA;SACb;QAED,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,EAAE;YACzC,OAAO,KAAK,CAAA;SACb;QAED,IAAI,WAAW,EAAE,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"}
@@ -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,7 +66,10 @@ var getPath = function () {
66
66
  return location.pathname || '';
67
67
  };
68
68
 
69
- var host = 'https://api.swetrix.com/log';
69
+ var defaultPageActions = {
70
+ stop: function () { },
71
+ };
72
+ var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
70
73
  var Lib = /** @class */ (function () {
71
74
  function Lib(projectID, options) {
72
75
  this.projectID = projectID;
@@ -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
  }
@@ -172,16 +176,20 @@ var Lib = /** @class */ (function () {
172
176
  }
173
177
  };
174
178
  Lib.prototype.canTrack = function () {
175
- var _a, _b, _c;
179
+ var _a, _b, _c, _d;
180
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) {
181
+ this.debug('Tracking disabled: the \'disabled\' setting is set to true.');
182
+ return false;
183
+ }
176
184
  if (!isInBrowser()) {
177
185
  this.debug('Tracking disabled: script does not run in browser environment.');
178
186
  return false;
179
187
  }
180
- if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.respectDNT) && ((_b = window.navigator) === null || _b === void 0 ? void 0 : _b.doNotTrack) === '1') {
188
+ if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') {
181
189
  this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
182
190
  return false;
183
191
  }
184
- if (!((_c = this.options) === null || _c === void 0 ? void 0 : _c.debug) && isLocalhost()) {
192
+ if (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.debug) && isLocalhost()) {
185
193
  return false;
186
194
  }
187
195
  if (isAutomated()) {
@@ -191,6 +199,8 @@ var Lib = /** @class */ (function () {
191
199
  return true;
192
200
  };
193
201
  Lib.prototype.sendRequest = function (path, body) {
202
+ var _a;
203
+ var host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST;
194
204
  var req = new XMLHttpRequest();
195
205
  req.open('POST', "".concat(host, "/").concat(path), true);
196
206
  req.setRequestHeader('Content-Type', 'application/json');
@@ -200,24 +210,43 @@ var Lib = /** @class */ (function () {
200
210
  }());
201
211
 
202
212
  exports.LIB_INSTANCE = null;
203
- // Initialise the tracking library instance (other methods won't work if the library
204
- // 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
+ */
205
220
  function init(pid, options) {
206
221
  if (!exports.LIB_INSTANCE) {
207
222
  exports.LIB_INSTANCE = new Lib(pid, options);
208
223
  }
209
224
  return exports.LIB_INSTANCE;
210
225
  }
211
- // 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
+ */
212
233
  function track(event) {
213
234
  if (!exports.LIB_INSTANCE)
214
235
  return;
215
236
  exports.LIB_INSTANCE.track(event);
216
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
+ */
217
246
  function trackViews(options) {
218
247
  if (!exports.LIB_INSTANCE)
219
- return;
220
- exports.LIB_INSTANCE.trackPageViews(options);
248
+ return defaultPageActions;
249
+ return exports.LIB_INSTANCE.trackPageViews(options);
221
250
  }
222
251
 
223
252
  exports.init = init;
@@ -62,7 +62,10 @@ var getPath = function () {
62
62
  return location.pathname || '';
63
63
  };
64
64
 
65
- var host = 'https://api.swetrix.com/log';
65
+ var defaultPageActions = {
66
+ stop: function () { },
67
+ };
68
+ var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
66
69
  var Lib = /** @class */ (function () {
67
70
  function Lib(projectID, options) {
68
71
  this.projectID = projectID;
@@ -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
  }
@@ -168,16 +172,20 @@ var Lib = /** @class */ (function () {
168
172
  }
169
173
  };
170
174
  Lib.prototype.canTrack = function () {
171
- var _a, _b, _c;
175
+ var _a, _b, _c, _d;
176
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) {
177
+ this.debug('Tracking disabled: the \'disabled\' setting is set to true.');
178
+ return false;
179
+ }
172
180
  if (!isInBrowser()) {
173
181
  this.debug('Tracking disabled: script does not run in browser environment.');
174
182
  return false;
175
183
  }
176
- if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.respectDNT) && ((_b = window.navigator) === null || _b === void 0 ? void 0 : _b.doNotTrack) === '1') {
184
+ if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') {
177
185
  this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
178
186
  return false;
179
187
  }
180
- if (!((_c = this.options) === null || _c === void 0 ? void 0 : _c.debug) && isLocalhost()) {
188
+ if (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.debug) && isLocalhost()) {
181
189
  return false;
182
190
  }
183
191
  if (isAutomated()) {
@@ -187,6 +195,8 @@ var Lib = /** @class */ (function () {
187
195
  return true;
188
196
  };
189
197
  Lib.prototype.sendRequest = function (path, body) {
198
+ var _a;
199
+ var host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST;
190
200
  var req = new XMLHttpRequest();
191
201
  req.open('POST', "".concat(host, "/").concat(path), true);
192
202
  req.setRequestHeader('Content-Type', 'application/json');
@@ -196,24 +206,43 @@ var Lib = /** @class */ (function () {
196
206
  }());
197
207
 
198
208
  var LIB_INSTANCE = null;
199
- // Initialise the tracking library instance (other methods won't work if the library
200
- // 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
+ */
201
216
  function init(pid, options) {
202
217
  if (!LIB_INSTANCE) {
203
218
  LIB_INSTANCE = new Lib(pid, options);
204
219
  }
205
220
  return LIB_INSTANCE;
206
221
  }
207
- // 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
+ */
208
229
  function track(event) {
209
230
  if (!LIB_INSTANCE)
210
231
  return;
211
232
  LIB_INSTANCE.track(event);
212
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
+ */
213
242
  function trackViews(options) {
214
243
  if (!LIB_INSTANCE)
215
- return;
216
- LIB_INSTANCE.trackPageViews(options);
244
+ return defaultPageActions;
245
+ return LIB_INSTANCE.trackPageViews(options);
217
246
  }
218
247
 
219
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"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=new XMLHttpRequest;i.open("POST","".concat("https://api.swetrix.com/log","/").concat(t),!0),i.setRequestHeader("Content-Type","application/json"),i.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,41 +1,49 @@
1
1
  {
2
2
  "name": "swetrix",
3
- "version": "1.2.0",
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
- "license": "Apache-2.0",
22
+ "funding": "https://ko-fi.com/andriir",
23
+ "license": "MIT",
23
24
  "bugs": {
24
25
  "url": "https://github.com/Swetrix/swetrix-js/issues"
25
26
  },
26
27
  "homepage": "https://swetrix.com/docs",
27
28
  "dependencies": {
28
- "@types/node": "^14.14.41"
29
+ "@types/node": "^14.18.0"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@rollup/plugin-commonjs": "^21.0.1",
32
33
  "@rollup/plugin-node-resolve": "^13.0.6",
33
34
  "rimraf": "^3.0.2",
34
- "rollup": "^2.60.1",
35
+ "rollup": "^2.61.1",
35
36
  "rollup-plugin-node-resolve": "^5.2.0",
36
37
  "rollup-plugin-sourcemaps": "^0.6.3",
37
38
  "rollup-plugin-typescript2": "^0.30.0",
38
39
  "rollup-plugin-uglify": "^6.0.4",
39
- "typescript": "^4.5.2"
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,51 +4,72 @@ 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
22
+
23
+ /** Set a custom URL of the API server (for selfhosted variants of Swetrix). */
24
+ apiURL?: string
18
25
  }
19
26
 
20
27
  export interface TrackEventOptions {
21
- // The event name
28
+ /** The custom event name. */
22
29
  ev: string
23
30
 
24
- // 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. */
25
32
  unique?: boolean
26
33
  }
27
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
+
28
43
  export interface PageData {
29
- // Current URL path
44
+ /** Current URL path. */
30
45
  path: string
31
46
 
32
- // Object with actions related to tracking page views which are abailable to end user
33
- actions: object
47
+ /** The object returned by `trackPageViews()`, used to stop tracking pages. */
48
+ actions: PageActions
34
49
  }
35
50
 
36
51
  export interface PageViewsOptions {
37
- // If true, only unique events will be saved
38
- // 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
+ */
39
56
  unique?: boolean
40
57
 
41
- // A list of Regular Expressions or string pathes to ignore
42
- ignore?: Array<any>
58
+ /** A list of Regular Expressions or string pathes to ignore. */
59
+ ignore?: Array<string | RegExp>
43
60
 
44
- // Do not send Heartbeat requests to the server
61
+ /** Do not send Heartbeat requests to the server. */
45
62
  noHeartbeat?: boolean
46
63
 
47
- // 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. */
48
65
  heartbeatOnBackground?: boolean
49
66
  }
50
67
 
51
- const host = 'https://api.swetrix.com/log'
68
+ export const defaultPageActions = {
69
+ stop() {},
70
+ }
71
+
72
+ const DEFAULT_API_HOST = 'https://api.swetrix.com/log'
52
73
 
53
74
  export class Lib {
54
75
  private pageData: PageData | null = null
@@ -71,9 +92,9 @@ export class Lib {
71
92
  this.sendRequest('custom', data)
72
93
  }
73
94
 
74
- trackPageViews(options?: PageViewsOptions): void | object {
95
+ trackPageViews(options?: PageViewsOptions): PageActions {
75
96
  if (!this.canTrack()) {
76
- return
97
+ return defaultPageActions
77
98
  }
78
99
 
79
100
  if (this.pageData) {
@@ -125,6 +146,7 @@ export class Lib {
125
146
  if (Array.isArray(ignore)) {
126
147
  for (let i = 0; i < ignore.length; ++i) {
127
148
  if (ignore[i] === path) return true
149
+ // @ts-ignore
128
150
  if (ignore[i] instanceof RegExp && ignore[i].test(path)) return true
129
151
  }
130
152
  }
@@ -170,6 +192,11 @@ export class Lib {
170
192
  }
171
193
 
172
194
  private canTrack(): boolean {
195
+ if (this.options?.disabled) {
196
+ this.debug('Tracking disabled: the \'disabled\' setting is set to true.')
197
+ return false
198
+ }
199
+
173
200
  if (!isInBrowser()) {
174
201
  this.debug('Tracking disabled: script does not run in browser environment.')
175
202
  return false
@@ -193,6 +220,7 @@ export class Lib {
193
220
  }
194
221
 
195
222
  private sendRequest(path: string, body: object): void {
223
+ const host = this.options?.apiURL || DEFAULT_API_HOST
196
224
  const req = new XMLHttpRequest()
197
225
  req.open('POST', `${host}/${path}`, true)
198
226
  req.setRequestHeader('Content-Type', 'application/json')
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
+ }