react-event-tracking 1.0.0

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 ADDED
@@ -0,0 +1,72 @@
1
+ A convenient React context for tracking analytics events.
2
+
3
+ ## Features
4
+
5
+ - **Nested Contexts**: Automatically merges parameters from parent providers.
6
+ - **Zero Re-renders**: No need to wrap props in `useCallback`/`useMemo`.
7
+
8
+
9
+ ## Installation
10
+
11
+ ```
12
+ npm install treact-event-tracking
13
+ ```
14
+ ```
15
+ yarn add react-event-tracking
16
+ ```
17
+
18
+ ## Quickstart
19
+
20
+ 1. Define the root handler (e.g., send to GTM or API)
21
+ ```tsx
22
+ import { AnalyticsRoot } from 'react-event-tracking';
23
+
24
+ const Main = () => (
25
+ <AnalyticsRoot onEvent={(name, params) => gtag('event', name, params)}>
26
+ <App/>
27
+ </AnalyticsRoot>
28
+ );
29
+ ```
30
+ 2. Wrap any component with shared parameters
31
+ ```tsx
32
+ import { AnalyticsProvider } from 'react-event-tracking';
33
+
34
+ const Dashboard = () => (
35
+ <AnalyticsProvider params={{ screen: 'dashboard' }}>
36
+ <DashboardView/>
37
+ </AnalyticsProvider>
38
+ );
39
+ ```
40
+
41
+ 3. Send events conveniently. On button click, parameters will be merged.
42
+
43
+ ```tsx
44
+ import { useAnalytics } from 'react-event-tracking';
45
+
46
+ const MyButton = () => {
47
+ const { sendEvent } = useAnalytics();
48
+
49
+ return (
50
+ // event sent with parameters: { screen: 'dashboard', button_id: '123' }
51
+ <button onClick={() => sendEvent('click', { button_id: '123' })}>
52
+ Click me
53
+ </button>
54
+ );
55
+ };
56
+ ```
57
+
58
+ ## Example
59
+
60
+ ### Page View
61
+
62
+ ```tsx
63
+ export function PageView(props) {
64
+ const { sendEvent } = useAnalytics();
65
+
66
+ useEffect(() => {
67
+ sendEvent('page_view');
68
+ }, []);
69
+
70
+ return <>{props.children}</>
71
+ }
72
+ ```
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "react-event-tracking",
3
+ "version": "1.0.0",
4
+ "exports": {
5
+ ".": {
6
+ "types": "./dist/index.d.ts",
7
+ "import": "./dist/index.mjs",
8
+ "require": "./dist/index.cjs"
9
+ }
10
+ },
11
+ "scripts": {
12
+ "build": "tsc --noEmit && unbuild",
13
+ "test": "vitest"
14
+ },
15
+ "author": "",
16
+ "license": "ISC",
17
+ "description": "",
18
+ "devDependencies": {
19
+ "@testing-library/dom": "^10.4.1",
20
+ "@testing-library/react": "^16.3.2",
21
+ "@testing-library/user-event": "^14.6.1",
22
+ "@types/react": "^18.0.0",
23
+ "@types/react-dom": "^18.0.0",
24
+ "jsdom": "^28.1.0",
25
+ "react": "^18.0.0",
26
+ "react-dom": "^18.0.0",
27
+ "typescript": "^5.9.3",
28
+ "unbuild": "^3.6.1",
29
+ "vitest": "^4.0.18"
30
+ },
31
+ "peerDependencies": {
32
+ "react": ">=18.0.0",
33
+ "react-dom": ">=18.0.0"
34
+ }
35
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,70 @@
1
+ import React, {
2
+ useCallback,
3
+ useContext,
4
+ useMemo,
5
+ useRef,
6
+ type PropsWithChildren
7
+ } from "react"
8
+
9
+ export type AnalyticsParams = Record<string, any>
10
+
11
+ interface AnalyticsContextValue {
12
+ sendEvent: (eventName: string, params?: AnalyticsParams) => void
13
+ }
14
+
15
+ export const AnalyticsContext = React.createContext<AnalyticsContextValue | null>(null)
16
+
17
+ export const useAnalytics = () => {
18
+ const ctx = useContext(AnalyticsContext)
19
+ if (!ctx) {
20
+ throw new Error("useAnalytics must be used within AnalyticsRoot")
21
+ }
22
+
23
+ return ctx
24
+ }
25
+
26
+ export const AnalyticsRoot = ({
27
+ onEvent,
28
+ children
29
+ }: PropsWithChildren<{
30
+ onEvent: (eventName: string, params?: AnalyticsParams) => void
31
+ }>) => {
32
+ const onEventRef = useRef(onEvent)
33
+ onEventRef.current = onEvent
34
+
35
+ const sendEvent = useCallback((eventName: string, params?: AnalyticsParams) => {
36
+ onEventRef.current(eventName, params)
37
+ }, [])
38
+
39
+ const value = useMemo(() => ({ sendEvent }), [sendEvent])
40
+
41
+ return <AnalyticsContext.Provider value={value}>{children}</AnalyticsContext.Provider>
42
+ }
43
+
44
+ export const AnalyticsProvider = ({
45
+ params,
46
+ children
47
+ }: PropsWithChildren<{
48
+ params: AnalyticsParams
49
+ }>) => {
50
+ const ctx = useAnalytics()
51
+
52
+ const paramsRef = useRef(params)
53
+ paramsRef.current = params
54
+
55
+ const sendEvent = useCallback(
56
+ (eventName: string, eventParams?: AnalyticsParams) => {
57
+ const currentParams = paramsRef.current
58
+
59
+ ctx.sendEvent(eventName, {
60
+ ...currentParams,
61
+ ...eventParams
62
+ })
63
+ },
64
+ [ctx]
65
+ )
66
+
67
+ const value = useMemo(() => ({ sendEvent }), [sendEvent])
68
+
69
+ return <AnalyticsContext.Provider value={value}>{children}</AnalyticsContext.Provider>
70
+ }
@@ -0,0 +1,162 @@
1
+ import { describe, it, expect, vi } from "vitest"
2
+ import React from "react"
3
+ import { render, screen } from "@testing-library/react"
4
+ import userEvent from "@testing-library/user-event"
5
+ import { AnalyticsRoot, AnalyticsProvider, useAnalytics } from "../src"
6
+
7
+ const TestButton = ({
8
+ eventName,
9
+ params,
10
+ label = "Click me"
11
+ }: {
12
+ eventName: string
13
+ params?: Record<string, any>
14
+ label?: string
15
+ }) => {
16
+ const { sendEvent } = useAnalytics()
17
+ return <button onClick={() => sendEvent(eventName, params)}>{label}</button>
18
+ }
19
+
20
+ describe("Analytics Context", () => {
21
+ it("should send event from root", async () => {
22
+ const onEvent = vi.fn()
23
+
24
+ render(
25
+ <AnalyticsRoot onEvent={onEvent}>
26
+ <TestButton
27
+ eventName="test_click"
28
+ params={{ foo: "bar" }}
29
+ label="Root Click"
30
+ />
31
+ </AnalyticsRoot>
32
+ )
33
+
34
+ await userEvent.click(screen.getByText("Root Click"))
35
+
36
+ expect(onEvent).toHaveBeenCalledTimes(1)
37
+ expect(onEvent).toHaveBeenCalledWith("test_click", { foo: "bar" })
38
+ })
39
+
40
+ it("should merge params from nested providers", async () => {
41
+ const onEvent = vi.fn()
42
+
43
+ render(
44
+ <AnalyticsRoot onEvent={onEvent}>
45
+ <AnalyticsProvider params={{ section: "header" }}>
46
+ <AnalyticsProvider params={{ item: "logo" }}>
47
+ <TestButton
48
+ eventName="logo_click"
49
+ params={{ action: "click" }}
50
+ label="Nested Click"
51
+ />
52
+ </AnalyticsProvider>
53
+ </AnalyticsProvider>
54
+ </AnalyticsRoot>
55
+ )
56
+
57
+ await userEvent.click(screen.getByText("Nested Click"))
58
+
59
+ expect(onEvent).toHaveBeenCalledWith("logo_click", {
60
+ section: "header",
61
+ item: "logo",
62
+ action: "click"
63
+ })
64
+ })
65
+
66
+ it("should override params from child to parent", async () => {
67
+ const onEvent = vi.fn()
68
+
69
+ render(
70
+ <AnalyticsRoot onEvent={onEvent}>
71
+ <AnalyticsProvider params={{ page: "home", id: 1 }}>
72
+ {/* Переопределяем id */}
73
+ <AnalyticsProvider params={{ id: 2 }}>
74
+ <TestButton
75
+ eventName="click"
76
+ params={{ id: 3 }}
77
+ label="Override Click"
78
+ />
79
+ </AnalyticsProvider>
80
+ </AnalyticsProvider>
81
+ </AnalyticsRoot>
82
+ )
83
+
84
+ await userEvent.click(screen.getByText("Override Click"))
85
+
86
+ expect(onEvent).toHaveBeenCalledWith("click", {
87
+ page: "home",
88
+ id: 3
89
+ })
90
+ })
91
+
92
+ it("should use latest params without re-rendering children", async () => {
93
+ const onEvent = vi.fn()
94
+
95
+ const Wrapper = ({ count }: { count: number }) => (
96
+ <AnalyticsProvider params={{ count }}>
97
+ <TestButton eventName="count_click" label="Rerender Click" />
98
+ </AnalyticsProvider>
99
+ )
100
+
101
+ const { rerender } = render(
102
+ <AnalyticsRoot onEvent={onEvent}>
103
+ <Wrapper count={1} />
104
+ </AnalyticsRoot>
105
+ )
106
+
107
+ await userEvent.click(screen.getByText("Rerender Click"))
108
+ expect(onEvent).toHaveBeenLastCalledWith("count_click", { count: 1 })
109
+
110
+ rerender(
111
+ <AnalyticsRoot onEvent={onEvent}>
112
+ <Wrapper count={2} />
113
+ </AnalyticsRoot>
114
+ )
115
+
116
+ await userEvent.click(screen.getByText("Rerender Click"))
117
+ expect(onEvent).toHaveBeenLastCalledWith("count_click", { count: 2 })
118
+ })
119
+
120
+ it("should NOT trigger re-render in consumers when params update (optimization check)", () => {
121
+ const renderFn = vi.fn()
122
+
123
+ const MemoChild = React.memo(() => {
124
+ useAnalytics()
125
+ renderFn()
126
+ return <div>Memo Child</div>
127
+ })
128
+
129
+ // eslint-disable-next-line react/display-name
130
+ MemoChild.displayName = "MemoChild"
131
+
132
+ const { rerender } = render(
133
+ <AnalyticsRoot onEvent={() => {}}>
134
+ <AnalyticsProvider params={{ val: 1 }}>
135
+ <MemoChild />
136
+ </AnalyticsProvider>
137
+ </AnalyticsRoot>
138
+ )
139
+
140
+ expect(renderFn).toHaveBeenCalledTimes(1)
141
+
142
+ rerender(
143
+ <AnalyticsRoot onEvent={() => {}}>
144
+ <AnalyticsProvider params={{ val: 2 }}>
145
+ <MemoChild />
146
+ </AnalyticsProvider>
147
+ </AnalyticsRoot>
148
+ )
149
+
150
+ expect(renderFn).toHaveBeenCalledTimes(1)
151
+ })
152
+
153
+ it("should throw error if used outside of AnalyticsRoot", () => {
154
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {})
155
+
156
+ expect(() => {
157
+ render(<TestButton eventName="fail" label="Fail Click" />)
158
+ }).toThrow("useAnalytics must be used within AnalyticsRoot")
159
+
160
+ consoleSpy.mockRestore()
161
+ })
162
+ })
package/tests/setup.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { afterEach } from 'vitest'
2
+ import { cleanup } from '@testing-library/react'
3
+
4
+ afterEach(() => {
5
+ cleanup()
6
+ })
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "jsx": "react-jsx",
6
+ "declaration": true,
7
+ "declarationMap": true,
8
+ "outDir": "./dist",
9
+ "rootDir": "./src",
10
+ "strict": true,
11
+ "moduleResolution": "node",
12
+ "esModuleInterop": true,
13
+ "skipLibCheck": true
14
+ },
15
+ "include": ["src/**/*"],
16
+ "exclude": ["node_modules", "dist"]
17
+ }
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from 'vitest/config'
2
+
3
+
4
+ export default defineConfig({
5
+ test: {
6
+ globals: true,
7
+ environment: 'jsdom',
8
+ setupFiles: './tests/setup.ts',
9
+ },
10
+ })