@faststats/react 0.13.1 → 0.13.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,3 @@
1
+ import { WebAnalyticsOptions } from "@faststats/web";
2
+ export type AnalyticsProps = WebAnalyticsOptions;
3
+ export declare function Analytics(props: AnalyticsProps): null;
@@ -0,0 +1,34 @@
1
+ "use client";
2
+ import { setInstance } from "./instance.js";
3
+ import { WebAnalytics } from "@faststats/web";
4
+ import { useEffect, useRef } from "react";
5
+ // src/analytics.tsx
6
+ const SDK_NAME = "@faststats/react";
7
+ const SDK_VERSION = "0.13.2";
8
+ function Analytics(props) {
9
+ const ownedRef = useRef(null);
10
+ const propsRef = useRef(props);
11
+ propsRef.current = props;
12
+ useEffect(() => {
13
+ const wa = new WebAnalytics({
14
+ ...propsRef.current,
15
+ sdkName: SDK_NAME,
16
+ sdkVersion: SDK_VERSION
17
+ });
18
+ ownedRef.current = wa;
19
+ setInstance(wa);
20
+ wa.start();
21
+ return () => {
22
+ wa.destroy();
23
+ ownedRef.current = null;
24
+ setInstance(null);
25
+ };
26
+ }, []);
27
+ useEffect(() => {
28
+ const consentMode = props.consent;
29
+ if (consentMode === void 0) return;
30
+ ownedRef.current?.setConsentMode(consentMode);
31
+ }, [props.consent]);
32
+ return null;
33
+ }
34
+ export { Analytics };
package/dist/flags.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { FeatureFlagReason, FeatureFlagResult, FeatureFlags as FeatureFlags$1, FeatureFlagsContext, FeatureFlagsOptions, FeatureFlagsOptions as FeatureFlagsOptions$1, FeatureFlagsStatus } from "@faststats/web/flags";
2
- //#region src/flags.d.ts
3
2
  /** Feature flags that the hooks read once `<Analytics>` has started them. */
