react-event-tracking 1.0.3 → 1.0.4
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 +50 -1
- package/dist/index.cjs +3 -2
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +3 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
# react-event-tracking [](https://www.npmjs.com/package/react-event-tracking)
|
|
2
3
|
A convenient React context for tracking analytics events.
|
|
3
4
|
|
|
@@ -18,7 +19,7 @@ yarn add react-event-tracking
|
|
|
18
19
|
|
|
19
20
|
## Quickstart
|
|
20
21
|
|
|
21
|
-
1. Define the root handler (e.g., send to GTM or API)
|
|
22
|
+
1. Define the root handler (e.g., send to GTM, Amplitude or API)
|
|
22
23
|
```tsx
|
|
23
24
|
import { TrackRoot } from 'react-event-tracking';
|
|
24
25
|
|
|
@@ -56,6 +57,54 @@ const MyButton = () => {
|
|
|
56
57
|
};
|
|
57
58
|
```
|
|
58
59
|
|
|
60
|
+
## Best Practices
|
|
61
|
+
|
|
62
|
+
A common pattern is to layer data from global to specific. Here is how parameters merge:
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
// 1. ROOT: Global data (App Version, Environment)
|
|
66
|
+
<TrackRoot onEvent={handleEvent} initialParams={{ appVersion: '1.0.0' }}>
|
|
67
|
+
|
|
68
|
+
{/* 2. PAGE: Screen-level context */}
|
|
69
|
+
<TrackProvider params={{ page: 'ProductDetails', category: 'Shoes' }}>
|
|
70
|
+
|
|
71
|
+
{/* 3. COMPONENT: Widget-level context */}
|
|
72
|
+
<TrackProvider params={{ productId: 'sku-999' }}>
|
|
73
|
+
<AddToCartButton />
|
|
74
|
+
</TrackProvider>
|
|
75
|
+
|
|
76
|
+
</TrackProvider>
|
|
77
|
+
</TrackRoot>
|
|
78
|
+
|
|
79
|
+
// Inside AddToCartButton:
|
|
80
|
+
const { sendEvent } = useTracker();
|
|
81
|
+
|
|
82
|
+
// 4. EVENT: Action-specific data
|
|
83
|
+
// When clicked, we only pass what changed right now.
|
|
84
|
+
const handleClick = () => {
|
|
85
|
+
sendEvent('add_to_cart', { quantity: 1 });
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Resulting Event Payload:**
|
|
90
|
+
The library merges all layers automatically. The handler receives:
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
{
|
|
95
|
+
// From Root
|
|
96
|
+
appVersion: '1.0.0',
|
|
97
|
+
// From Page Provider
|
|
98
|
+
page: 'ProductDetails',
|
|
99
|
+
category: 'Shoes',
|
|
100
|
+
// From Component Provider
|
|
101
|
+
productId: 'sku-999',
|
|
102
|
+
// From Event
|
|
103
|
+
quantity: 1
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
|
|
59
108
|
## Built-in Hooks
|
|
60
109
|
|
|
61
110
|
### useMountEvent
|
package/dist/index.cjs
CHANGED
|
@@ -16,7 +16,8 @@ const useTracker = () => {
|
|
|
16
16
|
};
|
|
17
17
|
const TrackRoot = ({
|
|
18
18
|
onEvent,
|
|
19
|
-
children
|
|
19
|
+
children,
|
|
20
|
+
initialParams
|
|
20
21
|
}) => {
|
|
21
22
|
const onEventRef = React.useRef(onEvent);
|
|
22
23
|
onEventRef.current = onEvent;
|
|
@@ -24,7 +25,7 @@ const TrackRoot = ({
|
|
|
24
25
|
onEventRef.current(eventName, params);
|
|
25
26
|
}, []);
|
|
26
27
|
const value = React.useMemo(() => ({ sendEvent }), [sendEvent]);
|
|
27
|
-
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
|
|
28
|
+
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, /* @__PURE__ */ React__default.createElement(TrackProvider, { params: initialParams || {} }, children));
|
|
28
29
|
};
|
|
29
30
|
const TrackProvider = ({
|
|
30
31
|
params,
|
package/dist/index.d.cts
CHANGED
|
@@ -7,8 +7,9 @@ type TrackContextValue = {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
declare const useTracker: () => TrackContextValue;
|
|
10
|
-
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children, initialParams }: PropsWithChildren<{
|
|
11
11
|
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
|
+
initialParams?: EventParams;
|
|
12
13
|
}>) => react_jsx_runtime.JSX.Element;
|
|
13
14
|
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
14
15
|
params: EventParams;
|
package/dist/index.d.mts
CHANGED
|
@@ -7,8 +7,9 @@ type TrackContextValue = {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
declare const useTracker: () => TrackContextValue;
|
|
10
|
-
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children, initialParams }: PropsWithChildren<{
|
|
11
11
|
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
|
+
initialParams?: EventParams;
|
|
12
13
|
}>) => react_jsx_runtime.JSX.Element;
|
|
13
14
|
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
14
15
|
params: EventParams;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,8 +7,9 @@ type TrackContextValue = {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
declare const useTracker: () => TrackContextValue;
|
|
10
|
-
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children, initialParams }: PropsWithChildren<{
|
|
11
11
|
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
|
+
initialParams?: EventParams;
|
|
12
13
|
}>) => react_jsx_runtime.JSX.Element;
|
|
13
14
|
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
14
15
|
params: EventParams;
|
package/dist/index.mjs
CHANGED
|
@@ -10,7 +10,8 @@ const useTracker = () => {
|
|
|
10
10
|
};
|
|
11
11
|
const TrackRoot = ({
|
|
12
12
|
onEvent,
|
|
13
|
-
children
|
|
13
|
+
children,
|
|
14
|
+
initialParams
|
|
14
15
|
}) => {
|
|
15
16
|
const onEventRef = useRef(onEvent);
|
|
16
17
|
onEventRef.current = onEvent;
|
|
@@ -18,7 +19,7 @@ const TrackRoot = ({
|
|
|
18
19
|
onEventRef.current(eventName, params);
|
|
19
20
|
}, []);
|
|
20
21
|
const value = useMemo(() => ({ sendEvent }), [sendEvent]);
|
|
21
|
-
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
|
|
22
|
+
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, /* @__PURE__ */ React.createElement(TrackProvider, { params: initialParams || {} }, children));
|
|
22
23
|
};
|
|
23
24
|
const TrackProvider = ({
|
|
24
25
|
params,
|