react-event-tracking 1.0.1 → 1.0.3
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 +20 -19
- package/dist/index.cjs +25 -13
- package/dist/index.d.cts +14 -13
- package/dist/index.d.mts +14 -13
- package/dist/index.d.ts +14 -13
- package/dist/index.mjs +23 -11
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
# react-event-tracking [](https://www.npmjs.com/package/react-event-tracking)
|
|
2
|
+
A convenient React context for tracking analytics events.
|
|
2
3
|
|
|
3
4
|
## Features
|
|
4
5
|
|
|
@@ -9,7 +10,7 @@ A convenient React context for tracking analytics events.
|
|
|
9
10
|
## Installation
|
|
10
11
|
|
|
11
12
|
```
|
|
12
|
-
npm install
|
|
13
|
+
npm install react-event-tracking
|
|
13
14
|
```
|
|
14
15
|
```
|
|
15
16
|
yarn add react-event-tracking
|
|
@@ -19,32 +20,32 @@ yarn add react-event-tracking
|
|
|
19
20
|
|
|
20
21
|
1. Define the root handler (e.g., send to GTM or API)
|
|
21
22
|
```tsx
|
|
22
|
-
import {
|
|
23
|
+
import { TrackRoot } from 'react-event-tracking';
|
|
23
24
|
|
|
24
25
|
const Main = () => (
|
|
25
|
-
<
|
|
26
|
+
<TrackRoot onEvent={(name, params) => gtag('event', name, params)}>
|
|
26
27
|
<App/>
|
|
27
|
-
</
|
|
28
|
+
</TrackRoot>
|
|
28
29
|
);
|
|
29
30
|
```
|
|
30
31
|
2. Wrap any component with shared parameters
|
|
31
32
|
```tsx
|
|
32
|
-
import {
|
|
33
|
+
import { TrackProvider } from 'react-event-tracking';
|
|
33
34
|
|
|
34
35
|
const Dashboard = () => (
|
|
35
|
-
<
|
|
36
|
+
<TrackProvider params={{ screen: 'dashboard' }}>
|
|
36
37
|
<DashboardView/>
|
|
37
|
-
</
|
|
38
|
+
</TrackProvider>
|
|
38
39
|
);
|
|
39
40
|
```
|
|
40
41
|
|
|
41
42
|
3. Send events conveniently. On button click, parameters will be merged.
|
|
42
43
|
|
|
43
44
|
```tsx
|
|
44
|
-
import {
|
|
45
|
+
import { useTracker } from 'react-event-tracking';
|
|
45
46
|
|
|
46
47
|
const MyButton = () => {
|
|
47
|
-
const { sendEvent } =
|
|
48
|
+
const { sendEvent } = useTracker();
|
|
48
49
|
|
|
49
50
|
return (
|
|
50
51
|
// event sent with parameters: { screen: 'dashboard', button_id: '123' }
|
|
@@ -55,18 +56,18 @@ const MyButton = () => {
|
|
|
55
56
|
};
|
|
56
57
|
```
|
|
57
58
|
|
|
58
|
-
##
|
|
59
|
+
## Built-in Hooks
|
|
59
60
|
|
|
60
|
-
###
|
|
61
|
+
### useMountEvent
|
|
62
|
+
|
|
63
|
+
Sends an event when the component mounts.
|
|
61
64
|
|
|
62
65
|
```tsx
|
|
63
|
-
|
|
64
|
-
const { sendEvent } = useAnalytics();
|
|
66
|
+
import { useMountEvent } from 'react-event-tracking';
|
|
65
67
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}, []);
|
|
68
|
+
export function DashboardScreen(props) {
|
|
69
|
+
useMountEvent('page_view', { screen: 'dashboard' });
|
|
69
70
|
|
|
70
|
-
return
|
|
71
|
+
return <div>Dashboard</div>;
|
|
71
72
|
}
|
|
72
|
-
```
|
|
73
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -6,15 +6,15 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
|
|
|
6
6
|
|
|
7
7
|
const React__default = /*#__PURE__*/_interopDefaultCompat(React);
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const ctx = React.useContext(
|
|
9
|
+
const TrackContext = React__default.createContext(null);
|
|
10
|
+
const useTracker = () => {
|
|
11
|
+
const ctx = React.useContext(TrackContext);
|
|
12
12
|
if (!ctx) {
|
|
13
|
-
throw new Error("
|
|
13
|
+
throw new Error("useTracker must be used within TrackRoot");
|
|
14
14
|
}
|
|
15
15
|
return ctx;
|
|
16
16
|
};
|
|
17
|
-
const
|
|
17
|
+
const TrackRoot = ({
|
|
18
18
|
onEvent,
|
|
19
19
|
children
|
|
20
20
|
}) => {
|
|
@@ -24,13 +24,13 @@ const AnalyticsRoot = ({
|
|
|
24
24
|
onEventRef.current(eventName, params);
|
|
25
25
|
}, []);
|
|
26
26
|
const value = React.useMemo(() => ({ sendEvent }), [sendEvent]);
|
|
27
|
-
return /* @__PURE__ */ React__default.createElement(
|
|
27
|
+
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
|
|
28
28
|
};
|
|
29
|
-
const
|
|
29
|
+
const TrackProvider = ({
|
|
30
30
|
params,
|
|
31
31
|
children
|
|
32
32
|
}) => {
|
|
33
|
-
const ctx =
|
|
33
|
+
const ctx = useTracker();
|
|
34
34
|
const paramsRef = React.useRef(params);
|
|
35
35
|
paramsRef.current = params;
|
|
36
36
|
const sendEvent = React.useCallback(
|
|
@@ -44,10 +44,22 @@ const AnalyticsProvider = ({
|
|
|
44
44
|
[ctx]
|
|
45
45
|
);
|
|
46
46
|
const value = React.useMemo(() => ({ sendEvent }), [sendEvent]);
|
|
47
|
-
return /* @__PURE__ */ React__default.createElement(
|
|
47
|
+
return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
function useMountEvent(eventName, params) {
|
|
51
|
+
const { sendEvent } = useTracker();
|
|
52
|
+
const counterRef = React.useRef(0);
|
|
53
|
+
React.useEffect(() => {
|
|
54
|
+
if (counterRef.current > 0) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
counterRef.current++;
|
|
58
|
+
sendEvent(eventName, params);
|
|
59
|
+
}, []);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
exports.TrackProvider = TrackProvider;
|
|
63
|
+
exports.TrackRoot = TrackRoot;
|
|
64
|
+
exports.useMountEvent = useMountEvent;
|
|
65
|
+
exports.useTracker = useTracker;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import
|
|
2
|
+
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
sendEvent: (eventName: string, params?:
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
declare const
|
|
10
|
-
declare const
|
|
11
|
-
onEvent: (eventName: string, params?:
|
|
4
|
+
type EventParams = Record<string, any>;
|
|
5
|
+
type TrackContextValue = {
|
|
6
|
+
sendEvent: (eventName: string, params?: EventParams) => void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
declare const useTracker: () => TrackContextValue;
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
11
|
+
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
12
|
}>) => react_jsx_runtime.JSX.Element;
|
|
13
|
-
declare const
|
|
14
|
-
params:
|
|
13
|
+
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
14
|
+
params: EventParams;
|
|
15
15
|
}>) => react_jsx_runtime.JSX.Element;
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
18
|
+
|
|
19
|
+
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import
|
|
2
|
+
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
sendEvent: (eventName: string, params?:
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
declare const
|
|
10
|
-
declare const
|
|
11
|
-
onEvent: (eventName: string, params?:
|
|
4
|
+
type EventParams = Record<string, any>;
|
|
5
|
+
type TrackContextValue = {
|
|
6
|
+
sendEvent: (eventName: string, params?: EventParams) => void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
declare const useTracker: () => TrackContextValue;
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
11
|
+
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
12
|
}>) => react_jsx_runtime.JSX.Element;
|
|
13
|
-
declare const
|
|
14
|
-
params:
|
|
13
|
+
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
14
|
+
params: EventParams;
|
|
15
15
|
}>) => react_jsx_runtime.JSX.Element;
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
18
|
+
|
|
19
|
+
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import
|
|
2
|
+
import { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
sendEvent: (eventName: string, params?:
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
declare const
|
|
10
|
-
declare const
|
|
11
|
-
onEvent: (eventName: string, params?:
|
|
4
|
+
type EventParams = Record<string, any>;
|
|
5
|
+
type TrackContextValue = {
|
|
6
|
+
sendEvent: (eventName: string, params?: EventParams) => void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
declare const useTracker: () => TrackContextValue;
|
|
10
|
+
declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
|
|
11
|
+
onEvent: (eventName: string, params?: EventParams) => void;
|
|
12
12
|
}>) => react_jsx_runtime.JSX.Element;
|
|
13
|
-
declare const
|
|
14
|
-
params:
|
|
13
|
+
declare const TrackProvider: ({ params, children }: PropsWithChildren<{
|
|
14
|
+
params: EventParams;
|
|
15
15
|
}>) => react_jsx_runtime.JSX.Element;
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
declare function useMountEvent(eventName: string, params?: EventParams): void;
|
|
18
|
+
|
|
19
|
+
export { TrackProvider, TrackRoot, useMountEvent, useTracker };
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import React, { useContext, useRef, useCallback, useMemo } from 'react';
|
|
1
|
+
import React, { useContext, useRef, useCallback, useMemo, useEffect } from 'react';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const ctx = useContext(
|
|
3
|
+
const TrackContext = React.createContext(null);
|
|
4
|
+
const useTracker = () => {
|
|
5
|
+
const ctx = useContext(TrackContext);
|
|
6
6
|
if (!ctx) {
|
|
7
|
-
throw new Error("
|
|
7
|
+
throw new Error("useTracker must be used within TrackRoot");
|
|
8
8
|
}
|
|
9
9
|
return ctx;
|
|
10
10
|
};
|
|
11
|
-
const
|
|
11
|
+
const TrackRoot = ({
|
|
12
12
|
onEvent,
|
|
13
13
|
children
|
|
14
14
|
}) => {
|
|
@@ -18,13 +18,13 @@ const AnalyticsRoot = ({
|
|
|
18
18
|
onEventRef.current(eventName, params);
|
|
19
19
|
}, []);
|
|
20
20
|
const value = useMemo(() => ({ sendEvent }), [sendEvent]);
|
|
21
|
-
return /* @__PURE__ */ React.createElement(
|
|
21
|
+
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
|
|
22
22
|
};
|
|
23
|
-
const
|
|
23
|
+
const TrackProvider = ({
|
|
24
24
|
params,
|
|
25
25
|
children
|
|
26
26
|
}) => {
|
|
27
|
-
const ctx =
|
|
27
|
+
const ctx = useTracker();
|
|
28
28
|
const paramsRef = useRef(params);
|
|
29
29
|
paramsRef.current = params;
|
|
30
30
|
const sendEvent = useCallback(
|
|
@@ -38,7 +38,19 @@ const AnalyticsProvider = ({
|
|
|
38
38
|
[ctx]
|
|
39
39
|
);
|
|
40
40
|
const value = useMemo(() => ({ sendEvent }), [sendEvent]);
|
|
41
|
-
return /* @__PURE__ */ React.createElement(
|
|
41
|
+
return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
function useMountEvent(eventName, params) {
|
|
45
|
+
const { sendEvent } = useTracker();
|
|
46
|
+
const counterRef = useRef(0);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (counterRef.current > 0) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
counterRef.current++;
|
|
52
|
+
sendEvent(eventName, params);
|
|
53
|
+
}, []);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
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.3",
|
|
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",
|