@mcp-fe/react-event-tracker 0.0.15 → 0.0.16

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.
Files changed (2) hide show
  1. package/README.md +297 -4
  2. package/package.json +8 -2
package/README.md CHANGED
@@ -1,7 +1,300 @@
1
- # react-event-tracker
1
+ # @mcp-fe/react-event-tracker
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ React integration for the MCP-FE (Model Context Protocol - Frontend Edge) ecosystem. This library provides React hooks that automatically track user interactions and navigation events, making them available to AI agents through the MCP protocol.
4
4
 
5
- ## Running unit tests
5
+ ## Overview
6
+
7
+ `@mcp-fe/react-event-tracker` is a React-specific integration layer that simplifies the process of tracking user interactions in React applications. It works in conjunction with [`@mcp-fe/mcp-worker`](../mcp-worker/README.md) to provide a complete MCP-FE solution for React apps.
8
+
9
+ ### Key Features
10
+
11
+ - **Automatic Navigation Tracking**: Tracks route changes in React Router and TanStack Router applications
12
+ - **User Interaction Tracking**: Automatically tracks clicks, input changes, and other user interactions
13
+ - **Zero Configuration**: Works out-of-the-box with minimal setup
14
+ - **Authentication Support**: Built-in support for setting authentication tokens
15
+ - **Framework Agnostic Core**: Built on top of [`@mcp-fe/event-tracker`](../event-tracker/README.md)
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install @mcp-fe/react-event-tracker @mcp-fe/mcp-worker
21
+ # or
22
+ pnpm add @mcp-fe/react-event-tracker @mcp-fe/mcp-worker
23
+ # or
24
+ yarn add @mcp-fe/react-event-tracker @mcp-fe/mcp-worker
25
+ ```
26
+
27
+ ### Peer Dependencies
28
+
29
+ This package requires the following peer dependencies:
30
+
31
+ - React >=19.0.0
32
+ - React DOM >=19.0.0
33
+ - React Router DOM >=6.0.0 (for React Router integration)
34
+ - TanStack Router >=1.0.0 (for TanStack Router integration)
35
+
36
+ ## Setup
37
+
38
+ Before using the React hooks, you need to:
39
+
40
+ 1. **Copy worker files** to your public directory from `@mcp-fe/mcp-worker`
41
+ 2. **Set up the MCP proxy server** (see [`@mcp-fe/mcp-worker` documentation](../mcp-worker/README.md))
42
+
43
+ ## Usage
44
+
45
+ ### React Router Integration
46
+
47
+ For applications using React Router, use the `useReactRouterEventTracker` hook:
48
+
49
+ ```typescript
50
+ import React, { useEffect } from 'react';
51
+ import { BrowserRouter, Routes, Route } from 'react-router-dom';
52
+ import { useReactRouterEventTracker } from '@mcp-fe/react-event-tracker';
53
+
54
+ function App() {
55
+ const { setAuthToken } = useReactRouterEventTracker({
56
+ backendWsUrl: 'ws://localhost:3001'
57
+ });
58
+
59
+ // Set authentication token when available
60
+ useEffect(() => {
61
+ const token = localStorage.getItem('authToken');
62
+ if (token) {
63
+ setAuthToken(token);
64
+ }
65
+ }, [setAuthToken]);
66
+
67
+ return (
68
+ <BrowserRouter>
69
+ <Routes>
70
+ <Route path="/" element={<HomePage />} />
71
+ <Route path="/about" element={<AboutPage />} />
72
+ {/* Your other routes */}
73
+ </Routes>
74
+ </BrowserRouter>
75
+ );
76
+ }
77
+
78
+ function HomePage() {
79
+ return (
80
+ <div>
81
+ <h1>Welcome!</h1>
82
+ <button onClick={() => alert('Clicked!')}>
83
+ Click me (this click will be tracked)
84
+ </button>
85
+ <input
86
+ placeholder="Type here (input changes will be tracked)"
87
+ onChange={(e) => console.log(e.target.value)}
88
+ />
89
+ </div>
90
+ );
91
+ }
92
+ ```
93
+
94
+ ### TanStack Router Integration
95
+
96
+ For applications using TanStack Router, use the `useTanstackRouterEventTracker` hook:
97
+
98
+ ```typescript
99
+ import React, { useEffect } from 'react';
100
+ import { useTanstackRouterEventTracker } from '@mcp-fe/react-event-tracker';
101
+
102
+ function App() {
103
+ const { setAuthToken } = useTanstackRouterEventTracker({
104
+ backendWsUrl: 'ws://localhost:3001'
105
+ });
106
+
107
+ useEffect(() => {
108
+ const token = localStorage.getItem('authToken');
109
+ if (token) {
110
+ setAuthToken(token);
111
+ }
112
+ }, [setAuthToken]);
113
+
114
+ return (
115
+ <div>
116
+ {/* Your TanStack Router setup */}
117
+ <h1>My TanStack Router App</h1>
118
+ {/* Components and interactions will be automatically tracked */}
119
+ </div>
120
+ );
121
+ }
122
+ ```
123
+
124
+ ## Configuration Options
125
+
126
+ Both hooks accept optional configuration parameters:
127
+
128
+ ```typescript
129
+ interface WorkerClientInitOptions {
130
+ backendWsUrl?: string; // WebSocket URL of your MCP proxy server
131
+ workerPath?: string; // Custom path to worker files
132
+ fallbackToServiceWorker?: boolean; // Enable ServiceWorker fallback
133
+ }
134
+ ```
135
+
136
+ ### Example with custom configuration:
137
+
138
+ ```typescript
139
+ const { setAuthToken } = useReactRouterEventTracker({
140
+ backendWsUrl: 'wss://my-mcp-server.com/ws',
141
+ workerPath: '/workers',
142
+ fallbackToServiceWorker: true
143
+ });
144
+ ```
145
+
146
+ ## What Gets Tracked
147
+
148
+ The React event tracker automatically captures:
149
+
150
+ ### Navigation Events
151
+ - Route changes (from previous route to current route)
152
+ - Page loads and refreshes
153
+
154
+ ### User Interactions
155
+ - **Clicks**: Element tag, ID, class, text content (first 100 chars)
156
+ - **Input Changes**: Input/textarea changes (debounced by 1 second)
157
+ - **Form Interactions**: Button clicks, link clicks
158
+
159
+ ### Event Data Structure
160
+
161
+ Each tracked event includes:
162
+
163
+ ```typescript
164
+ interface UserEventData {
165
+ type: 'navigation' | 'click' | 'input' | 'custom';
166
+ path?: string; // Current page path
167
+ from?: string; // Previous route (navigation only)
168
+ to?: string; // Current route (navigation only)
169
+ element?: string; // Element tag name
170
+ elementId?: string; // Element ID attribute
171
+ elementClass?: string; // Element class attribute
172
+ elementText?: string; // Element text content
173
+ metadata?: Record<string, unknown>; // Additional data
174
+ timestamp: number; // Event timestamp
175
+ }
176
+ ```
177
+
178
+ ## Authentication
179
+
180
+ To associate tracked events with specific users, set an authentication token:
181
+
182
+ ```typescript
183
+ const { setAuthToken } = useReactRouterEventTracker();
184
+
185
+ // When user logs in
186
+ const handleLogin = async (credentials) => {
187
+ const response = await login(credentials);
188
+ const token = response.token;
189
+
190
+ // Store token for your app
191
+ localStorage.setItem('authToken', token);
192
+
193
+ // Set token for MCP tracking
194
+ setAuthToken(token);
195
+ };
196
+
197
+ // When user logs out
198
+ const handleLogout = () => {
199
+ localStorage.removeItem('authToken');
200
+ setAuthToken(''); // Clear the token
201
+ };
202
+ ```
203
+
204
+ ## Integration with MCP Agents
205
+
206
+ Once events are being tracked, AI agents can query them through the MCP protocol:
207
+
208
+ ```json
209
+ {
210
+ "jsonrpc": "2.0",
211
+ "method": "tools/call",
212
+ "params": {
213
+ "name": "get_user_events",
214
+ "arguments": {
215
+ "type": "click",
216
+ "limit": 10
217
+ }
218
+ }
219
+ }
220
+ ```
221
+
222
+ This enables AI agents to:
223
+ - Debug user interface issues
224
+ - Provide context-aware customer support
225
+ - Analyze user behavior patterns
226
+ - Guide users through complex workflows
227
+
228
+ ## Advanced Usage
229
+
230
+ ### Custom Event Tracking
231
+
232
+ You can also track custom events alongside the automatic tracking:
233
+
234
+ ```typescript
235
+ import { trackEvent } from '@mcp-fe/event-tracker';
236
+
237
+ // Track a custom business event
238
+ await trackEvent({
239
+ type: 'custom',
240
+ metadata: {
241
+ eventName: 'purchase_completed',
242
+ orderId: '12345',
243
+ amount: 99.99
244
+ }
245
+ });
246
+ ```
247
+
248
+ ### Connection Status Monitoring
249
+
250
+ Monitor the connection status to the MCP proxy server:
251
+
252
+ ```typescript
253
+ import { getConnectionStatus, onConnectionStatus } from '@mcp-fe/event-tracker';
254
+
255
+ // Check current status
256
+ const isConnected = await getConnectionStatus();
257
+
258
+ // Listen for status changes
259
+ onConnectionStatus((connected) => {
260
+ console.log('MCP connection:', connected ? 'Connected' : 'Disconnected');
261
+ });
262
+ ```
263
+
264
+ ## Requirements
265
+
266
+ - **MCP Worker**: This package requires [`@mcp-fe/mcp-worker`](../mcp-worker/README.md) to be properly set up
267
+ - **MCP Proxy Server**: You need a running MCP proxy server to collect the events
268
+ - **Public Worker Files**: Worker files must be accessible from your web server's public directory
269
+
270
+ ## Troubleshooting
271
+
272
+ ### Events Not Being Tracked
273
+
274
+ 1. **Check worker initialization**: Ensure `@mcp-fe/mcp-worker` files are in your public directory
275
+ 2. **Verify proxy connection**: Check that your MCP proxy server is running and accessible
276
+ 3. **Check browser console**: Look for initialization errors or connection issues
277
+
278
+ ### Navigation Not Tracked
279
+
280
+ 1. **Router integration**: Ensure you're using the correct hook for your router (React Router vs TanStack Router)
281
+ 2. **Hook placement**: Make sure the hook is called within the router context
282
+
283
+ ### Authentication Issues
284
+
285
+ 1. **Token format**: Ensure your authentication token is in the expected format
286
+ 2. **Timing**: Set the auth token after both the tracker and your authentication system are initialized
287
+
288
+ ## Examples
289
+
290
+ Check out the example application in the `apps/mcp-fe` directory for a complete working implementation.
291
+
292
+ ## Related Packages
293
+
294
+ - [`@mcp-fe/mcp-worker`](../mcp-worker/README.md): Core MCP worker implementation
295
+ - [`@mcp-fe/event-tracker`](../event-tracker/README.md): Framework-agnostic event tracking library
296
+
297
+ ## License
298
+
299
+ Licensed under the Apache License, Version 2.0. See the [LICENSE](../../LICENSE) file for details.
6
300
 
7
- Run `nx test react-event-tracker` to execute the unit tests via [Vitest](https://vitest.dev/).
package/package.json CHANGED
@@ -1,7 +1,13 @@
1
1
  {
2
2
  "name": "@mcp-fe/react-event-tracker",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "license": "Apache-2.0",
5
+ "homepage": "https://mcp-fe.ai",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/mcp-fe/mcp-fe.git",
9
+ "directory": "libs/react-event-tracker"
10
+ },
5
11
  "main": "./index.js",
6
12
  "types": "./index.d.ts",
7
13
  "exports": {
@@ -12,7 +18,7 @@
12
18
  }
13
19
  },
14
20
  "dependencies": {
15
- "@mcp-fe/event-tracker": "0.0.15"
21
+ "@mcp-fe/event-tracker": "0.0.16"
16
22
  },
17
23
  "peerDependencies": {
18
24
  "react": ">=19.0.0",