@thisbefine/analytics 0.1.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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +403 -0
  3. package/dist/core/analytics.d.ts +123 -0
  4. package/dist/core/analytics.d.ts.map +1 -0
  5. package/dist/core/analytics.js +334 -0
  6. package/dist/core/analytics.js.map +1 -0
  7. package/dist/core/errors.d.ts +94 -0
  8. package/dist/core/errors.d.ts.map +1 -0
  9. package/dist/core/errors.js +420 -0
  10. package/dist/core/errors.js.map +1 -0
  11. package/dist/core/logger.d.ts +28 -0
  12. package/dist/core/logger.d.ts.map +1 -0
  13. package/dist/core/logger.js +36 -0
  14. package/dist/core/logger.js.map +1 -0
  15. package/dist/core/logging.d.ts +12 -0
  16. package/dist/core/logging.d.ts.map +1 -0
  17. package/dist/core/logging.js +33 -0
  18. package/dist/core/logging.js.map +1 -0
  19. package/dist/core/privacy.d.ts +53 -0
  20. package/dist/core/privacy.d.ts.map +1 -0
  21. package/dist/core/privacy.js +94 -0
  22. package/dist/core/privacy.js.map +1 -0
  23. package/dist/core/queue.d.ts +59 -0
  24. package/dist/core/queue.d.ts.map +1 -0
  25. package/dist/core/queue.js +263 -0
  26. package/dist/core/queue.js.map +1 -0
  27. package/dist/core/session.d.ts +90 -0
  28. package/dist/core/session.d.ts.map +1 -0
  29. package/dist/core/session.js +246 -0
  30. package/dist/core/session.js.map +1 -0
  31. package/dist/core/storage.d.ts +64 -0
  32. package/dist/core/storage.d.ts.map +1 -0
  33. package/dist/core/storage.js +242 -0
  34. package/dist/core/storage.js.map +1 -0
  35. package/dist/core/types.d.ts +298 -0
  36. package/dist/core/types.d.ts.map +1 -0
  37. package/dist/core/types.js +34 -0
  38. package/dist/core/types.js.map +1 -0
  39. package/dist/core/validation.d.ts +11 -0
  40. package/dist/core/validation.d.ts.map +1 -0
  41. package/dist/core/validation.js +82 -0
  42. package/dist/core/validation.js.map +1 -0
  43. package/dist/core/version.d.ts +2 -0
  44. package/dist/core/version.d.ts.map +1 -0
  45. package/dist/core/version.js +4 -0
  46. package/dist/core/version.js.map +1 -0
  47. package/dist/index.d.ts +45 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +41 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/next/analytics.d.ts +59 -0
  52. package/dist/next/analytics.d.ts.map +1 -0
  53. package/dist/next/analytics.js +87 -0
  54. package/dist/next/analytics.js.map +1 -0
  55. package/dist/next.d.ts +46 -0
  56. package/dist/next.d.ts.map +1 -0
  57. package/dist/next.js +44 -0
  58. package/dist/next.js.map +1 -0
  59. package/dist/react/analytics.d.ts +51 -0
  60. package/dist/react/analytics.d.ts.map +1 -0
  61. package/dist/react/analytics.js +63 -0
  62. package/dist/react/analytics.js.map +1 -0
  63. package/dist/react/bug-report-widget.d.ts +21 -0
  64. package/dist/react/bug-report-widget.d.ts.map +1 -0
  65. package/dist/react/bug-report-widget.js +34 -0
  66. package/dist/react/bug-report-widget.js.map +1 -0
  67. package/dist/react/hooks.d.ts +141 -0
  68. package/dist/react/hooks.d.ts.map +1 -0
  69. package/dist/react/hooks.js +186 -0
  70. package/dist/react/hooks.js.map +1 -0
  71. package/dist/react.d.ts +42 -0
  72. package/dist/react.d.ts.map +1 -0
  73. package/dist/react.js +39 -0
  74. package/dist/react.js.map +1 -0
  75. package/dist/widget/bug-report.d.ts +16 -0
  76. package/dist/widget/bug-report.d.ts.map +1 -0
  77. package/dist/widget/bug-report.js +416 -0
  78. package/dist/widget/bug-report.js.map +1 -0
  79. package/package.json +107 -0
