react-event-tracking 1.0.7 → 1.0.9
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 +18 -0
- package/dist/index.cjs +23 -19
- package/dist/index.d.cts +4 -3
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.mjs +23 -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)
|
|
@@ -164,6 +165,23 @@ 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 PageParams {
|
|
174
|
+
screenId: string;
|
|
175
|
+
isAuthorized: boolean;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const MyPage = () => (
|
|
179
|
+
<TrackProvider<PageParams> params={{ screenId: 'main', isAuthorized: true }}>
|
|
180
|
+
<Content />
|
|
181
|
+
</TrackProvider>
|
|
182
|
+
);
|
|
183
|
+
```
|
|
184
|
+
|
|
167
185
|
## Best Practices
|
|
168
186
|
|
|
169
187
|
A common pattern is to layer data from global to specific. Here is how parameters merge:
|
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,19 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
|
|
|
6
6
|
|
|
7
7
|
const React__default = /*#__PURE__*/_interopDefaultCompat(React);
|
|
8
8
|
|
|
9
|
+
function parseEventArgs(eventNameOrObject, eventParams) {
|
|
10
|
+
if (typeof eventNameOrObject === "object") {
|
|
11
|
+
return {
|
|
12
|
+
eventName: eventNameOrObject.eventName,
|
|
13
|
+
params: eventNameOrObject.params
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
eventName: eventNameOrObject,
|
|
18
|
+
params: eventParams
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
9
22
|
const TrackContext = React__default.createContext(null);
|
|
10
23
|
const useTracker = () => {
|
|
11
24
|
const ctx = React.useContext(TrackContext);
|
|
@@ -21,15 +34,10 @@ const TrackRootComponent = ({ onEvent, children, filter, transform }) => {
|
|
|
21
34
|
const filterRef = useFreshRef(filter);
|
|
22
35
|
const transformRef = useFreshRef(transform);
|
|
23
36
|
function sendEvent(eventNameOrObject, eventParams) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
incomingParams = eventNameOrObject.params;
|
|
29
|
-
} else {
|
|
30
|
-
eventName = eventNameOrObject;
|
|
31
|
-
incomingParams = eventParams;
|
|
32
|
-
}
|
|
37
|
+
const { eventName, params: incomingParams } = parseEventArgs(
|
|
38
|
+
eventNameOrObject,
|
|
39
|
+
eventParams
|
|
40
|
+
);
|
|
33
41
|
let localName = eventName;
|
|
34
42
|
let localParams = incomingParams || EmptyParams;
|
|
35
43
|
let shouldProcessLocal = true;
|
|
@@ -86,15 +94,10 @@ const TrackProvider = ({
|
|
|
86
94
|
const ctx = useTracker();
|
|
87
95
|
const paramsRef = useFreshRef(params);
|
|
88
96
|
function sendEvent(eventNameOrObject, eventParams) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
incomingParams = eventNameOrObject.params;
|
|
94
|
-
} else {
|
|
95
|
-
eventName = eventNameOrObject;
|
|
96
|
-
incomingParams = eventParams;
|
|
97
|
-
}
|
|
97
|
+
const { eventName, params: incomingParams } = parseEventArgs(
|
|
98
|
+
eventNameOrObject,
|
|
99
|
+
eventParams
|
|
100
|
+
);
|
|
98
101
|
const currentParams = paramsRef.current;
|
|
99
102
|
ctx.sendEvent(eventName, {
|
|
100
103
|
...currentParams,
|
|
@@ -111,9 +114,10 @@ function useFreshRef(data) {
|
|
|
111
114
|
return ref;
|
|
112
115
|
}
|
|
113
116
|
|
|
114
|
-
function useMountEvent(
|
|
117
|
+
function useMountEvent(eventNameOrObject, eventParams) {
|
|
115
118
|
const { sendEvent } = useTracker();
|
|
116
119
|
const counterRef = React.useRef(0);
|
|
120
|
+
const { eventName, params } = parseEventArgs(eventNameOrObject, eventParams);
|
|
117
121
|
React.useEffect(() => {
|
|
118
122
|
if (counterRef.current > 0) {
|
|
119
123
|
return;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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
6
|
sendEvent(eventName: string, params?: EventParams): void;
|
|
7
7
|
sendEvent(event: EventObject): void;
|
|
@@ -26,10 +26,11 @@ type TrackRootProps = PropsWithChildren<{
|
|
|
26
26
|
declare const TrackRoot: (({ onEvent, children, filter, transform }: TrackRootProps) => react_jsx_runtime.JSX.Element) & {
|
|
27
27
|
factory: (onEvent: (eventName: string, params?: EventParams) => void, filter?: EventFilter, transform?: EventTransformer) => (props: Omit<TrackRootProps, "onEvent" | "filter" | "transform">) => react_jsx_runtime.JSX.Element;
|
|
28
28
|
};
|
|
29
|
-
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
30
|
-
params: EventParams
|
|
29
|
+
declare const TrackProvider: <T extends Record<string, any>>({ params, children }: PropsWithChildren<{
|
|
30
|
+
params: EventParams<T>;
|
|
31
31
|
}>) => react_jsx_runtime.JSX.Element;
|
|
32
32
|
|
|
33
33
|
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
34
|
+
declare function useMountEvent(event: EventObject): void;
|
|
34
35
|
|
|
35
36
|
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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
6
|
sendEvent(eventName: string, params?: EventParams): void;
|
|
7
7
|
sendEvent(event: EventObject): void;
|
|
@@ -26,10 +26,11 @@ type TrackRootProps = PropsWithChildren<{
|
|
|
26
26
|
declare const TrackRoot: (({ onEvent, children, filter, transform }: TrackRootProps) => react_jsx_runtime.JSX.Element) & {
|
|
27
27
|
factory: (onEvent: (eventName: string, params?: EventParams) => void, filter?: EventFilter, transform?: EventTransformer) => (props: Omit<TrackRootProps, "onEvent" | "filter" | "transform">) => react_jsx_runtime.JSX.Element;
|
|
28
28
|
};
|
|
29
|
-
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
30
|
-
params: EventParams
|
|
29
|
+
declare const TrackProvider: <T extends Record<string, any>>({ params, children }: PropsWithChildren<{
|
|
30
|
+
params: EventParams<T>;
|
|
31
31
|
}>) => react_jsx_runtime.JSX.Element;
|
|
32
32
|
|
|
33
33
|
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
34
|
+
declare function useMountEvent(event: EventObject): void;
|
|
34
35
|
|
|
35
36
|
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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
6
|
sendEvent(eventName: string, params?: EventParams): void;
|
|
7
7
|
sendEvent(event: EventObject): void;
|
|
@@ -26,10 +26,11 @@ type TrackRootProps = PropsWithChildren<{
|
|
|
26
26
|
declare const TrackRoot: (({ onEvent, children, filter, transform }: TrackRootProps) => react_jsx_runtime.JSX.Element) & {
|
|
27
27
|
factory: (onEvent: (eventName: string, params?: EventParams) => void, filter?: EventFilter, transform?: EventTransformer) => (props: Omit<TrackRootProps, "onEvent" | "filter" | "transform">) => react_jsx_runtime.JSX.Element;
|
|
28
28
|
};
|
|
29
|
-
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
30
|
-
params: EventParams
|
|
29
|
+
declare const TrackProvider: <T extends Record<string, any>>({ params, children }: PropsWithChildren<{
|
|
30
|
+
params: EventParams<T>;
|
|
31
31
|
}>) => react_jsx_runtime.JSX.Element;
|
|
32
32
|
|
|
33
33
|
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
34
|
+
declare function useMountEvent(event: EventObject): void;
|
|
34
35
|
|
|
35
36
|
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import React, { useContext, useCallback, useMemo, useRef, useEffect } from 'react';
|
|
2
2
|
|
|
3
|
+
function parseEventArgs(eventNameOrObject, eventParams) {
|
|
4
|
+
if (typeof eventNameOrObject === "object") {
|
|
5
|
+
return {
|
|
6
|
+
eventName: eventNameOrObject.eventName,
|
|
7
|
+
params: eventNameOrObject.params
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
eventName: eventNameOrObject,
|
|
12
|
+
params: eventParams
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
3
16
|
const TrackContext = React.createContext(null);
|
|
4
17
|
const useTracker = () => {
|
|
5
18
|
const ctx = useContext(TrackContext);
|
|
@@ -15,15 +28,10 @@ const TrackRootComponent = ({ onEvent, children, filter, transform }) => {
|
|
|
15
28
|
const filterRef = useFreshRef(filter);
|
|
16
29
|
const transformRef = useFreshRef(transform);
|
|
17
30
|
function sendEvent(eventNameOrObject, eventParams) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
incomingParams = eventNameOrObject.params;
|
|
23
|
-
} else {
|
|
24
|
-
eventName = eventNameOrObject;
|
|
25
|
-
incomingParams = eventParams;
|
|
26
|
-
}
|
|
31
|
+
const { eventName, params: incomingParams } = parseEventArgs(
|
|
32
|
+
eventNameOrObject,
|
|
33
|
+
eventParams
|
|
34
|
+
);
|
|
27
35
|
let localName = eventName;
|
|
28
36
|
let localParams = incomingParams || EmptyParams;
|
|
29
37
|
let shouldProcessLocal = true;
|
|
@@ -80,15 +88,10 @@ const TrackProvider = ({
|
|
|
80
88
|
const ctx = useTracker();
|
|
81
89
|
const paramsRef = useFreshRef(params);
|
|
82
90
|
function sendEvent(eventNameOrObject, eventParams) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
incomingParams = eventNameOrObject.params;
|
|
88
|
-
} else {
|
|
89
|
-
eventName = eventNameOrObject;
|
|
90
|
-
incomingParams = eventParams;
|
|
91
|
-
}
|
|
91
|
+
const { eventName, params: incomingParams } = parseEventArgs(
|
|
92
|
+
eventNameOrObject,
|
|
93
|
+
eventParams
|
|
94
|
+
);
|
|
92
95
|
const currentParams = paramsRef.current;
|
|
93
96
|
ctx.sendEvent(eventName, {
|
|
94
97
|
...currentParams,
|
|
@@ -105,9 +108,10 @@ function useFreshRef(data) {
|
|
|
105
108
|
return ref;
|
|
106
109
|
}
|
|
107
110
|
|
|
108
|
-
function useMountEvent(
|
|
111
|
+
function useMountEvent(eventNameOrObject, eventParams) {
|
|
109
112
|
const { sendEvent } = useTracker();
|
|
110
113
|
const counterRef = useRef(0);
|
|
114
|
+
const { eventName, params } = parseEventArgs(eventNameOrObject, eventParams);
|
|
111
115
|
useEffect(() => {
|
|
112
116
|
if (counterRef.current > 0) {
|
|
113
117
|
return;
|