4
3
  export declare class FeatureFlags extends FeatureFlags$1 {
5
4
  setup(...args: Parameters<FeatureFlags$1["setup"]>): (options: {
@@ -17,5 +16,4 @@ export declare function useFlag(key: string, defaultValue: number): number;
17
16
  export declare function useFlag<T>(key: string, defaultValue: T): T;
18
17
  /** The running flags instance, re-rendering whenever flags or status change. */
19
18
  export declare function useFeatureFlags(): FeatureFlags | null;
20
- //#endregion
21
19
  export type { FeatureFlagReason, FeatureFlagResult, FeatureFlagsContext, FeatureFlagsOptions, FeatureFlagsStatus };
package/dist/flags.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import { useSyncExternalStore } from "react";
3
3
  import { FeatureFlags as FeatureFlags$1 } from "@faststats/web/flags";
4
- //#region src/flags.ts
4
+ // src/flags.ts
5
5
  let active = null;
6
6
  let version = 0;
7
7
  const listeners = /* @__PURE__ */ new Set();
@@ -41,5 +41,4 @@ function useFeatureFlags() {
41
41
  useSyncExternalStore(subscribe, () => version, () => 0);
42
42
  return active;
43
43
  }
44
- //#endregion
45
44
  export { FeatureFlags, featureFlags, useFeatureFlags, useFlag };
@@ -0,0 +1,9 @@
1
+ import { ConsentMode, IdentifyOptions, IdentifyUser, WebAnalytics } from "@faststats/web";
2
+ export declare function useAnalytics(): WebAnalytics | null;
3
+ export declare function useTrack(): (name: string, properties?: Record<string, unknown>) => void | undefined;
4
+ export declare function useEvent(name: string, properties?: Record<string, unknown>): () => void;
5
+ export declare function useIdentify(): (userOrExternalId: IdentifyUser | string, email?: string, options?: IdentifyOptions) => Promise<boolean>;
6
+ export declare function useLogout(): (resetAnonymousIdentity?: boolean) => void | undefined;
7
+ export declare function useConsentMode(): (mode: ConsentMode) => void | undefined;
8
+ export declare function useOptIn(): () => void | undefined;
9
+ export declare function useOptOut(): () => void | undefined;
package/dist/hooks.js ADDED
@@ -0,0 +1,45 @@
1
+ "use client";
2
+ import { getInstanceSnapshot, subscribeToInstance } from "./instance.js";
3
+ import { useCallback, useSyncExternalStore } from "react";
4
+ // src/hooks.ts
5
+ function useAnalytics() {
6
+ return useSyncExternalStore(subscribeToInstance, getInstanceSnapshot, () => null);
7
+ }
8
+ function useTrack() {
9
+ const analytics = useAnalytics();
10
+ return useCallback((name, properties) => analytics?.track(name, properties), [analytics]);
11
+ }
12
+ function useEvent(name, properties) {
13
+ const track = useTrack();
14
+ return useCallback(() => {
15
+ track(name, properties);
16
+ }, [
17
+ name,
18
+ properties,
19
+ track
20
+ ]);
21
+ }
22
+ function useIdentify() {
23
+ const analytics = useAnalytics();
24
+ return useCallback((userOrExternalId, email, options) => {
25
+ if (!analytics) return Promise.resolve(false);
26
+ return typeof userOrExternalId === "string" ? analytics.identify(userOrExternalId, email, options) : analytics.identify(userOrExternalId);
27
+ }, [analytics]);
28
+ }
29
+ function useLogout() {
30
+ const analytics = useAnalytics();
31
+ return useCallback((resetAnonymousIdentity = true) => analytics?.logout(resetAnonymousIdentity), [analytics]);
32
+ }
33
+ function useConsentMode() {
34
+ const analytics = useAnalytics();
35
+ return useCallback((mode) => analytics?.setConsentMode(mode), [analytics]);
36
+ }
37
+ function useOptIn() {
38
+ const setConsentMode = useConsentMode();
39
+ return useCallback(() => setConsentMode("granted"), [setConsentMode]);
40
+ }
41
+ function useOptOut() {
42
+ const setConsentMode = useConsentMode();
43
+ return useCallback(() => setConsentMode("denied"), [setConsentMode]);
44
+ }
45
+ export { useAnalytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
package/dist/index.d.ts CHANGED
@@ -1,16 +1,4 @@
1
- import { AnalyticsExtension, ConsentMode, ConsentMode as ConsentMode$1, IdentifyOptions, IdentifyOptions as IdentifyOptions$1, IdentifyUser, WebAnalytics, WebAnalyticsOptions, WebAnalyticsOptions as WebAnalyticsOptions$1 } from "@faststats/web";
2
- //#region src/analytics.d.ts
3
- type AnalyticsProps = WebAnalyticsOptions$1;
4
- export declare function Analytics(props: AnalyticsProps): null;
5
- //#endregion
6
- //#region src/hooks.d.ts
7
- export declare function useAnalytics(): WebAnalytics | null;
8
- export declare function useTrack(): (name: string, properties?: Record<string, unknown>) => void | undefined;
9
- export declare function useEvent(name: string, properties?: Record<string, unknown>): () => void;
10
- export declare function useIdentify(): (userOrExternalId: IdentifyUser | string, email?: string, options?: IdentifyOptions$1) => Promise<boolean>;
11
- export declare function useLogout(): (resetAnonymousIdentity?: boolean) => void | undefined;
12
- export declare function useConsentMode(): (mode: ConsentMode$1) => void | undefined;
13
- export declare function useOptIn(): () => void | undefined;
14
- export declare function useOptOut(): () => void | undefined;
15
- //#endregion
16
- export type { AnalyticsExtension, AnalyticsProps, ConsentMode, IdentifyOptions, WebAnalyticsOptions };
1
+ import { Analytics, AnalyticsProps } from "./analytics.js";
2
+ import { useAnalytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack } from "./hooks.js";
3
+ import { AnalyticsExtension, ConsentMode, IdentifyOptions, WebAnalyticsOptions } from "@faststats/web";
4
+ export { Analytics, type AnalyticsExtension, type AnalyticsProps, type ConsentMode, type IdentifyOptions, type WebAnalyticsOptions, useAnalytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
package/dist/index.js CHANGED
@@ -1,93 +1,4 @@
1
1
  "use client";
2
- import { WebAnalytics } from "@faststats/web";
3
- import { useCallback, useEffect, useRef, useSyncExternalStore } from "react";
4
- //#region src/instance.ts
5
- let instance = null;
6
- const listeners = /* @__PURE__ */ new Set();
7
- function subscribeToInstance(onStoreChange) {
8
- listeners.add(onStoreChange);
9
- return () => {
10
- listeners.delete(onStoreChange);
11
- };
12
- }
13
- function getInstanceSnapshot() {
14
- return instance;
15
- }
16
- function setInstance(wa) {
17
- instance = wa;
18
- for (const l of listeners) l();
19
- }
20
- //#endregion
21
- //#region src/analytics.tsx
22
- const SDK_NAME = "@faststats/react";
23
- const SDK_VERSION = "0.13.1";
24
- function Analytics(props) {
25
- const ownedRef = useRef(null);
26
- const propsRef = useRef(props);
27
- propsRef.current = props;
28
- useEffect(() => {
29
- const wa = new WebAnalytics({
30
- ...propsRef.current,
31
- sdkName: SDK_NAME,
32
- sdkVersion: SDK_VERSION
33
- });
34
- ownedRef.current = wa;
35
- setInstance(wa);
36
- wa.start();
37
- return () => {
38
- wa.destroy();
39
- ownedRef.current = null;
40
- setInstance(null);
41
- };
42
- }, []);
43
- useEffect(() => {
44
- const consentMode = props.consent;
45
- if (consentMode === void 0) return;
46
- ownedRef.current?.setConsentMode(consentMode);
47
- }, [props.consent]);
48
- return null;
49
- }
50
- //#endregion
51
- //#region src/hooks.ts
52
- function useAnalytics() {
53
- return useSyncExternalStore(subscribeToInstance, getInstanceSnapshot, () => null);
54
- }
55
- function useTrack() {
56
- const analytics = useAnalytics();
57
- return useCallback((name, properties) => analytics?.track(name, properties), [analytics]);
58
- }
59
- function useEvent(name, properties) {
60
- const track = useTrack();
61
- return useCallback(() => {
62
- track(name, properties);
63
- }, [
64
- name,
65
- properties,
66
- track
67
- ]);
68
- }
69
- function useIdentify() {
70
- const analytics = useAnalytics();
71
- return useCallback((userOrExternalId, email, options) => {
72
- if (!analytics) return Promise.resolve(false);
73
- return typeof userOrExternalId === "string" ? analytics.identify(userOrExternalId, email, options) : analytics.identify(userOrExternalId);
74
- }, [analytics]);
75
- }
76
- function useLogout() {
77
- const analytics = useAnalytics();
78
- return useCallback((resetAnonymousIdentity = true) => analytics?.logout(resetAnonymousIdentity), [analytics]);
79
- }
80
- function useConsentMode() {
81
- const analytics = useAnalytics();
82
- return useCallback((mode) => analytics?.setConsentMode(mode), [analytics]);
83
- }
84
- function useOptIn() {
85
- const setConsentMode = useConsentMode();
86
- return useCallback(() => setConsentMode("granted"), [setConsentMode]);
87
- }
88
- function useOptOut() {
89
- const setConsentMode = useConsentMode();
90
- return useCallback(() => setConsentMode("denied"), [setConsentMode]);
91
- }
92
- //#endregion
2
+ import { Analytics } from "./analytics.js";
3
+ import { useAnalytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack } from "./hooks.js";
93
4
  export { Analytics, useAnalytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
@@ -0,0 +1,17 @@
1
+ // src/instance.ts
2
+ let instance = null;
3
+ const listeners = /* @__PURE__ */ new Set();
4
+ function subscribeToInstance(onStoreChange) {
5
+ listeners.add(onStoreChange);
6
+ return () => {
7
+ listeners.delete(onStoreChange);
8
+ };
9
+ }
10
+ function getInstanceSnapshot() {
11
+ return instance;
12
+ }
13
+ function setInstance(wa) {
14
+ instance = wa;
15
+ for (const l of listeners) l();
16
+ }
17
+ export { getInstanceSnapshot, setInstance, subscribeToInstance };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/faststats-dev/faststats-javascript.git"
6
6
  },
7
- "version": "0.13.1",
7
+ "version": "0.13.2",
8
8
  "type": "module",
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.js",
@@ -51,7 +51,7 @@
51
51
  "react": ">=18"
52
52
  },
53
53
  "dependencies": {
54
- "@faststats/web": "^0.13.1"
54
+ "@faststats/web": "^0.13.2"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@types/node": "^26.6.5",