@arthurreira/analytics 0.18.0 → 0.19.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.
@@ -38,8 +38,34 @@ var AfAnalytics = (() => {
38
38
  // src/vanilla.ts
39
39
  var vanilla_exports = {};
40
40
  __export(vanilla_exports, {
41
- init: () => init
41
+ consent: () => consent,
42
+ init: () => init,
43
+ isOptedOut: () => isOptedOut,
44
+ optIn: () => optIn,
45
+ optOut: () => optOut
42
46
  });
47
+
48
+ // src/lib/consent.ts
49
+ var CONSENT_KEY = "af_analytics_opted_out";
50
+ function optOut() {
51
+ if (typeof localStorage === "undefined") return;
52
+ localStorage.setItem(CONSENT_KEY, "1");
53
+ }
54
+ function optIn() {
55
+ if (typeof localStorage === "undefined") return;
56
+ localStorage.removeItem(CONSENT_KEY);
57
+ }
58
+ function isOptedOut() {
59
+ if (typeof localStorage !== "undefined" && localStorage.getItem(CONSENT_KEY) === "1") return true;
60
+ if (typeof navigator !== "undefined" && navigator.doNotTrack === "1") return true;
61
+ return false;
62
+ }
63
+ function consent(enabled) {
64
+ if (enabled) optIn();
65
+ else optOut();
66
+ }
67
+
68
+ // src/vanilla.ts
43
69
  var _currentScript = document.currentScript;
44
70
  var SESSION_EXPIRY_MS = 30 * 60 * 1e3;
45
71
  var IDLE_TOUCH_INTERVAL_MS = 5 * 60 * 1e3;
@@ -97,6 +123,7 @@ var AfAnalytics = (() => {
97
123
  }
98
124
  }
99
125
  function send(apiUrl, apiKey, payload) {
126
+ if (isOptedOut()) return;
100
127
  fetch(`${apiUrl}/events`, {
101
128
  method: "POST",
102
129
  headers: { "X-API-Key": apiKey, "Content-Type": "application/json" },
@@ -235,6 +262,7 @@ var AfAnalytics = (() => {
235
262
  send(apiUrl, apiKey, __spreadValues(__spreadValues({}, baseFields(sessionId, "js_exception")), fields));
236
263
  }
237
264
  function init() {
265
+ if (isOptedOut()) return;
238
266
  const config = readConfig();
239
267
  if (!config) return;
240
268
  const { apiKey, apiUrl } = config;
package/dist/client.js CHANGED
@@ -8,6 +8,14 @@ import { usePathname } from "next/navigation";
8
8
  // src/hooks/useAnalytics.ts
9
9
  import { useEffect, useRef } from "react";
10
10
 
11
+ // src/lib/consent.ts
12
+ var CONSENT_KEY = "af_analytics_opted_out";
13
+ function isOptedOut() {
14
+ if (typeof localStorage !== "undefined" && localStorage.getItem(CONSENT_KEY) === "1") return true;
15
+ if (typeof navigator !== "undefined" && navigator.doNotTrack === "1") return true;
16
+ return false;
17
+ }
18
+
11
19
  // src/lib/api.ts
12
20
  var BASE_FIELDS = (sessionId, eventType, path) => ({
13
21
  session_id: sessionId,
@@ -28,6 +36,7 @@ function getGpu() {
28
36
  }
29
37
  }
30
38
  async function createSession(apiUrl, apiKey, visitorId) {
39
+ if (isOptedOut()) return null;
31
40
  const sessionData = { visitor_id: visitorId };
32
41
  if (typeof window !== "undefined") {
33
42
  const nav = navigator;
@@ -77,6 +86,7 @@ async function createSession(apiUrl, apiKey, visitorId) {
77
86
  }
78
87
  }
79
88
  async function sendEvent(apiUrl, apiKey, payload) {
89
+ if (isOptedOut()) return;
80
90
  await fetch(`${apiUrl}/events`, {
81
91
  method: "POST",
82
92
  headers: {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,12 @@
1
+ declare function optOut(): void;
2
+ declare function optIn(): void;
3
+ /**
4
+ * Returns true when the user has opted out via localStorage flag OR
5
+ * the browser-level DNT header is set to "1".
6
+ */
7
+ declare function isOptedOut(): boolean;
8
+ declare function consent(enabled: boolean): void;
9
+
1
10
  declare function createSession(apiUrl: string, apiKey: string, visitorId?: string): Promise<string | null>;
2
11
  declare function trackPageview(apiUrl: string, apiKey: string, sessionId: string, path: string): Promise<void>;
3
12
  declare function trackClick(apiUrl: string, apiKey: string, sessionId: string, path: string, e: MouseEvent, element: HTMLElement): Promise<void>;
@@ -19,4 +28,4 @@ interface PresenceConnection {
19
28
  }
20
29
  declare function connectPresence(apiKey: string, sessionId: string, wsUrl?: string): PresenceConnection;
21
30
 
22
- export { DEFAULT_WS_URL, type ExceptionFields, type PresenceConnection, connectPresence, createSession, trackCTA, trackClick, trackCopy, trackError, trackException, trackPageview, trackScroll, trackSearch };
31
+ export { DEFAULT_WS_URL, type ExceptionFields, type PresenceConnection, connectPresence, consent, createSession, isOptedOut, optIn, optOut, trackCTA, trackClick, trackCopy, trackError, trackException, trackPageview, trackScroll, trackSearch };
package/dist/index.js CHANGED
@@ -1,3 +1,23 @@
1
+ // src/lib/consent.ts
2
+ var CONSENT_KEY = "af_analytics_opted_out";
3
+ function optOut() {
4
+ if (typeof localStorage === "undefined") return;
5
+ localStorage.setItem(CONSENT_KEY, "1");
6
+ }
7
+ function optIn() {
8
+ if (typeof localStorage === "undefined") return;
9
+ localStorage.removeItem(CONSENT_KEY);
10
+ }
11
+ function isOptedOut() {
12
+ if (typeof localStorage !== "undefined" && localStorage.getItem(CONSENT_KEY) === "1") return true;
13
+ if (typeof navigator !== "undefined" && navigator.doNotTrack === "1") return true;
14
+ return false;
15
+ }
16
+ function consent(enabled) {
17
+ if (enabled) optIn();
18
+ else optOut();
19
+ }
20
+
1
21
  // src/lib/api.ts
2
22
  var BASE_FIELDS = (sessionId, eventType, path) => ({
3
23
  session_id: sessionId,
@@ -18,6 +38,7 @@ function getGpu() {
18
38
  }
19
39
  }
20
40
  async function createSession(apiUrl, apiKey, visitorId) {
41
+ if (isOptedOut()) return null;
21
42
  const sessionData = { visitor_id: visitorId };
22
43
  if (typeof window !== "undefined") {
23
44
  const nav = navigator;
@@ -67,6 +88,7 @@ async function createSession(apiUrl, apiKey, visitorId) {
67
88
  }
68
89
  }
69
90
  async function sendEvent(apiUrl, apiKey, payload) {
91
+ if (isOptedOut()) return;
70
92
  await fetch(`${apiUrl}/events`, {
71
93
  method: "POST",
72
94
  headers: {
@@ -206,7 +228,11 @@ function connectPresence(apiKey, sessionId, wsUrl = DEFAULT_WS_URL) {
206
228
  export {
207
229
  DEFAULT_WS_URL,
208
230
  connectPresence,
231
+ consent,
209
232
  createSession,
233
+ isOptedOut,
234
+ optIn,
235
+ optOut,
210
236
  trackCTA,
211
237
  trackClick,
212
238
  trackCopy,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arthurreira/analytics",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "tsup",