react-event-tracking 1.0.2 → 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 +62 -12
- package/dist/index.cjs +16 -2
- package/dist/index.d.cts +8 -4
- package/dist/index.d.mts +8 -4
- package/dist/index.d.ts +8 -4
- package/dist/index.mjs +17 -4
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
# react-event-tracking [](https://www.npmjs.com/package/react-event-tracking)
|
|
3
|
+
A convenient React context for tracking analytics events.
|
|
2
4
|
|
|
3
5
|
## Features
|
|
4
6
|
|
|
@@ -9,7 +11,7 @@ A convenient React context for tracking analytics events.
|
|
|
9
11
|
## Installation
|
|
10
12
|
|
|
11
13
|
```
|
|
12
|
-
npm install
|
|
14
|
+
npm install react-event-tracking
|
|
13
15
|
```
|
|
14
16
|
```
|
|
15
17
|
yarn add react-event-tracking
|
|
@@ -17,7 +19,7 @@ yarn add react-event-tracking
|
|
|
17
19
|
|
|
18
20
|
## Quickstart
|
|
19
21
|
|
|
20
|
-
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)
|
|
21
23
|
```tsx
|
|
22
24
|
import { TrackRoot } from 'react-event-tracking';
|
|
23
25
|
|
|
@@ -55,18 +57,66 @@ const MyButton = () => {
|
|
|
55
57
|
};
|
|
56
58
|
```
|
|
57
59
|
|
|
58
|
-
##
|
|
60
|
+
## Best Practices
|
|
59
61
|
|
|
60
|
-
|
|
62
|
+
A common pattern is to layer data from global to specific. Here is how parameters merge:
|
|
61
63
|
|
|
62
64
|
```tsx
|
|
63
|
-
|
|
64
|
-
|
|
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>
|
|
65
78
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}, []);
|
|
79
|
+
// Inside AddToCartButton:
|
|
80
|
+
const { sendEvent } = useTracker();
|
|
69
81
|
|
|
70
|
-
|
|
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
|
|
71
104
|
}
|
|
72
|
-
```
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
## Built-in Hooks
|
|
109
|
+
|
|
110
|
+
### useMountEvent
|
|
111
|
+
|
|
112
|
+
Sends an event when the component mounts.
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { useMountEvent } from 'react-event-tracking';
|
|
116
|
+
|
|
117
|
+
export function DashboardScreen(props) {
|
|
118
|
+
useMountEvent('page_view', { screen: 'dashboard' });
|
|
119
|
+
|
|
120
|
+
return <div>Dashboard</div>;
|
|
121
|
+
}
|
|
122
|
+
```
|
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,
|
|
@@ -47,6 +48,19 @@ const TrackProvider = ({
|
|
|
47
48
|
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
|
|
48
49
|
};
|
|
49
50
|
|
|
51
|
+
function useMountEvent(eventName, params) {
|
|
52
|
+
const { sendEvent } = useTracker();
|
|
53
|
+
const counterRef = React.useRef(0);
|
|
54
|
+
React.useEffect(() => {
|
|
55
|
+
if (counterRef.current > 0) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
counterRef.current++;
|
|
59
|
+
sendEvent(eventName, params);
|
|
60
|
+
}, []);
|
|
61
|
+
}
|
|
62
|
+
|
|
50
63
|
exports.TrackProvider = TrackProvider;
|
|
51
64
|
exports.TrackRoot = TrackRoot;
|
|
65
|
+
exports.useMountEvent = useMountEvent;
|
|
52
66
|
exports.useTracker = useTracker;
|
package/dist/index.d.cts
CHANGED
|
@@ -2,15 +2,19 @@ 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
|
-
|
|
5
|
+
type TrackContextValue = {
|
|
6
6
|
sendEvent: (eventName: string, params?: EventParams) => void;
|
|
7
|
-
}
|
|
7
|
+
};
|
|
8
|
+
|
|
8
9
|
declare const useTracker: () => TrackContextValue;
|
|
9
|
-
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children, initialParams }: PropsWithChildren<{
|
|
10
11
|
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
|
+
initialParams?: EventParams;
|
|
11
13
|
}>) => react_jsx_runtime.JSX.Element;
|
|
12
14
|
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
13
15
|
params: EventParams;
|
|
14
16
|
}>) => react_jsx_runtime.JSX.Element;
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
19
|
+
|
|
20
|
+
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.d.mts
CHANGED
|
@@ -2,15 +2,19 @@ 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
|
-
|
|
5
|
+
type TrackContextValue = {
|
|
6
6
|
sendEvent: (eventName: string, params?: EventParams) => void;
|
|
7
|
-
}
|
|
7
|
+
};
|
|
8
|
+
|
|
8
9
|
declare const useTracker: () => TrackContextValue;
|
|
9
|
-
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children, initialParams }: PropsWithChildren<{
|
|
10
11
|
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
|
+
initialParams?: EventParams;
|
|
11
13
|
}>) => react_jsx_runtime.JSX.Element;
|
|
12
14
|
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
13
15
|
params: EventParams;
|
|
14
16
|
}>) => react_jsx_runtime.JSX.Element;
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
19
|
+
|
|
20
|
+
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,15 +2,19 @@ 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
|
-
|
|
5
|
+
type TrackContextValue = {
|
|
6
6
|
sendEvent: (eventName: string, params?: EventParams) => void;
|
|
7
|
-
}
|
|
7
|
+
};
|
|
8
|
+
|
|
8
9
|
declare const useTracker: () => TrackContextValue;
|
|
9
|
-
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children, initialParams }: PropsWithChildren<{
|
|
10
11
|
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
|
+
initialParams?: EventParams;
|
|
11
13
|
}>) => react_jsx_runtime.JSX.Element;
|
|
12
14
|
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
13
15
|
params: EventParams;
|
|
14
16
|
}>) => react_jsx_runtime.JSX.Element;
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
19
|
+
|
|
20
|
+
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useContext, useRef, useCallback, useMemo } from 'react';
|
|
1
|
+
import React, { useContext, useRef, useCallback, useMemo, useEffect } from 'react';
|
|
2
2
|
|
|
3
3
|
const TrackContext = React.createContext(null);
|
|
4
4
|
const useTracker = () => {
|
|
@@ -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,
|
|
@@ -41,4 +42,16 @@ const TrackProvider = ({
|
|
|
41
42
|
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
|
|
42
43
|
};
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
function useMountEvent(eventName, params) {
|
|
46
|
+
const { sendEvent } = useTracker();
|
|
47
|
+
const counterRef = useRef(0);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (counterRef.current > 0) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
counterRef.current++;
|
|
53
|
+
sendEvent(eventName, params);
|
|
54
|
+
}, []);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-event-tracking",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist"
|
|
6
6
|
],
|
|
7
|
+
"repository": "git@github.com:zeeeeby/react-event-tracking.git",
|
|
7
8
|
"exports": {
|
|
8
9
|
".": {
|
|
9
10
|
"types": "./dist/index.d.ts",
|
|
@@ -13,7 +14,8 @@
|
|
|
13
14
|
},
|
|
14
15
|
"scripts": {
|
|
15
16
|
"build": "tsc --noEmit && unbuild",
|
|
16
|
-
"test": "vitest"
|
|
17
|
+
"test": "vitest",
|
|
18
|
+
"toc": "npx markdown-toc README.md -i"
|
|
17
19
|
},
|
|
18
20
|
"author": "",
|
|
19
21
|
"license": "ISC",
|