react-event-tracking 1.0.8 → 1.0.10
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 +23 -6
- package/dist/index.cjs +16 -18
- package/dist/index.d.cts +12 -8
- package/dist/index.d.mts +12 -8
- package/dist/index.d.ts +12 -8
- package/dist/index.mjs +17 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ A convenient React context for tracking analytics events.
|
|
|
19
19
|
* [Multiple Trackers & Factory](#multiple-trackers--factory)
|
|
20
20
|
* [Filtering Events](#filtering-events)
|
|
21
21
|
* [Transforming Events](#transforming-events)
|
|
22
|
+
* [TypeScript Generics Support](#typescript-generics-support)
|
|
22
23
|
- [Best Practices](#best-practices)
|
|
23
24
|
- [Built-in Hooks](#built-in-hooks)
|
|
24
25
|
* [useMountEvent](#usemountevent)
|
|
@@ -59,20 +60,20 @@ const Dashboard = () => (
|
|
|
59
60
|
|
|
60
61
|
3. Send events conveniently. On button click, parameters will be merged.
|
|
61
62
|
```tsx
|
|
62
|
-
import {
|
|
63
|
+
import { useReactEventTracking } from 'react-event-tracking';
|
|
63
64
|
|
|
64
65
|
const MyButton = () => {
|
|
65
|
-
const {
|
|
66
|
+
const { track } = useReactEventTracking();
|
|
66
67
|
|
|
67
68
|
return (
|
|
68
69
|
<>
|
|
69
70
|
// event sent with parameters: { screen: 'dashboard', button_id: '123' }
|
|
70
|
-
<button onClick={() =>
|
|
71
|
+
<button onClick={() => track('click', { button_id: '123' })}>
|
|
71
72
|
Click me
|
|
72
73
|
</button>
|
|
73
74
|
|
|
74
75
|
{/* Option B: Object call */}
|
|
75
|
-
<button onClick={() =>
|
|
76
|
+
<button onClick={() => track({ eventName: 'click', params: { button_id: '456' } })}>
|
|
76
77
|
Click me too
|
|
77
78
|
</button>
|
|
78
79
|
</>
|
|
@@ -164,6 +165,22 @@ const AmpltitudeUS = TrackRoot.factory(
|
|
|
164
165
|
);
|
|
165
166
|
```
|
|
166
167
|
|
|
168
|
+
### TypeScript Generics Support
|
|
169
|
+
|
|
170
|
+
`TrackProvider` supports generics for strict typing of the passed parameters.
|
|
171
|
+
|
|
172
|
+
```tsx
|
|
173
|
+
interface ScreenParams {
|
|
174
|
+
screen: "dashboard" | "authScreen"
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const MyPage = () => (
|
|
178
|
+
<TrackProvider<ScreenParams> params={{ screen: 'dashboard' }}>
|
|
179
|
+
<Content />
|
|
180
|
+
</TrackProvider>
|
|
181
|
+
);
|
|
182
|
+
```
|
|
183
|
+
|
|
167
184
|
## Best Practices
|
|
168
185
|
|
|
169
186
|
A common pattern is to layer data from global to specific. Here is how parameters merge:
|
|
@@ -186,12 +203,12 @@ A common pattern is to layer data from global to specific. Here is how parameter
|
|
|
186
203
|
</TrackRoot>
|
|
187
204
|
|
|
188
205
|
// Inside AddToCartButton:
|
|
189
|
-
const {
|
|
206
|
+
const { track } = useReactEventTracking();
|
|
190
207
|
|
|
191
208
|
// 4. EVENT: Action-specific data
|
|
192
209
|
// When clicked, we only pass what changed right now.
|
|
193
210
|
const handleClick = () => {
|
|
194
|
-
|
|
211
|
+
track('add_to_cart', { quantity: 1 });
|
|
195
212
|
};
|
|
196
213
|
```
|
|
197
214
|
|
package/dist/index.cjs
CHANGED
|
@@ -20,10 +20,10 @@ function parseEventArgs(eventNameOrObject, eventParams) {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
const TrackContext = React__default.createContext(null);
|
|
23
|
-
const
|
|
23
|
+
const useReactEventTracking = () => {
|
|
24
24
|
const ctx = React.useContext(TrackContext);
|
|
25
25
|
if (!ctx) {
|
|
26
|
-
throw new Error("
|
|
26
|
+
throw new Error("useReactEventTracking must be used within TrackRoot");
|
|
27
27
|
}
|
|
28
28
|
return ctx;
|
|
29
29
|
};
|
|
@@ -33,7 +33,7 @@ const TrackRootComponent = ({ onEvent, children, filter, transform }) => {
|
|
|
33
33
|
const onEventRef = useFreshRef(onEvent);
|
|
34
34
|
const filterRef = useFreshRef(filter);
|
|
35
35
|
const transformRef = useFreshRef(transform);
|
|
36
|
-
function
|
|
36
|
+
function track(eventNameOrObject, eventParams) {
|
|
37
37
|
const { eventName, params: incomingParams } = parseEventArgs(
|
|
38
38
|
eventNameOrObject,
|
|
39
39
|
eventParams
|
|
@@ -68,20 +68,19 @@ const TrackRootComponent = ({ onEvent, children, filter, transform }) => {
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
if (parentCtx) {
|
|
71
|
-
parentCtx.
|
|
71
|
+
parentCtx.track(eventName, incomingParams);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
const
|
|
75
|
-
const value = React.useMemo(() => ({ sendEvent: sendEventCached }), [sendEventCached]);
|
|
74
|
+
const value = React.useMemo(() => ({ track }), [parentCtx]);
|
|
76
75
|
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
|
|
77
76
|
};
|
|
78
|
-
const factory = (
|
|
77
|
+
const factory = (args) => {
|
|
79
78
|
return (props) => /* @__PURE__ */ React__default.createElement(
|
|
80
79
|
TrackRootComponent,
|
|
81
80
|
{
|
|
82
|
-
onEvent,
|
|
83
|
-
filter,
|
|
84
|
-
transform,
|
|
81
|
+
onEvent: args.onEvent,
|
|
82
|
+
filter: args.filter,
|
|
83
|
+
transform: args.transform,
|
|
85
84
|
...props
|
|
86
85
|
}
|
|
87
86
|
);
|
|
@@ -91,21 +90,20 @@ const TrackProvider = ({
|
|
|
91
90
|
params,
|
|
92
91
|
children
|
|
93
92
|
}) => {
|
|
94
|
-
const ctx =
|
|
93
|
+
const ctx = useReactEventTracking();
|
|
95
94
|
const paramsRef = useFreshRef(params);
|
|
96
|
-
function
|
|
95
|
+
function track(eventNameOrObject, eventParams) {
|
|
97
96
|
const { eventName, params: incomingParams } = parseEventArgs(
|
|
98
97
|
eventNameOrObject,
|
|
99
98
|
eventParams
|
|
100
99
|
);
|
|
101
100
|
const currentParams = paramsRef.current;
|
|
102
|
-
ctx.
|
|
101
|
+
ctx.track(eventName, {
|
|
103
102
|
...currentParams,
|
|
104
103
|
...incomingParams
|
|
105
104
|
});
|
|
106
105
|
}
|
|
107
|
-
const
|
|
108
|
-
const value = React.useMemo(() => ({ sendEvent: sendEventCached }), [sendEventCached]);
|
|
106
|
+
const value = React.useMemo(() => ({ track }), [ctx]);
|
|
109
107
|
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
|
|
110
108
|
};
|
|
111
109
|
function useFreshRef(data) {
|
|
@@ -115,7 +113,7 @@ function useFreshRef(data) {
|
|
|
115
113
|
}
|
|
116
114
|
|
|
117
115
|
function useMountEvent(eventNameOrObject, eventParams) {
|
|
118
|
-
const {
|
|
116
|
+
const { track } = useReactEventTracking();
|
|
119
117
|
const counterRef = React.useRef(0);
|
|
120
118
|
const { eventName, params } = parseEventArgs(eventNameOrObject, eventParams);
|
|
121
119
|
React.useEffect(() => {
|
|
@@ -123,11 +121,11 @@ function useMountEvent(eventNameOrObject, eventParams) {
|
|
|
123
121
|
return;
|
|
124
122
|
}
|
|
125
123
|
counterRef.current++;
|
|
126
|
-
|
|
124
|
+
track(eventName, params);
|
|
127
125
|
}, []);
|
|
128
126
|
}
|
|
129
127
|
|
|
130
128
|
exports.TrackProvider = TrackProvider;
|
|
131
129
|
exports.TrackRoot = TrackRoot;
|
|
132
130
|
exports.useMountEvent = useMountEvent;
|
|
133
|
-
exports.
|
|
131
|
+
exports.useReactEventTracking = useReactEventTracking;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
|
-
type EventParams = Record<string, any
|
|
4
|
+
type EventParams<T extends Record<string, any> = Record<string, any>> = T;
|
|
5
5
|
interface TrackContextValue {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
track(eventName: string, params?: EventParams): void;
|
|
7
|
+
track(event: EventObject): void;
|
|
8
8
|
}
|
|
9
9
|
type EventObject = {
|
|
10
10
|
eventName: string;
|
|
@@ -17,20 +17,24 @@ type TransformedEvent = {
|
|
|
17
17
|
};
|
|
18
18
|
type EventTransformer = (eventName: string, params: EventParams) => TransformedEvent;
|
|
19
19
|
|
|
20
|
-
declare const
|
|
20
|
+
declare const useReactEventTracking: () => TrackContextValue;
|
|
21
21
|
type TrackRootProps = PropsWithChildren<{
|
|
22
22
|
onEvent: (eventName: string, params: EventParams) => void;
|
|
23
23
|
filter?: EventFilter;
|
|
24
24
|
transform?: EventTransformer;
|
|
25
25
|
}>;
|
|
26
26
|
declare const TrackRoot: (({ onEvent, children, filter, transform }: TrackRootProps) => react_jsx_runtime.JSX.Element) & {
|
|
27
|
-
factory: (
|
|
27
|
+
factory: (args: {
|
|
28
|
+
onEvent: (eventName: string, params?: EventParams) => void;
|
|
29
|
+
filter?: EventFilter;
|
|
30
|
+
transform?: EventTransformer;
|
|
31
|
+
}) => (props: Omit<TrackRootProps, "onEvent" | "filter" | "transform">) => react_jsx_runtime.JSX.Element;
|
|
28
32
|
};
|
|
29
|
-
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
30
|
-
params: EventParams
|
|
33
|
+
declare const TrackProvider: <T extends Record<string, any>>({ params, children }: PropsWithChildren<{
|
|
34
|
+
params: EventParams<T>;
|
|
31
35
|
}>) => react_jsx_runtime.JSX.Element;
|
|
32
36
|
|
|
33
37
|
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
34
38
|
declare function useMountEvent(event: EventObject): void;
|
|
35
39
|
|
|
36
|
-
export { TrackProvider, TrackRoot, useMountEvent,
|
|
40
|
+
export { TrackProvider, TrackRoot, useMountEvent, useReactEventTracking };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
|
-
type EventParams = Record<string, any
|
|
4
|
+
type EventParams<T extends Record<string, any> = Record<string, any>> = T;
|
|
5
5
|
interface TrackContextValue {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
track(eventName: string, params?: EventParams): void;
|
|
7
|
+
track(event: EventObject): void;
|
|
8
8
|
}
|
|
9
9
|
type EventObject = {
|
|
10
10
|
eventName: string;
|
|
@@ -17,20 +17,24 @@ type TransformedEvent = {
|
|
|
17
17
|
};
|
|
18
18
|
type EventTransformer = (eventName: string, params: EventParams) => TransformedEvent;
|
|
19
19
|
|
|
20
|
-
declare const
|
|
20
|
+
declare const useReactEventTracking: () => TrackContextValue;
|
|
21
21
|
type TrackRootProps = PropsWithChildren<{
|
|
22
22
|
onEvent: (eventName: string, params: EventParams) => void;
|
|
23
23
|
filter?: EventFilter;
|
|
24
24
|
transform?: EventTransformer;
|
|
25
25
|
}>;
|
|
26
26
|
declare const TrackRoot: (({ onEvent, children, filter, transform }: TrackRootProps) => react_jsx_runtime.JSX.Element) & {
|
|
27
|
-
factory: (
|
|
27
|
+
factory: (args: {
|
|
28
|
+
onEvent: (eventName: string, params?: EventParams) => void;
|
|
29
|
+
filter?: EventFilter;
|
|
30
|
+
transform?: EventTransformer;
|
|
31
|
+
}) => (props: Omit<TrackRootProps, "onEvent" | "filter" | "transform">) => react_jsx_runtime.JSX.Element;
|
|
28
32
|
};
|
|
29
|
-
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
30
|
-
params: EventParams
|
|
33
|
+
declare const TrackProvider: <T extends Record<string, any>>({ params, children }: PropsWithChildren<{
|
|
34
|
+
params: EventParams<T>;
|
|
31
35
|
}>) => react_jsx_runtime.JSX.Element;
|
|
32
36
|
|
|
33
37
|
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
34
38
|
declare function useMountEvent(event: EventObject): void;
|
|
35
39
|
|
|
36
|
-
export { TrackProvider, TrackRoot, useMountEvent,
|
|
40
|
+
export { TrackProvider, TrackRoot, useMountEvent, useReactEventTracking };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
|
-
type EventParams = Record<string, any
|
|
4
|
+
type EventParams<T extends Record<string, any> = Record<string, any>> = T;
|
|
5
5
|
interface TrackContextValue {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
track(eventName: string, params?: EventParams): void;
|
|
7
|
+
track(event: EventObject): void;
|
|
8
8
|
}
|
|
9
9
|
type EventObject = {
|
|
10
10
|
eventName: string;
|
|
@@ -17,20 +17,24 @@ type TransformedEvent = {
|
|
|
17
17
|
};
|
|
18
18
|
type EventTransformer = (eventName: string, params: EventParams) => TransformedEvent;
|
|
19
19
|
|
|
20
|
-
declare const
|
|
20
|
+
declare const useReactEventTracking: () => TrackContextValue;
|
|
21
21
|
type TrackRootProps = PropsWithChildren<{
|
|
22
22
|
onEvent: (eventName: string, params: EventParams) => void;
|
|
23
23
|
filter?: EventFilter;
|
|
24
24
|
transform?: EventTransformer;
|
|
25
25
|
}>;
|
|
26
26
|
declare const TrackRoot: (({ onEvent, children, filter, transform }: TrackRootProps) => react_jsx_runtime.JSX.Element) & {
|
|
27
|
-
factory: (
|
|
27
|
+
factory: (args: {
|
|
28
|
+
onEvent: (eventName: string, params?: EventParams) => void;
|
|
29
|
+
filter?: EventFilter;
|
|
30
|
+
transform?: EventTransformer;
|
|
31
|
+
}) => (props: Omit<TrackRootProps, "onEvent" | "filter" | "transform">) => react_jsx_runtime.JSX.Element;
|
|
28
32
|
};
|
|
29
|
-
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
30
|
-
params: EventParams
|
|
33
|
+
declare const TrackProvider: <T extends Record<string, any>>({ params, children }: PropsWithChildren<{
|
|
34
|
+
params: EventParams<T>;
|
|
31
35
|
}>) => react_jsx_runtime.JSX.Element;
|
|
32
36
|
|
|
33
37
|
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
34
38
|
declare function useMountEvent(event: EventObject): void;
|
|
35
39
|
|
|
36
|
-
export { TrackProvider, TrackRoot, useMountEvent,
|
|
40
|
+
export { TrackProvider, TrackRoot, useMountEvent, useReactEventTracking };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useContext,
|
|
1
|
+
import React, { useContext, useMemo, useRef, useEffect } from 'react';
|
|
2
2
|
|
|
3
3
|
function parseEventArgs(eventNameOrObject, eventParams) {
|
|
4
4
|
if (typeof eventNameOrObject === "object") {
|
|
@@ -14,10 +14,10 @@ function parseEventArgs(eventNameOrObject, eventParams) {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
const TrackContext = React.createContext(null);
|
|
17
|
-
const
|
|
17
|
+
const useReactEventTracking = () => {
|
|
18
18
|
const ctx = useContext(TrackContext);
|
|
19
19
|
if (!ctx) {
|
|
20
|
-
throw new Error("
|
|
20
|
+
throw new Error("useReactEventTracking must be used within TrackRoot");
|
|
21
21
|
}
|
|
22
22
|
return ctx;
|
|
23
23
|
};
|
|
@@ -27,7 +27,7 @@ const TrackRootComponent = ({ onEvent, children, filter, transform }) => {
|
|
|
27
27
|
const onEventRef = useFreshRef(onEvent);
|
|
28
28
|
const filterRef = useFreshRef(filter);
|
|
29
29
|
const transformRef = useFreshRef(transform);
|
|
30
|
-
function
|
|
30
|
+
function track(eventNameOrObject, eventParams) {
|
|
31
31
|
const { eventName, params: incomingParams } = parseEventArgs(
|
|
32
32
|
eventNameOrObject,
|
|
33
33
|
eventParams
|
|
@@ -62,20 +62,19 @@ const TrackRootComponent = ({ onEvent, children, filter, transform }) => {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
if (parentCtx) {
|
|
65
|
-
parentCtx.
|
|
65
|
+
parentCtx.track(eventName, incomingParams);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
const
|
|
69
|
-
const value = useMemo(() => ({ sendEvent: sendEventCached }), [sendEventCached]);
|
|
68
|
+
const value = useMemo(() => ({ track }), [parentCtx]);
|
|
70
69
|
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
|
|
71
70
|
};
|
|
72
|
-
const factory = (
|
|
71
|
+
const factory = (args) => {
|
|
73
72
|
return (props) => /* @__PURE__ */ React.createElement(
|
|
74
73
|
TrackRootComponent,
|
|
75
74
|
{
|
|
76
|
-
onEvent,
|
|
77
|
-
filter,
|
|
78
|
-
transform,
|
|
75
|
+
onEvent: args.onEvent,
|
|
76
|
+
filter: args.filter,
|
|
77
|
+
transform: args.transform,
|
|
79
78
|
...props
|
|
80
79
|
}
|
|
81
80
|
);
|
|
@@ -85,21 +84,20 @@ const TrackProvider = ({
|
|
|
85
84
|
params,
|
|
86
85
|
children
|
|
87
86
|
}) => {
|
|
88
|
-
const ctx =
|
|
87
|
+
const ctx = useReactEventTracking();
|
|
89
88
|
const paramsRef = useFreshRef(params);
|
|
90
|
-
function
|
|
89
|
+
function track(eventNameOrObject, eventParams) {
|
|
91
90
|
const { eventName, params: incomingParams } = parseEventArgs(
|
|
92
91
|
eventNameOrObject,
|
|
93
92
|
eventParams
|
|
94
93
|
);
|
|
95
94
|
const currentParams = paramsRef.current;
|
|
96
|
-
ctx.
|
|
95
|
+
ctx.track(eventName, {
|
|
97
96
|
...currentParams,
|
|
98
97
|
...incomingParams
|
|
99
98
|
});
|
|
100
99
|
}
|
|
101
|
-
const
|
|
102
|
-
const value = useMemo(() => ({ sendEvent: sendEventCached }), [sendEventCached]);
|
|
100
|
+
const value = useMemo(() => ({ track }), [ctx]);
|
|
103
101
|
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
|
|
104
102
|
};
|
|
105
103
|
function useFreshRef(data) {
|
|
@@ -109,7 +107,7 @@ function useFreshRef(data) {
|
|
|
109
107
|
}
|
|
110
108
|
|
|
111
109
|
function useMountEvent(eventNameOrObject, eventParams) {
|
|
112
|
-
const {
|
|
110
|
+
const { track } = useReactEventTracking();
|
|
113
111
|
const counterRef = useRef(0);
|
|
114
112
|
const { eventName, params } = parseEventArgs(eventNameOrObject, eventParams);
|
|
115
113
|
useEffect(() => {
|
|
@@ -117,8 +115,8 @@ function useMountEvent(eventNameOrObject, eventParams) {
|
|
|
117
115
|
return;
|
|
118
116
|
}
|
|
119
117
|
counterRef.current++;
|
|
120
|
-
|
|
118
|
+
track(eventName, params);
|
|
121
119
|
}, []);
|
|
122
120
|
}
|
|
123
121
|
|
|
124
|
-
export { TrackProvider, TrackRoot, useMountEvent,
|
|
122
|
+
export { TrackProvider, TrackRoot, useMountEvent, useReactEventTracking };
|