@vllnt/analytics 0.1.1 → 0.2.0-canary.0cf6be9

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,33 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { AnalyticsConfig, ConsentState, ConsentCategory, UseConsentOptions, UseConsentReturn } from './index.cjs';
4
+ export { h as initAnalytics } from './analytics-BGm5JlCM.cjs';
5
+
6
+ type AnalyticsContextValue = {
7
+ acceptAll: () => void;
8
+ config: AnalyticsConfig;
9
+ consent: ConsentState | null;
10
+ declineAnalytics: () => void;
11
+ hasResponded: boolean;
12
+ isLoading: boolean;
13
+ resetConsent: () => void;
14
+ updateConsent: (category: ConsentCategory, value: boolean) => void;
15
+ };
16
+ /**
17
+ * Analytics provider component
18
+ */
19
+ declare function AnalyticsProvider({ children, config, consentVersion, }: {
20
+ children: ReactNode;
21
+ config: AnalyticsConfig;
22
+ consentVersion?: number;
23
+ }): react.JSX.Element;
24
+ /**
25
+ * Hook to access analytics context
26
+ */
27
+ declare function useAnalytics(): AnalyticsContextValue;
28
+ /**
29
+ * Hook to manage consent state
30
+ */
31
+ declare function useConsent(options?: UseConsentOptions): UseConsentReturn;
32
+
33
+ export { AnalyticsProvider, useAnalytics, useConsent };
@@ -0,0 +1,33 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { AnalyticsConfig, ConsentState, ConsentCategory, UseConsentOptions, UseConsentReturn } from './index.js';
4
+ export { h as initAnalytics } from './analytics-BGm5JlCM.js';
5
+
6
+ type AnalyticsContextValue = {
7
+ acceptAll: () => void;
8
+ config: AnalyticsConfig;
9
+ consent: ConsentState | null;
10
+ declineAnalytics: () => void;
11
+ hasResponded: boolean;
12
+ isLoading: boolean;
13
+ resetConsent: () => void;
14
+ updateConsent: (category: ConsentCategory, value: boolean) => void;
15
+ };
16
+ /**
17
+ * Analytics provider component
18
+ */
19
+ declare function AnalyticsProvider({ children, config, consentVersion, }: {
20
+ children: ReactNode;
21
+ config: AnalyticsConfig;
22
+ consentVersion?: number;
23
+ }): react.JSX.Element;
24
+ /**
25
+ * Hook to access analytics context
26
+ */
27
+ declare function useAnalytics(): AnalyticsContextValue;
28
+ /**
29
+ * Hook to manage consent state
30
+ */
31
+ declare function useConsent(options?: UseConsentOptions): UseConsentReturn;
32
+
33
+ export { AnalyticsProvider, useAnalytics, useConsent };
package/dist/react.js ADDED
@@ -0,0 +1,177 @@
1
+ "use client";
2
+ import {
3
+ CONSENT_VERSION,
4
+ clearConsent,
5
+ createAcceptAllConsent,
6
+ createDeclineAnalyticsConsent,
7
+ disableAnalytics,
8
+ enableAnalytics,
9
+ initAnalytics,
10
+ loadConsent,
11
+ needsRePrompt,
12
+ saveConsent,
13
+ setMaxQueuedEvents,
14
+ updateConsentCategory
15
+ } from "./chunk-UVPDJSPZ.js";
16
+
17
+ // src/react.tsx
18
+ import {
19
+ createContext,
20
+ useCallback,
21
+ useContext,
22
+ useEffect,
23
+ useMemo,
24
+ useState
25
+ } from "react";
26
+ import { jsx } from "react/jsx-runtime";
27
+ var AnalyticsContext = createContext(null);
28
+ function AnalyticsProvider({
29
+ children,
30
+ config,
31
+ consentVersion = CONSENT_VERSION
32
+ }) {
33
+ const [consent, setConsent] = useState(() => {
34
+ setMaxQueuedEvents(config.maxQueuedEvents);
35
+ const stored = loadConsent();
36
+ if (!stored) return null;
37
+ if (needsRePrompt(stored, consentVersion)) {
38
+ clearConsent();
39
+ return null;
40
+ }
41
+ return stored;
42
+ });
43
+ const [hasResponded, setHasResponded] = useState(() => consent !== null);
44
+ useEffect(() => {
45
+ if (!consent) return;
46
+ if (consent.analytics) {
47
+ initAnalytics(config);
48
+ } else {
49
+ disableAnalytics();
50
+ }
51
+ }, [consent, config]);
52
+ const acceptAll = useCallback(() => {
53
+ const newConsent = { ...createAcceptAllConsent(), version: consentVersion };
54
+ saveConsent(newConsent);
55
+ setConsent(newConsent);
56
+ setHasResponded(true);
57
+ enableAnalytics(config);
58
+ }, [config, consentVersion]);
59
+ const declineAnalytics = useCallback(() => {
60
+ const newConsent = {
61
+ ...createDeclineAnalyticsConsent(),
62
+ version: consentVersion
63
+ };
64
+ saveConsent(newConsent);
65
+ setConsent(newConsent);
66
+ setHasResponded(true);
67
+ disableAnalytics();
68
+ }, [consentVersion]);
69
+ const updateConsentHandler = useCallback(
70
+ (category, value2) => {
71
+ if (!consent) return;
72
+ const newConsent = updateConsentCategory(consent, category, value2);
73
+ saveConsent(newConsent);
74
+ setConsent(newConsent);
75
+ if (category === "analytics") {
76
+ if (value2) {
77
+ enableAnalytics(config);
78
+ } else {
79
+ disableAnalytics();
80
+ }
81
+ }
82
+ },
83
+ [consent, config]
84
+ );
85
+ const resetConsentHandler = useCallback(() => {
86
+ clearConsent();
87
+ setConsent(null);
88
+ setHasResponded(false);
89
+ disableAnalytics();
90
+ }, []);
91
+ const value = useMemo(
92
+ () => ({
93
+ acceptAll,
94
+ config,
95
+ consent,
96
+ declineAnalytics,
97
+ hasResponded,
98
+ isLoading: false,
99
+ resetConsent: resetConsentHandler,
100
+ updateConsent: updateConsentHandler
101
+ }),
102
+ [
103
+ config,
104
+ consent,
105
+ hasResponded,
106
+ acceptAll,
107
+ declineAnalytics,
108
+ updateConsentHandler,
109
+ resetConsentHandler
110
+ ]
111
+ );
112
+ return /* @__PURE__ */ jsx(AnalyticsContext.Provider, { value, children });
113
+ }
114
+ function useAnalytics() {
115
+ const context = useContext(AnalyticsContext);
116
+ if (!context) {
117
+ throw new Error("useAnalytics must be used within an AnalyticsProvider");
118
+ }
119
+ return context;
120
+ }
121
+ function useConsent(options = {}) {
122
+ const { consentVersion = CONSENT_VERSION } = options;
123
+ const [consent, setConsent] = useState(() => {
124
+ const stored = loadConsent();
125
+ if (!stored) return null;
126
+ if (needsRePrompt(stored, consentVersion)) {
127
+ clearConsent();
128
+ return null;
129
+ }
130
+ return stored;
131
+ });
132
+ const [hasResponded, setHasResponded] = useState(() => consent !== null);
133
+ const acceptAll = useCallback(() => {
134
+ const newConsent = { ...createAcceptAllConsent(), version: consentVersion };
135
+ saveConsent(newConsent);
136
+ setConsent(newConsent);
137
+ setHasResponded(true);
138
+ }, [consentVersion]);
139
+ const declineAnalytics = useCallback(() => {
140
+ const newConsent = {
141
+ ...createDeclineAnalyticsConsent(),
142
+ version: consentVersion
143
+ };
144
+ saveConsent(newConsent);
145
+ setConsent(newConsent);
146
+ setHasResponded(true);
147
+ }, [consentVersion]);
148
+ const updateConsentHandler = useCallback(
149
+ (category, value) => {
150
+ if (!consent) return;
151
+ const newConsent = updateConsentCategory(consent, category, value);
152
+ saveConsent(newConsent);
153
+ setConsent(newConsent);
154
+ },
155
+ [consent]
156
+ );
157
+ const resetConsentHandler = useCallback(() => {
158
+ clearConsent();
159
+ setConsent(null);
160
+ setHasResponded(false);
161
+ }, []);
162
+ return {
163
+ acceptAll,
164
+ consent,
165
+ declineAnalytics,
166
+ hasResponded,
167
+ isLoading: false,
168
+ resetConsent: resetConsentHandler,
169
+ updateConsent: updateConsentHandler
170
+ };
171
+ }
172
+ export {
173
+ AnalyticsProvider,
174
+ initAnalytics,
175
+ useAnalytics,
176
+ useConsent
177
+ };