@@ -0,0 +1,186 @@
1
+ "use client";
2
+ import { useCallback } from "react";
3
+ import { getAnalytics } from "../core/analytics";
4
+ /**
5
+ * Get the analytics instance, throwing if not initialized
6
+ */
7
+ const getAnalyticsOrThrow = () => {
8
+ const analytics = getAnalytics();
9
+ if (!analytics) {
10
+ throw new Error("Analytics not initialized. Make sure you have added <Analytics /> to your app.");
11
+ }
12
+ return analytics;
13
+ };
14
+ /**
15
+ * Hook to access the Analytics instance
16
+ *
17
+ * @throws Error if Analytics component has not been mounted
18
+ *
19
+ * @example
20
+ * ```tsx
21
+ * const analytics = useAnalytics();
22
+ *
23
+ * const handleClick = () => {
24
+ * analytics.track('button_clicked', { buttonId: 'signup' });
25
+ * };
26
+ * ```
27
+ */
28
+ export const useAnalytics = () => {
29
+ return getAnalyticsOrThrow();
30
+ };
31
+ /**
32
+ * Hook to get a memoized track function for a specific event
33
+ *
34
+ * This is useful for event handlers where you want to avoid
35
+ * creating new function references on each render.
36
+ *
37
+ * @param eventName - The name of the event to track
38
+ *
39
+ * @example
40
+ * ```tsx
41
+ * const trackSignup = useTrack('signup_clicked');
42
+ *
43
+ * return (
44
+ * <button onClick={() => trackSignup({ location: 'header' })}>
45
+ * Sign Up
46
+ * </button>
47
+ * );
48
+ * ```
49
+ */
50
+ export const useTrack = (eventName) => {
51
+ return useCallback((properties) => {
52
+ getAnalytics()?.track(eventName, properties);
53
+ }, [eventName]);
54
+ };
55
+ /**
56
+ * Hook to identify the current user
57
+ *
58
+ * Returns a memoized identify function.
59
+ *
60
+ * @example
61
+ * ```tsx
62
+ * const identify = useIdentify();
63
+ *
64
+ * // After login
65
+ * identify(user.id, {
66
+ * email: user.email,
67
+ * name: user.name,
68
+ * plan: user.plan,
69
+ * });
70
+ * ```
71
+ */
72
+ export const useIdentify = () => {
73
+ return useCallback((userId, traits) => {
74
+ getAnalytics()?.identify(userId, traits);
75
+ }, []);
76
+ };
77
+ /**
78
+ * Hook to associate the current user with an account/company
79
+ *
80
+ * Returns a memoized group function.
81
+ *
82
+ * @example
83
+ * ```tsx
84
+ * const group = useGroup();
85
+ *
86
+ * // After user selects workspace/company
87
+ * group(workspace.id, {
88
+ * name: workspace.name,
89
+ * plan: workspace.plan,
90
+ * mrr: workspace.mrr,
91
+ * });
92
+ * ```
93
+ */
94
+ export const useGroup = () => {
95
+ return useCallback((accountId, traits) => {
96
+ getAnalytics()?.group(accountId, traits);
97
+ }, []);
98
+ };
99
+ /**
100
+ * Hook to track page views
101
+ *
102
+ * Returns a memoized page function.
103
+ *
104
+ * @example
105
+ * ```tsx
106
+ * const trackPage = usePage();
107
+ *
108
+ * useEffect(() => {
109
+ * trackPage('/dashboard', { section: 'overview' });
110
+ * }, [trackPage]);
111
+ * ```
112
+ */
113
+ export const usePage = () => {
114
+ return useCallback((name, properties) => {
115
+ getAnalytics()?.page(name, properties);
116
+ }, []);
117
+ };
118
+ /**
119
+ * Hook to reset the analytics session (call on logout)
120
+ *
121
+ * @example
122
+ * ```tsx
123
+ * const resetAnalytics = useReset();
124
+ *
125
+ * const handleLogout = () => {
126
+ * resetAnalytics();
127
+ * // ... other logout logic
128
+ * };
129
+ * ```
130
+ */
131
+ export const useReset = () => {
132
+ return useCallback(() => {
133
+ getAnalytics()?.reset();
134
+ }, []);
135
+ };
136
+ /**
137
+ * Hook to get the current user state
138
+ *
139
+ * @example
140
+ * ```tsx
141
+ * const getUser = useGetUser();
142
+ *
143
+ * const handleDebug = () => {
144
+ * console.log(getUser());
145
+ * // { anonymousId: '...', userId: '...', traits: { ... } }
146
+ * };
147
+ * ```
148
+ */
149
+ export const useGetUser = () => {
150
+ return useCallback(() => {
151
+ return getAnalytics()?.getUser();
152
+ }, []);
153
+ };
154
+ /**
155
+ * Hook to capture exceptions
156
+ *
157
+ * @example
158
+ * ```tsx
159
+ * const captureException = useCaptureException();
160
+ *
161
+ * try { ... } catch (error) {
162
+ * captureException(error as Error, { component: 'Checkout' });
163
+ * }
164
+ * ```
165
+ */
166
+ export const useCaptureException = () => {
167
+ return useCallback((error, context) => {
168
+ getAnalytics()?.captureException(error, context);
169
+ }, []);
170
+ };
171
+ /**
172
+ * Hook to send structured log events
173
+ *
174
+ * @example
175
+ * ```tsx
176
+ * const log = useLog();
177
+ *
178
+ * log('User completed onboarding', 'info', { step: 3 });
179
+ * ```
180
+ */
181
+ export const useLog = () => {
182
+ return useCallback((message, level, metadata) => {
183
+ getAnalytics()?.log(message, level, metadata);
184
+ }, []);
185
+ };
186
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/react/hooks.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAIjD;;GAEG;AACH,MAAM,mBAAmB,GAAG,GAAc,EAAE;IAC3C,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IAEjC,IAAI,CAAC,SAAS,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CACd,gFAAgF,CAChF,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AAClB,CAAC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,GAAc,EAAE;IAC3C,OAAO,mBAAmB,EAAE,CAAC;AAC9B,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,SAAiB,EAAE,EAAE;IAC7C,OAAO,WAAW,CACjB,CAAC,UAAoC,EAAE,EAAE;QACxC,YAAY,EAAE,EAAE,KAAK,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;IAC9C,CAAC,EACD,CAAC,SAAS,CAAC,CACX,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,EAAE;IAC/B,OAAO,WAAW,CAAC,CAAC,MAAc,EAAE,MAAmB,EAAE,EAAE;QAC1D,YAAY,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1C,CAAC,EAAE,EAAE,CAAC,CAAC;AACR,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM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CAAC,OAAe,EAAE,KAAe,EAAE,QAAkC,EAAE,EAAE;QACxE,YAAY,EAAE,EAAE,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;IAC/C,CAAC,EACD,EAAE,CACF,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @thisbefine/analytics/react
3
+ *
4
+ * React integration for Thisbefine Analytics.
5
+ *
6
+ * @example
7
+ * ```tsx
8
+ * import { Analytics, useTrack } from '@thisbefine/analytics/react';
9
+ *
10
+ * // Add to your root layout (reads from NEXT_PUBLIC_TBF_API_KEY)
11
+ * function RootLayout({ children }) {
12
+ * return (
13
+ * <html>
14
+ * <body>
15
+ * {children}
16
+ * <Analytics />
17
+ * </body>
18
+ * </html>
19
+ * );
20
+ * }
21
+ *
22
+ * // Use in components
23
+ * function SignupButton() {
24
+ * const trackSignup = useTrack('signup_clicked');
25
+ *
26
+ * return (
27
+ * <button onClick={() => trackSignup({ location: 'header' })}>
28
+ * Sign Up
29
+ * </button>
30
+ * );
31
+ * }
32
+ * ```
33
+ *
34
+ * @packageDocumentation
35
+ */
36
+ export type { AccountTraits, Analytics as AnalyticsInstance, AnalyticsConfig, UserState, UserTraits, } from "./core/types";
37
+ export type { AnalyticsProps } from "./react/analytics";
38
+ export { Analytics } from "./react/analytics";
39
+ export type { BugReportFABProps } from "./react/bug-report-widget";
40
+ export { BugReportFAB } from "./react/bug-report-widget";
41
+ export { useAnalytics, useCaptureException, useGetUser, useGroup, useIdentify, useLog, usePage, useReset, useTrack, } from "./react/hooks";
42
+ //# sourceMappingURL=react.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,YAAY,EACX,aAAa,EACb,SAAS,IAAI,iBAAiB,EAC9B,eAAe,EACf,SAAS,EACT,UAAU,GACV,MAAM,cAAc,CAAC;AAEtB,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,OAAO,EACN,YAAY,EACZ,mBAAmB,EACnB,UAAU,EACV,QAAQ,EACR,WAAW,EACX,MAAM,EACN,OAAO,EACP,QAAQ,EACR,QAAQ,GACR,MAAM,eAAe,CAAC"}
package/dist/react.js ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @thisbefine/analytics/react
3
+ *
4
+ * React integration for Thisbefine Analytics.
5
+ *
6
+ * @example
7
+ * ```tsx
8
+ * import { Analytics, useTrack } from '@thisbefine/analytics/react';
9
+ *
10
+ * // Add to your root layout (reads from NEXT_PUBLIC_TBF_API_KEY)
11
+ * function RootLayout({ children }) {
12
+ * return (
13
+ * <html>
14
+ * <body>
15
+ * {children}
16
+ * <Analytics />
17
+ * </body>
18
+ * </html>
19
+ * );
20
+ * }
21
+ *
22
+ * // Use in components
23
+ * function SignupButton() {
24
+ * const trackSignup = useTrack('signup_clicked');
25
+ *
26
+ * return (
27
+ * <button onClick={() => trackSignup({ location: 'header' })}>
28
+ * Sign Up
29
+ * </button>
30
+ * );
31
+ * }
32
+ * ```
33
+ *
34
+ * @packageDocumentation
35
+ */
36
+ export { Analytics } from "./react/analytics";
37
+ export { BugReportFAB } from "./react/bug-report-widget";
38
+ export { useAnalytics, useCaptureException, useGetUser, useGroup, useIdentify, useLog, usePage, useReset, useTrack, } from "./react/hooks";
39
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAWH,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAG9C,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,OAAO,EACN,YAAY,EACZ,mBAAmB,EACnB,UAAU,EACV,QAAQ,EACR,WAAW,EACX,MAAM,EACN,OAAO,EACP,QAAQ,EACR,QAAQ,GACR,MAAM,eAAe,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { Analytics } from "../core/types";
2
+ export interface BugReportWidgetOptions {
3
+ /** FAB position. Default: "bottom-right" */
4
+ position?: "bottom-right" | "bottom-left";
5
+ /** FAB button color. Default: "#ef4444" (red) */
6
+ buttonColor?: string;
7
+ /** FAB button text. Default: "Report Bug" */
8
+ buttonText?: string;
9
+ }
10
+ /**
11
+ * Create and mount the bug report widget
12
+ */
13
+ export declare const createBugReportWidget: (analytics: Analytics, options?: BugReportWidgetOptions) => {
14
+ destroy: () => void;
15
+ };
16
+ //# sourceMappingURL=bug-report.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bug-report.d.ts","sourceRoot":"","sources":["../../src/widget/bug-report.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE/C,MAAM,WAAW,sBAAsB;IACtC,4CAA4C;IAC5C,QAAQ,CAAC,EAAE,cAAc,GAAG,aAAa,CAAC;IAC1C,iDAAiD;IACjD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AAuND;;GAEG;AACH,eAAO,MAAM,qBAAqB,GACjC,WAAW,SAAS,EACpB,UAAU,sBAAsB,KAC9B;IAAE,OAAO,EAAE,MAAM,IAAI,CAAA;CAgPvB,CAAC"}