@faststats/react 0.0.1
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/README.md +4 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +68 -0
- package/package.json +37 -0
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ConsentMode, WebAnalytics, WebAnalyticsOptions } from "@faststats/web";
|
|
2
|
+
|
|
3
|
+
//#region src/analytics.d.ts
|
|
4
|
+
declare global {
|
|
5
|
+
interface Window {
|
|
6
|
+
__FA_reactAnalyticsInstance?: WebAnalytics;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
interface AnalyticsProps extends WebAnalyticsOptions {
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
}
|
|
12
|
+
declare function Analytics(props: AnalyticsProps): null;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/hooks.d.ts
|
|
15
|
+
declare function useTrack(): (name: string, extra?: Record<string, unknown>) => void;
|
|
16
|
+
declare function useEvent(name: string, extra?: Record<string, unknown>): () => void;
|
|
17
|
+
declare function useIdentify(): (externalId: string, email: string, options?: {
|
|
18
|
+
name?: string;
|
|
19
|
+
phone?: string;
|
|
20
|
+
avatarUrl?: string;
|
|
21
|
+
traits?: Record<string, unknown>;
|
|
22
|
+
}) => void;
|
|
23
|
+
declare function useLogout(): (resetAnonymousIdentity?: boolean) => void;
|
|
24
|
+
declare function useConsentMode(): (mode: ConsentMode) => void;
|
|
25
|
+
declare function useOptIn(): () => void;
|
|
26
|
+
declare function useOptOut(): () => void;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { Analytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { WebAnalytics } from "@faststats/web";
|
|
4
|
+
import { useEffect, useRef } from "react";
|
|
5
|
+
|
|
6
|
+
//#region src/instance.ts
|
|
7
|
+
let instance = null;
|
|
8
|
+
function setInstance(wa) {
|
|
9
|
+
instance = wa;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/analytics.tsx
|
|
14
|
+
function Analytics(props) {
|
|
15
|
+
const configRef = useRef(props);
|
|
16
|
+
configRef.current = props;
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (typeof window === "undefined") return;
|
|
19
|
+
const wa = window.__FA_reactAnalyticsInstance ?? new WebAnalytics(configRef.current);
|
|
20
|
+
const consentMode = configRef.current.consent?.mode;
|
|
21
|
+
if (consentMode !== void 0) wa.setConsentMode(consentMode);
|
|
22
|
+
window.__FA_reactAnalyticsInstance = wa;
|
|
23
|
+
setInstance(wa);
|
|
24
|
+
return () => setInstance(window.__FA_reactAnalyticsInstance ?? null);
|
|
25
|
+
}, []);
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/hooks.ts
|
|
31
|
+
function useTrack() {
|
|
32
|
+
return (name, extra) => {
|
|
33
|
+
if (instance) instance.track(name, extra ?? {});
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function useEvent(name, extra) {
|
|
37
|
+
return () => {
|
|
38
|
+
if (instance) instance.track(name, extra ?? {});
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function useIdentify() {
|
|
42
|
+
return (externalId, email, options) => {
|
|
43
|
+
if (instance) instance.identify(externalId, email, options);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function useLogout() {
|
|
47
|
+
return (resetAnonymousIdentity = true) => {
|
|
48
|
+
if (instance) instance.logout(resetAnonymousIdentity);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function useConsentMode() {
|
|
52
|
+
return (mode) => {
|
|
53
|
+
if (instance) instance.setConsentMode(mode);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function useOptIn() {
|
|
57
|
+
return () => {
|
|
58
|
+
if (instance) instance.optIn();
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function useOptOut() {
|
|
62
|
+
return () => {
|
|
63
|
+
if (instance) instance.optOut();
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
export { Analytics, useConsentMode, useEvent, useIdentify, useLogout, useOptIn, useOptOut, useTrack };
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@faststats/react",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsdown"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@faststats/web": "workspace:*"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/react": "^19.0.0",
|
|
33
|
+
"react": "^19.0.0",
|
|
34
|
+
"tsdown": "^0.20.3",
|
|
35
|
+
"typescript": "^5.9.3"
|
|
36
|
+
}
|
|
37
|
+
}
|