@faststats/react 0.1.3 → 0.1.5

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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ConsentMode, WebAnalyticsOptions } from "@faststats/web";
1
+ import { ConsentMode, WebAnalyticsOptions, reportError } from "@faststats/web";
2
2
 
3
3
  //#region src/analytics.d.ts
4
4
  type AnalyticsProps = WebAnalyticsOptions;
@@ -18,4 +18,4 @@ declare function useConsentMode(): (mode: ConsentMode) => void;
18
18
  declare function useOptIn(): () => void;
19
19
  declare function useOptOut(): () => void;
20
20
  //#endregion
21
- export { Analytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
21
+ export { Analytics, reportError, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
package/dist/index.js CHANGED
@@ -1,67 +1,84 @@
1
1
  "use client";
2
-
3
- import { WebAnalytics, getInstance } from "@faststats/web";
2
+ import { WebAnalytics, getInstance, reportError } from "@faststats/web";
4
3
  import { useEffect, useRef } from "react";
5
-
6
4
  //#region src/instance.ts
7
5
  let instance = null;
8
6
  function setInstance(wa) {
9
7
  instance = wa;
10
8
  }
11
-
12
9
  //#endregion
13
10
  //#region src/analytics.tsx
14
11
  function Analytics(props) {
15
- const configRef = useRef(props);
16
- configRef.current = props;
12
+ const ownedRef = useRef(null);
13
+ const propsRef = useRef(props);
14
+ propsRef.current = props;
17
15
  useEffect(() => {
18
16
  if (typeof window === "undefined") return;
19
- const wa = getInstance() ?? new WebAnalytics(configRef.current);
20
- const consentMode = configRef.current.consent?.mode;
17
+ if (ownedRef.current) return;
18
+ const existing = getInstance();
19
+ if (existing) existing.destroy();
20
+ const wa = new WebAnalytics(propsRef.current);
21
+ const consentMode = propsRef.current.consent?.mode;
21
22
  if (consentMode !== void 0) wa.setConsentMode(consentMode);
23
+ ownedRef.current = wa;
22
24
  setInstance(wa);
23
- return () => setInstance(null);
25
+ window.__faststats = { getInstance };
26
+ return () => {
27
+ if (ownedRef.current === wa) {
28
+ wa.destroy();
29
+ ownedRef.current = null;
30
+ }
31
+ setInstance(null);
32
+ window.__faststats = void 0;
33
+ };
24
34
  }, []);
35
+ useEffect(() => {
36
+ const consentMode = props.consent?.mode;
37
+ if (consentMode === void 0) return;
38
+ ownedRef.current?.setConsentMode(consentMode);
39
+ }, [props.consent?.mode]);
25
40
  return null;
26
41
  }
27
-
28
42
  //#endregion
29
43
  //#region src/hooks.ts
44
+ function getActiveInstance() {
45
+ if (instance) return instance;
46
+ return null;
47
+ }
30
48
  function useTrack() {
31
49
  return (name, extra) => {
32
- if (instance) instance.track(name, extra ?? {});
50
+ getActiveInstance()?.track(name, extra ?? {});
33
51
  };
34
52
  }
35
53
  function useEvent(name, extra) {
36
54
  return () => {
37
- if (instance) instance.track(name, extra ?? {});
55
+ getActiveInstance()?.track(name, extra ?? {});
38
56
  };
39
57
  }
40
58
  function useIdentify() {
41
59
  return (externalId, email, options) => {
42
- if (instance) instance.identify(externalId, email, options);
60
+ getActiveInstance()?.identify(externalId, email, options);
43
61
  };
44
62
  }
45
63
  function useLogout() {
46
64
  return (resetAnonymousIdentity = true) => {
47
- if (instance) instance.logout(resetAnonymousIdentity);
65
+ getActiveInstance()?.logout(resetAnonymousIdentity);
48
66
  };
49
67
  }
50
68
  function useConsentMode() {
51
69
  return (mode) => {
52
- if (instance) instance.setConsentMode(mode);
70
+ getActiveInstance()?.setConsentMode(mode);
53
71
  };
54
72
  }
55
73
  function useOptIn() {
56
74
  return () => {
57
- if (instance) instance.optIn();
75
+ getActiveInstance()?.optIn();
58
76
  };
59
77
  }
60
78
  function useOptOut() {
61
79
  return () => {
62
- if (instance) instance.optOut();
80
+ getActiveInstance()?.optOut();
63
81
  };
64
82
  }
65
-
66
83
  //#endregion
67
- export { Analytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
84
+ export { Analytics, reportError, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/faststats-dev/web-analytics.git"
6
6
  },
7
- "version": "0.1.3",
7
+ "version": "0.1.5",
8
8
  "type": "module",
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.js",
@@ -24,18 +24,19 @@
24
24
  "dist"
25
25
  ],
26
26
  "scripts": {
27
- "build": "tsdown"
27
+ "build": "tsdown",
28
+ "typecheck": "tsc --noEmit -p tsconfig.json"
28
29
  },
29
30
  "peerDependencies": {
30
31
  "react": ">=18"
31
32
  },
32
33
  "dependencies": {
33
- "@faststats/web": "^0.1.3"
34
+ "@faststats/web": "^0.1.5"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@types/react": "^19.0.0",
37
38
  "react": "^19.0.0",
38
- "tsdown": "^0.20.3",
39
+ "tsdown": "^0.21.4",
39
40
  "typescript": "^5.9.3"
40
41
  }
41
42
  }