react-event-tracking 1.0.6 → 1.0.7
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 +11 -2
- package/dist/index.cjs +58 -44
- package/dist/index.d.cts +7 -2
- package/dist/index.d.mts +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.mjs +58 -44
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,6 +5,9 @@ A convenient React context for tracking analytics events.
|
|
|
5
5
|
|
|
6
6
|
- **Nested Contexts**: Automatically merges parameters from parent providers.
|
|
7
7
|
- **Zero Re-renders**: No need to wrap props in `useCallback`/`useMemo`.
|
|
8
|
+
- **Multiple Providers**: Send events to different analytics services.
|
|
9
|
+
- **Event Filtering**: Control which events are sent to which provider.
|
|
10
|
+
- **Event Transformation**: Modify event names or parameters before they are sent to provider.
|
|
8
11
|
|
|
9
12
|
## Table of Contents
|
|
10
13
|
|
|
@@ -55,7 +58,6 @@ const Dashboard = () => (
|
|
|
55
58
|
```
|
|
56
59
|
|
|
57
60
|
3. Send events conveniently. On button click, parameters will be merged.
|
|
58
|
-
|
|
59
61
|
```tsx
|
|
60
62
|
import { useTracker } from 'react-event-tracking';
|
|
61
63
|
|
|
@@ -63,10 +65,17 @@ const MyButton = () => {
|
|
|
63
65
|
const { sendEvent } = useTracker();
|
|
64
66
|
|
|
65
67
|
return (
|
|
68
|
+
<>
|
|
66
69
|
// event sent with parameters: { screen: 'dashboard', button_id: '123' }
|
|
67
70
|
<button onClick={() => sendEvent('click', { button_id: '123' })}>
|
|
68
71
|
Click me
|
|
69
72
|
</button>
|
|
73
|
+
|
|
74
|
+
{/* Option B: Object call */}
|
|
75
|
+
<button onClick={() => sendEvent({ eventName: 'click', params: { button_id: '456' } })}>
|
|
76
|
+
Click me too
|
|
77
|
+
</button>
|
|
78
|
+
</>
|
|
70
79
|
);
|
|
71
80
|
};
|
|
72
81
|
```
|
|
@@ -144,7 +153,7 @@ const AmpltitudeUS = TrackRoot.factory(
|
|
|
144
153
|
(name, params) => {
|
|
145
154
|
if (params?.userRegion === 'EU') {
|
|
146
155
|
// Remove PII (Personally Identifiable Information)
|
|
147
|
-
const { userId, email, ...safeParams } = params
|
|
156
|
+
const { userId, email, ...safeParams } = params;
|
|
148
157
|
return {
|
|
149
158
|
eventName: name,
|
|
150
159
|
params: safeParams
|
package/dist/index.cjs
CHANGED
|
@@ -20,44 +20,51 @@ const TrackRootComponent = ({ onEvent, children, filter, transform }) => {
|
|
|
20
20
|
const onEventRef = useFreshRef(onEvent);
|
|
21
21
|
const filterRef = useFreshRef(filter);
|
|
22
22
|
const transformRef = useFreshRef(transform);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
function sendEvent(eventNameOrObject, eventParams) {
|
|
24
|
+
let eventName;
|
|
25
|
+
let incomingParams;
|
|
26
|
+
if (typeof eventNameOrObject === "object") {
|
|
27
|
+
eventName = eventNameOrObject.eventName;
|
|
28
|
+
incomingParams = eventNameOrObject.params;
|
|
29
|
+
} else {
|
|
30
|
+
eventName = eventNameOrObject;
|
|
31
|
+
incomingParams = eventParams;
|
|
32
|
+
}
|
|
33
|
+
let localName = eventName;
|
|
34
|
+
let localParams = incomingParams || EmptyParams;
|
|
35
|
+
let shouldProcessLocal = true;
|
|
36
|
+
try {
|
|
37
|
+
if (filterRef.current) {
|
|
38
|
+
shouldProcessLocal = filterRef.current(localName, localParams);
|
|
39
|
+
}
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error("TrackRoot filter failed:", error);
|
|
42
|
+
shouldProcessLocal = false;
|
|
43
|
+
}
|
|
44
|
+
if (shouldProcessLocal && transformRef.current) {
|
|
28
45
|
try {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
const paramsCopy = incomingParams ? { ...incomingParams } : EmptyParams;
|
|
47
|
+
const result = transformRef.current(eventName, paramsCopy);
|
|
48
|
+
localName = result.eventName;
|
|
49
|
+
localParams = result.params;
|
|
32
50
|
} catch (error) {
|
|
33
|
-
console.error("TrackRoot
|
|
51
|
+
console.error("TrackRoot transform failed:", error);
|
|
34
52
|
shouldProcessLocal = false;
|
|
35
53
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
} catch (error) {
|
|
43
|
-
console.error("TrackRoot transform failed:", error);
|
|
44
|
-
shouldProcessLocal = false;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (shouldProcessLocal) {
|
|
48
|
-
try {
|
|
49
|
-
onEventRef.current(localName, localParams);
|
|
50
|
-
} catch (error) {
|
|
51
|
-
console.error("TrackRoot onEvent failed:", error);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (parentCtx) {
|
|
55
|
-
parentCtx.sendEvent(eventName, params);
|
|
54
|
+
}
|
|
55
|
+
if (shouldProcessLocal) {
|
|
56
|
+
try {
|
|
57
|
+
onEventRef.current(localName, localParams);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.error("TrackRoot onEvent failed:", error);
|
|
56
60
|
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
}
|
|
62
|
+
if (parentCtx) {
|
|
63
|
+
parentCtx.sendEvent(eventName, incomingParams);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const sendEventCached = React.useCallback(sendEvent, [parentCtx]);
|
|
67
|
+
const value = React.useMemo(() => ({ sendEvent: sendEventCached }), [sendEventCached]);
|
|
61
68
|
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
|
|
62
69
|
};
|
|
63
70
|
const factory = (onEvent, filter, transform) => {
|
|
@@ -78,17 +85,24 @@ const TrackProvider = ({
|
|
|
78
85
|
}) => {
|
|
79
86
|
const ctx = useTracker();
|
|
80
87
|
const paramsRef = useFreshRef(params);
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
function sendEvent(eventNameOrObject, eventParams) {
|
|
89
|
+
let eventName;
|
|
90
|
+
let incomingParams;
|
|
91
|
+
if (typeof eventNameOrObject === "object") {
|
|
92
|
+
eventName = eventNameOrObject.eventName;
|
|
93
|
+
incomingParams = eventNameOrObject.params;
|
|
94
|
+
} else {
|
|
95
|
+
eventName = eventNameOrObject;
|
|
96
|
+
incomingParams = eventParams;
|
|
97
|
+
}
|
|
98
|
+
const currentParams = paramsRef.current;
|
|
99
|
+
ctx.sendEvent(eventName, {
|
|
100
|
+
...currentParams,
|
|
101
|
+
...incomingParams
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const sendEventCached = React.useCallback(sendEvent, [ctx]);
|
|
105
|
+
const value = React.useMemo(() => ({ sendEvent: sendEventCached }), [sendEventCached]);
|
|
92
106
|
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
|
|
93
107
|
};
|
|
94
108
|
function useFreshRef(data) {
|
package/dist/index.d.cts
CHANGED
|
@@ -2,8 +2,13 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
2
2
|
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
4
|
type EventParams = Record<string, any>;
|
|
5
|
-
|
|
6
|
-
sendEvent
|
|
5
|
+
interface TrackContextValue {
|
|
6
|
+
sendEvent(eventName: string, params?: EventParams): void;
|
|
7
|
+
sendEvent(event: EventObject): void;
|
|
8
|
+
}
|
|
9
|
+
type EventObject = {
|
|
10
|
+
eventName: string;
|
|
11
|
+
params?: EventParams;
|
|
7
12
|
};
|
|
8
13
|
type EventFilter = (eventName: string, params: EventParams) => boolean;
|
|
9
14
|
type TransformedEvent = {
|
package/dist/index.d.mts
CHANGED
|
@@ -2,8 +2,13 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
2
2
|
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
4
|
type EventParams = Record<string, any>;
|
|
5
|
-
|
|
6
|
-
sendEvent
|
|
5
|
+
interface TrackContextValue {
|
|
6
|
+
sendEvent(eventName: string, params?: EventParams): void;
|
|
7
|
+
sendEvent(event: EventObject): void;
|
|
8
|
+
}
|
|
9
|
+
type EventObject = {
|
|
10
|
+
eventName: string;
|
|
11
|
+
params?: EventParams;
|
|
7
12
|
};
|
|
8
13
|
type EventFilter = (eventName: string, params: EventParams) => boolean;
|
|
9
14
|
type TransformedEvent = {
|
package/dist/index.d.ts
CHANGED
|
@@ -2,8 +2,13 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
2
2
|
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
4
|
type EventParams = Record<string, any>;
|
|
5
|
-
|
|
6
|
-
sendEvent
|
|
5
|
+
interface TrackContextValue {
|
|
6
|
+
sendEvent(eventName: string, params?: EventParams): void;
|
|
7
|
+
sendEvent(event: EventObject): void;
|
|
8
|
+
}
|
|
9
|
+
type EventObject = {
|
|
10
|
+
eventName: string;
|
|
11
|
+
params?: EventParams;
|
|
7
12
|
};
|
|
8
13
|
type EventFilter = (eventName: string, params: EventParams) => boolean;
|
|
9
14
|
type TransformedEvent = {
|
package/dist/index.mjs
CHANGED
|
@@ -14,44 +14,51 @@ const TrackRootComponent = ({ onEvent, children, filter, transform }) => {
|
|
|
14
14
|
const onEventRef = useFreshRef(onEvent);
|
|
15
15
|
const filterRef = useFreshRef(filter);
|
|
16
16
|
const transformRef = useFreshRef(transform);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
function sendEvent(eventNameOrObject, eventParams) {
|
|
18
|
+
let eventName;
|
|
19
|
+
let incomingParams;
|
|
20
|
+
if (typeof eventNameOrObject === "object") {
|
|
21
|
+
eventName = eventNameOrObject.eventName;
|
|
22
|
+
incomingParams = eventNameOrObject.params;
|
|
23
|
+
} else {
|
|
24
|
+
eventName = eventNameOrObject;
|
|
25
|
+
incomingParams = eventParams;
|
|
26
|
+
}
|
|
27
|
+
let localName = eventName;
|
|
28
|
+
let localParams = incomingParams || EmptyParams;
|
|
29
|
+
let shouldProcessLocal = true;
|
|
30
|
+
try {
|
|
31
|
+
if (filterRef.current) {
|
|
32
|
+
shouldProcessLocal = filterRef.current(localName, localParams);
|
|
33
|
+
}
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error("TrackRoot filter failed:", error);
|
|
36
|
+
shouldProcessLocal = false;
|
|
37
|
+
}
|
|
38
|
+
if (shouldProcessLocal && transformRef.current) {
|
|
22
39
|
try {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
40
|
+
const paramsCopy = incomingParams ? { ...incomingParams } : EmptyParams;
|
|
41
|
+
const result = transformRef.current(eventName, paramsCopy);
|
|
42
|
+
localName = result.eventName;
|
|
43
|
+
localParams = result.params;
|
|
26
44
|
} catch (error) {
|
|
27
|
-
console.error("TrackRoot
|
|
45
|
+
console.error("TrackRoot transform failed:", error);
|
|
28
46
|
shouldProcessLocal = false;
|
|
29
47
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
} catch (error) {
|
|
37
|
-
console.error("TrackRoot transform failed:", error);
|
|
38
|
-
shouldProcessLocal = false;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (shouldProcessLocal) {
|
|
42
|
-
try {
|
|
43
|
-
onEventRef.current(localName, localParams);
|
|
44
|
-
} catch (error) {
|
|
45
|
-
console.error("TrackRoot onEvent failed:", error);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
if (parentCtx) {
|
|
49
|
-
parentCtx.sendEvent(eventName, params);
|
|
48
|
+
}
|
|
49
|
+
if (shouldProcessLocal) {
|
|
50
|
+
try {
|
|
51
|
+
onEventRef.current(localName, localParams);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error("TrackRoot onEvent failed:", error);
|
|
50
54
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
}
|
|
56
|
+
if (parentCtx) {
|
|
57
|
+
parentCtx.sendEvent(eventName, incomingParams);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const sendEventCached = useCallback(sendEvent, [parentCtx]);
|
|
61
|
+
const value = useMemo(() => ({ sendEvent: sendEventCached }), [sendEventCached]);
|
|
55
62
|
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
|
|
56
63
|
};
|
|
57
64
|
const factory = (onEvent, filter, transform) => {
|
|
@@ -72,17 +79,24 @@ const TrackProvider = ({
|
|
|
72
79
|
}) => {
|
|
73
80
|
const ctx = useTracker();
|
|
74
81
|
const paramsRef = useFreshRef(params);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
function sendEvent(eventNameOrObject, eventParams) {
|
|
83
|
+
let eventName;
|
|
84
|
+
let incomingParams;
|
|
85
|
+
if (typeof eventNameOrObject === "object") {
|
|
86
|
+
eventName = eventNameOrObject.eventName;
|
|
87
|
+
incomingParams = eventNameOrObject.params;
|
|
88
|
+
} else {
|
|
89
|
+
eventName = eventNameOrObject;
|
|
90
|
+
incomingParams = eventParams;
|
|
91
|
+
}
|
|
92
|
+
const currentParams = paramsRef.current;
|
|
93
|
+
ctx.sendEvent(eventName, {
|
|
94
|
+
...currentParams,
|
|
95
|
+
...incomingParams
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
const sendEventCached = useCallback(sendEvent, [ctx]);
|
|
99
|
+
const value = useMemo(() => ({ sendEvent: sendEventCached }), [sendEventCached]);
|
|
86
100
|
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
|
|
87
101
|
};
|
|
88
102
|
function useFreshRef(data) {
|