@temps-sdk/react-analytics 0.0.1-beta.8 → 0.0.2
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 +534 -0
- package/dist/EngagementTracker.d.ts +55 -0
- package/dist/EngagementTracker.d.ts.map +1 -0
- package/dist/EngagementTracker.js +223 -0
- package/dist/Provider.d.ts +1 -1
- package/dist/Provider.d.ts.map +1 -1
- package/dist/Provider.js +69 -6
- package/dist/SessionRecorder.d.ts +57 -0
- package/dist/SessionRecorder.d.ts.map +1 -0
- package/dist/SessionRecorder.js +536 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/types.d.ts +56 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/useEngagementTracking.d.ts +34 -0
- package/dist/useEngagementTracking.d.ts.map +1 -0
- package/dist/useEngagementTracking.js +65 -0
- package/dist/usePageLeave.d.ts +13 -0
- package/dist/usePageLeave.d.ts.map +1 -0
- package/dist/usePageLeave.js +66 -0
- package/dist/useScrollVisibility.d.ts +58 -0
- package/dist/useScrollVisibility.d.ts.map +1 -0
- package/dist/useScrollVisibility.js +65 -0
- package/dist/useSessionRecording.d.ts +23 -0
- package/dist/useSessionRecording.d.ts.map +1 -0
- package/dist/useSessionRecording.js +99 -0
- package/dist/useSpeedAnalytics.d.ts +8 -0
- package/dist/useSpeedAnalytics.d.ts.map +1 -0
- package/dist/useSpeedAnalytics.js +60 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +35 -1
- package/package.json +27 -5
package/README.md
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/AnomalyCo/temps/main/temps-demo.gif" alt="Temps Platform" width="700" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">@temps-sdk/react-analytics</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/@temps-sdk/react-analytics"><img src="https://img.shields.io/npm/v/@temps-sdk/react-analytics.svg" alt="npm version" /></a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/@temps-sdk/react-analytics"><img src="https://img.shields.io/npm/dm/@temps-sdk/react-analytics.svg" alt="npm downloads" /></a>
|
|
10
|
+
<a href="https://github.com/AnomalyCo/temps/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@temps-sdk/react-analytics.svg" alt="license" /></a>
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center">
|
|
14
|
+
Privacy-friendly analytics, Web Vitals, session replay, and engagement tracking for React. One provider, zero config, full insight into how users experience your product.
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# npm
|
|
21
|
+
npm install @temps-sdk/react-analytics
|
|
22
|
+
|
|
23
|
+
# bun
|
|
24
|
+
bun add @temps-sdk/react-analytics
|
|
25
|
+
|
|
26
|
+
# pnpm
|
|
27
|
+
pnpm add @temps-sdk/react-analytics
|
|
28
|
+
|
|
29
|
+
# yarn
|
|
30
|
+
yarn add @temps-sdk/react-analytics
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**Peer dependencies:** `react >= 18`
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
Wrap your app with the provider -- analytics start working immediately:
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { TempsAnalyticsProvider } from '@temps-sdk/react-analytics';
|
|
41
|
+
|
|
42
|
+
export default function App({ children }) {
|
|
43
|
+
return (
|
|
44
|
+
<TempsAnalyticsProvider>
|
|
45
|
+
{children}
|
|
46
|
+
</TempsAnalyticsProvider>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**What you get out of the box:**
|
|
52
|
+
|
|
53
|
+
| Feature | Description |
|
|
54
|
+
|---------|-------------|
|
|
55
|
+
| Pageview tracking | Automatic on `pushState` / `popstate` route changes |
|
|
56
|
+
| Page leave events | Time-on-page captured via `pagehide` + `beforeunload` |
|
|
57
|
+
| Web Vitals | LCP, FID, CLS, TTFB, FCP, INP -- sent automatically |
|
|
58
|
+
| Engagement tracking | Heartbeat-based active time, visibility, and inactivity detection |
|
|
59
|
+
|
|
60
|
+
## Provider Configuration
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
<TempsAnalyticsProvider
|
|
64
|
+
// Core
|
|
65
|
+
basePath="/api/_temps" // API endpoint (default)
|
|
66
|
+
domain="example.com" // Override detected hostname
|
|
67
|
+
disabled={false} // Kill switch for analytics
|
|
68
|
+
|
|
69
|
+
// Environment
|
|
70
|
+
ignoreLocalhost={true} // Skip localhost and test environments (default)
|
|
71
|
+
|
|
72
|
+
// Pageviews
|
|
73
|
+
autoTrackPageviews={true} // Track on pushState/popstate (default)
|
|
74
|
+
autoTrackPageLeave={true} // Track page_leave events (default)
|
|
75
|
+
pageLeaveEventName="page_leave" // Custom event name for page leave
|
|
76
|
+
|
|
77
|
+
// Performance
|
|
78
|
+
autoTrackSpeedAnalytics={true} // Web Vitals: LCP, FID, CLS, TTFB, FCP, INP (default)
|
|
79
|
+
|
|
80
|
+
// Engagement
|
|
81
|
+
autoTrackEngagement={true} // Heartbeat-based engagement metrics (default)
|
|
82
|
+
heartbeatInterval={30000} // Heartbeat every 30s (default)
|
|
83
|
+
inactivityTimeout={30000} // Mark inactive after 30s of no interaction (default)
|
|
84
|
+
engagementThreshold={10000} // Consider engaged after 10s (default)
|
|
85
|
+
|
|
86
|
+
// Session Recording
|
|
87
|
+
enableSessionRecording={false} // Off by default
|
|
88
|
+
sessionRecordingConfig={{
|
|
89
|
+
excludedPaths: ['/settings/*', '/admin/*'],
|
|
90
|
+
sessionSampleRate: 1.0, // Record 100% of sessions (default)
|
|
91
|
+
maskAllInputs: true, // Mask all input fields (default)
|
|
92
|
+
maskTextSelector: '[data-mask]', // CSS selector for text masking
|
|
93
|
+
blockClass: 'rr-block', // CSS class to block from recording
|
|
94
|
+
ignoreClass: 'rr-ignore', // CSS class to ignore
|
|
95
|
+
maskTextClass: 'rr-mask', // CSS class to mask text
|
|
96
|
+
recordCanvas: false, // Record canvas elements (default: false)
|
|
97
|
+
collectFonts: false, // Collect font data (default: false)
|
|
98
|
+
batchSize: 100, // Events per batch (default)
|
|
99
|
+
flushInterval: 5000, // Flush interval in ms (default)
|
|
100
|
+
}}
|
|
101
|
+
>
|
|
102
|
+
{children}
|
|
103
|
+
</TempsAnalyticsProvider>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Custom Events
|
|
107
|
+
|
|
108
|
+
### Using the Hook
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
import { useTempsAnalytics } from '@temps-sdk/react-analytics';
|
|
112
|
+
|
|
113
|
+
function PricingPage() {
|
|
114
|
+
const { trackEvent } = useTempsAnalytics();
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<button onClick={() => trackEvent('plan_selected', { plan: 'pro', billing: 'annual' })}>
|
|
118
|
+
Choose Pro
|
|
119
|
+
</button>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Using the Shorthand Hook
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
import { useTrackEvent } from '@temps-sdk/react-analytics';
|
|
128
|
+
|
|
129
|
+
function DownloadButton() {
|
|
130
|
+
const track = useTrackEvent();
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<button onClick={() => track('file_downloaded', { format: 'pdf' })}>
|
|
134
|
+
Download PDF
|
|
135
|
+
</button>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Using HTML Attributes (Zero JS)
|
|
141
|
+
|
|
142
|
+
Track events declaratively without writing any handler code:
|
|
143
|
+
|
|
144
|
+
```html
|
|
145
|
+
<button temps-event-name="cta_click" temps-data-section="hero" temps-data-variant="blue">
|
|
146
|
+
Get Started
|
|
147
|
+
</button>
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Any element with `temps-event-name` fires an event on click. All `temps-data-*` attributes are sent as event properties.
|
|
151
|
+
|
|
152
|
+
## Hooks Reference
|
|
153
|
+
|
|
154
|
+
### `useTempsAnalytics()`
|
|
155
|
+
|
|
156
|
+
The core hook. Returns the full analytics context.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const { trackEvent, trackPageview, identify, enabled } = useTempsAnalytics();
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
| Property | Type | Description |
|
|
163
|
+
|----------|------|-------------|
|
|
164
|
+
| `trackEvent` | `(name: string, data?: Record<string, JsonValue>) => Promise<void>` | Send a custom event |
|
|
165
|
+
| `trackPageview` | `() => void` | Manually fire a pageview |
|
|
166
|
+
| `identify` | `(userId: string, traits?) => void` | Identify a user (placeholder for future use) |
|
|
167
|
+
| `enabled` | `boolean` | Whether analytics are currently active |
|
|
168
|
+
|
|
169
|
+
### `useTrackEvent()`
|
|
170
|
+
|
|
171
|
+
Convenience hook that returns just the `trackEvent` function.
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const track = useTrackEvent();
|
|
175
|
+
track('button_clicked', { id: 'cta-hero' });
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### `useTrackPageview()`
|
|
179
|
+
|
|
180
|
+
Returns a function to manually trigger a pageview.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
const trackPageview = useTrackPageview();
|
|
184
|
+
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
trackPageview(); // Fire on mount
|
|
187
|
+
}, []);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### `usePageLeave(options?)`
|
|
191
|
+
|
|
192
|
+
Track when users leave a page, including time spent.
|
|
193
|
+
|
|
194
|
+
```tsx
|
|
195
|
+
import { usePageLeave } from '@temps-sdk/react-analytics';
|
|
196
|
+
|
|
197
|
+
function ArticlePage() {
|
|
198
|
+
const { triggerPageLeave } = usePageLeave({
|
|
199
|
+
eventName: 'article_leave', // Default: 'page_leave'
|
|
200
|
+
eventData: { category: 'blog' }, // Extra data merged into the event
|
|
201
|
+
enabled: true, // Default: true
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// Fires automatically on pagehide/beforeunload.
|
|
205
|
+
// Or trigger manually before client-side navigation:
|
|
206
|
+
const handleNavigation = () => {
|
|
207
|
+
triggerPageLeave();
|
|
208
|
+
router.push('/next-page');
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The event payload automatically includes `time_on_page_ms`, `url`, `referrer`, and `timestamp`.
|
|
214
|
+
|
|
215
|
+
### `useSpeedAnalytics(options?)`
|
|
216
|
+
|
|
217
|
+
Tracks [Web Vitals](https://web.dev/vitals/) performance metrics. Enabled automatically by the provider, but can be used standalone.
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { useSpeedAnalytics } from '@temps-sdk/react-analytics';
|
|
221
|
+
|
|
222
|
+
useSpeedAnalytics({
|
|
223
|
+
basePath: '/api/_temps',
|
|
224
|
+
disabled: false,
|
|
225
|
+
});
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**Metrics collected:**
|
|
229
|
+
|
|
230
|
+
| Metric | Full Name | Description |
|
|
231
|
+
|--------|-----------|-------------|
|
|
232
|
+
| **TTFB** | Time to First Byte | Server responsiveness |
|
|
233
|
+
| **FCP** | First Contentful Paint | First pixels on screen |
|
|
234
|
+
| **LCP** | Largest Contentful Paint | Main content visible |
|
|
235
|
+
| **FID** | First Input Delay | Input responsiveness |
|
|
236
|
+
| **CLS** | Cumulative Layout Shift | Visual stability |
|
|
237
|
+
| **INP** | Interaction to Next Paint | Overall responsiveness |
|
|
238
|
+
|
|
239
|
+
Initial metrics (TTFB, FCP, LCP, FID) are batched into a single request. Late metrics (CLS, INP) are sent individually as they stabilize.
|
|
240
|
+
|
|
241
|
+
### `useEngagementTracking(options?)`
|
|
242
|
+
|
|
243
|
+
Manual engagement tracking for specific pages or components. Useful when you need per-component engagement data or custom callbacks.
|
|
244
|
+
|
|
245
|
+
```tsx
|
|
246
|
+
import { useEngagementTracking } from '@temps-sdk/react-analytics';
|
|
247
|
+
|
|
248
|
+
function LongArticle() {
|
|
249
|
+
const { engagementData, isTracking } = useEngagementTracking({
|
|
250
|
+
heartbeatInterval: 15000, // 15s heartbeats
|
|
251
|
+
engagementThreshold: 5000, // Engaged after 5s
|
|
252
|
+
onEngagementUpdate: (data) => {
|
|
253
|
+
console.log(`Active for ${data.engagement_time_seconds}s`);
|
|
254
|
+
},
|
|
255
|
+
onPageLeave: (data) => {
|
|
256
|
+
console.log(`Total time: ${data.total_time_seconds}s`);
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
return <article>{/* ... */}</article>;
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
**Engagement data shape:**
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
interface EngagementData {
|
|
268
|
+
engagement_time_seconds: number; // Active time on page
|
|
269
|
+
total_time_seconds: number; // Total time on page
|
|
270
|
+
heartbeat_count: number; // Number of heartbeats sent
|
|
271
|
+
is_engaged: boolean; // Currently engaged
|
|
272
|
+
is_visible: boolean; // Page is visible (not backgrounded)
|
|
273
|
+
time_since_last_activity: number; // Seconds since last interaction
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### `useScrollVisibility(options?)`
|
|
278
|
+
|
|
279
|
+
Track when elements scroll into view using Intersection Observer. Returns a ref callback to attach to any element.
|
|
280
|
+
|
|
281
|
+
```tsx
|
|
282
|
+
import { useScrollVisibility } from '@temps-sdk/react-analytics';
|
|
283
|
+
|
|
284
|
+
function PricingSection() {
|
|
285
|
+
const ref = useScrollVisibility({
|
|
286
|
+
eventName: 'pricing_viewed',
|
|
287
|
+
eventData: { section: 'pricing' },
|
|
288
|
+
threshold: 0.75, // 75% of element must be visible
|
|
289
|
+
once: true, // Fire only once (default)
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
return <section ref={ref}>Pricing Plans</section>;
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
| Option | Type | Default | Description |
|
|
297
|
+
|--------|------|---------|-------------|
|
|
298
|
+
| `eventName` | `string` | `'component_visible'` | Event name to fire |
|
|
299
|
+
| `eventData` | `Record<string, JsonValue>` | - | Extra data |
|
|
300
|
+
| `threshold` | `number` | `0.5` | Visibility percentage (0.0 - 1.0) |
|
|
301
|
+
| `root` | `Element \| null` | `null` (viewport) | Scroll container |
|
|
302
|
+
| `rootMargin` | `string` | `'0px'` | Margin around root |
|
|
303
|
+
| `once` | `boolean` | `true` | Track only the first time |
|
|
304
|
+
| `enabled` | `boolean` | `true` | Enable/disable tracking |
|
|
305
|
+
|
|
306
|
+
### `useSessionRecording()`
|
|
307
|
+
|
|
308
|
+
Control session recording programmatically from within the provider.
|
|
309
|
+
|
|
310
|
+
```tsx
|
|
311
|
+
import { useSessionRecording } from '@temps-sdk/react-analytics';
|
|
312
|
+
|
|
313
|
+
function RecordingToggle() {
|
|
314
|
+
const {
|
|
315
|
+
isRecordingEnabled,
|
|
316
|
+
enableRecording,
|
|
317
|
+
disableRecording,
|
|
318
|
+
toggleRecording,
|
|
319
|
+
sessionId,
|
|
320
|
+
} = useSessionRecording();
|
|
321
|
+
|
|
322
|
+
return (
|
|
323
|
+
<div>
|
|
324
|
+
<p>Recording: {isRecordingEnabled ? 'ON' : 'OFF'}</p>
|
|
325
|
+
{sessionId && <p>Session: {sessionId}</p>}
|
|
326
|
+
<button onClick={toggleRecording}>Toggle</button>
|
|
327
|
+
</div>
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### `useSessionRecordingControl(defaultEnabled?)`
|
|
333
|
+
|
|
334
|
+
Standalone recording control without needing the provider context. Persists user preference to `localStorage`.
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
const { isEnabled, enable, disable, toggle } = useSessionRecordingControl(false);
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### `useAnalytics(options)`
|
|
341
|
+
|
|
342
|
+
Low-level hook for plugging in a custom analytics client. Useful if you want the hook interface without the Temps provider.
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
import { useAnalytics } from '@temps-sdk/react-analytics';
|
|
346
|
+
|
|
347
|
+
const track = useAnalytics({
|
|
348
|
+
client: {
|
|
349
|
+
track: (event, data) => myAnalytics.send(event, data),
|
|
350
|
+
identify: (userId) => myAnalytics.identify(userId),
|
|
351
|
+
},
|
|
352
|
+
defaultContext: { app: 'my-app' },
|
|
353
|
+
});
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## Session Recording
|
|
357
|
+
|
|
358
|
+
Session recording captures DOM mutations and user interactions using [rrweb](https://www.rrweb.io/) and streams them to your Temps instance for replay.
|
|
359
|
+
|
|
360
|
+
### Enable Recording
|
|
361
|
+
|
|
362
|
+
```tsx
|
|
363
|
+
<TempsAnalyticsProvider
|
|
364
|
+
enableSessionRecording={true}
|
|
365
|
+
sessionRecordingConfig={{
|
|
366
|
+
sessionSampleRate: 0.1, // Record 10% of sessions
|
|
367
|
+
}}
|
|
368
|
+
>
|
|
369
|
+
{children}
|
|
370
|
+
</TempsAnalyticsProvider>
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### Privacy Controls
|
|
374
|
+
|
|
375
|
+
Recording respects user privacy by default:
|
|
376
|
+
|
|
377
|
+
```tsx
|
|
378
|
+
sessionRecordingConfig={{
|
|
379
|
+
maskAllInputs: true, // All input values are masked
|
|
380
|
+
maskTextSelector: '[data-mask]', // Mask specific text elements
|
|
381
|
+
blockClass: 'rr-block', // Block elements entirely
|
|
382
|
+
ignoreClass: 'rr-ignore', // Ignore elements from recording
|
|
383
|
+
}}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
**In your markup:**
|
|
387
|
+
|
|
388
|
+
```html
|
|
389
|
+
<!-- All inputs are masked by default (passwords, emails, etc.) -->
|
|
390
|
+
<input type="password" />
|
|
391
|
+
|
|
392
|
+
<!-- This element won't appear in recordings at all -->
|
|
393
|
+
<div class="rr-block">Sensitive content</div>
|
|
394
|
+
|
|
395
|
+
<!-- This text will be replaced with asterisks -->
|
|
396
|
+
<p data-mask>User's private note</p>
|
|
397
|
+
|
|
398
|
+
<!-- This element is ignored (not captured) -->
|
|
399
|
+
<aside class="rr-ignore">Debug panel</aside>
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### Path Exclusions
|
|
403
|
+
|
|
404
|
+
Exclude specific routes from recording:
|
|
405
|
+
|
|
406
|
+
```tsx
|
|
407
|
+
sessionRecordingConfig={{
|
|
408
|
+
excludedPaths: [
|
|
409
|
+
'/settings/*', // Wildcard: all settings subpages
|
|
410
|
+
'/admin/*', // Wildcard: entire admin area
|
|
411
|
+
'/checkout', // Exact match
|
|
412
|
+
],
|
|
413
|
+
}}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
Recording automatically pauses when navigating to excluded paths and resumes when leaving them.
|
|
417
|
+
|
|
418
|
+
### How It Works
|
|
419
|
+
|
|
420
|
+
1. `SessionRecorder` initializes a session via `POST /api/_temps/session-replay/init` with device metadata
|
|
421
|
+
2. DOM mutations are captured by rrweb, packed with `@rrweb/packer`, and batched (100 events or 10s)
|
|
422
|
+
3. Batches are base64-encoded and sent to `POST /api/_temps/session-replay/events`
|
|
423
|
+
4. On page unload, remaining events are flushed via `navigator.sendBeacon` for reliability
|
|
424
|
+
5. Failed sends use exponential backoff (up to 5 retries before dropping)
|
|
425
|
+
6. Full DOM snapshots are taken every 30s or every 200 events for replay accuracy
|
|
426
|
+
|
|
427
|
+
## Standalone Engagement Tracker
|
|
428
|
+
|
|
429
|
+
For non-React environments or custom integrations, use the `EngagementTracker` class directly:
|
|
430
|
+
|
|
431
|
+
```typescript
|
|
432
|
+
import { EngagementTracker } from '@temps-sdk/react-analytics';
|
|
433
|
+
|
|
434
|
+
const tracker = new EngagementTracker({
|
|
435
|
+
basePath: '/api/_temps',
|
|
436
|
+
domain: 'example.com',
|
|
437
|
+
heartbeatInterval: 30000,
|
|
438
|
+
inactivityTimeout: 30000,
|
|
439
|
+
engagementThreshold: 10000,
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
// Clean up when done
|
|
443
|
+
tracker.destroy();
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
## Framework Setup
|
|
447
|
+
|
|
448
|
+
### Next.js (App Router)
|
|
449
|
+
|
|
450
|
+
The SDK ships with `"use client"` directives. Create a client-side provider wrapper:
|
|
451
|
+
|
|
452
|
+
```tsx
|
|
453
|
+
// app/providers.tsx
|
|
454
|
+
'use client';
|
|
455
|
+
|
|
456
|
+
import { TempsAnalyticsProvider } from '@temps-sdk/react-analytics';
|
|
457
|
+
|
|
458
|
+
export function Providers({ children }: { children: React.ReactNode }) {
|
|
459
|
+
return (
|
|
460
|
+
<TempsAnalyticsProvider
|
|
461
|
+
enableSessionRecording={process.env.NODE_ENV === 'production'}
|
|
462
|
+
>
|
|
463
|
+
{children}
|
|
464
|
+
</TempsAnalyticsProvider>
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// app/layout.tsx
|
|
469
|
+
import { Providers } from './providers';
|
|
470
|
+
|
|
471
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
472
|
+
return (
|
|
473
|
+
<html>
|
|
474
|
+
<body>
|
|
475
|
+
<Providers>{children}</Providers>
|
|
476
|
+
</body>
|
|
477
|
+
</html>
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### Vite / Create React App
|
|
483
|
+
|
|
484
|
+
```tsx
|
|
485
|
+
// main.tsx
|
|
486
|
+
import { TempsAnalyticsProvider } from '@temps-sdk/react-analytics';
|
|
487
|
+
|
|
488
|
+
ReactDOM.createRoot(document.getElementById('root')!).render(
|
|
489
|
+
<TempsAnalyticsProvider>
|
|
490
|
+
<App />
|
|
491
|
+
</TempsAnalyticsProvider>
|
|
492
|
+
);
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
## Environment Detection
|
|
496
|
+
|
|
497
|
+
The SDK automatically disables itself in non-production environments when `ignoreLocalhost` is `true` (default):
|
|
498
|
+
|
|
499
|
+
| Environment | Detection method |
|
|
500
|
+
|---|---|
|
|
501
|
+
| Localhost | `localhost`, `127.0.0.1`, `::1`, `*.local` |
|
|
502
|
+
| Test runners | `jest`, `mocha`, `vitest`, `cypress` globals |
|
|
503
|
+
| Manual override | `localStorage.temps_ignore = "true"` |
|
|
504
|
+
|
|
505
|
+
## TypeScript
|
|
506
|
+
|
|
507
|
+
All hooks, components, and types are fully typed and exported:
|
|
508
|
+
|
|
509
|
+
```typescript
|
|
510
|
+
import type {
|
|
511
|
+
AnalyticsContextValue,
|
|
512
|
+
TempsAnalyticsProviderProps,
|
|
513
|
+
EngagementData,
|
|
514
|
+
EngagementTrackerOptions,
|
|
515
|
+
WebVitalMetric,
|
|
516
|
+
SpeedMetric,
|
|
517
|
+
JsonValue,
|
|
518
|
+
} from '@temps-sdk/react-analytics';
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
## Requirements
|
|
522
|
+
|
|
523
|
+
- React 18+ or React 19
|
|
524
|
+
- A running Temps instance with the analytics proxy configured
|
|
525
|
+
|
|
526
|
+
## Related
|
|
527
|
+
|
|
528
|
+
- [`@temps-sdk/kv`](https://www.npmjs.com/package/@temps-sdk/kv) -- Key-value store
|
|
529
|
+
- [`@temps-sdk/blob`](https://www.npmjs.com/package/@temps-sdk/blob) -- File storage
|
|
530
|
+
- [`@temps-sdk/node-sdk`](https://www.npmjs.com/package/@temps-sdk/node-sdk) -- Full platform API client and server-side error tracking
|
|
531
|
+
|
|
532
|
+
## License
|
|
533
|
+
|
|
534
|
+
MIT
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export interface EngagementTrackerOptions {
|
|
2
|
+
basePath?: string;
|
|
3
|
+
domain?: string;
|
|
4
|
+
heartbeatInterval?: number;
|
|
5
|
+
inactivityTimeout?: number;
|
|
6
|
+
engagementThreshold?: number;
|
|
7
|
+
onHeartbeat?: (data: EngagementData) => void;
|
|
8
|
+
onPageLeave?: (data: EngagementData) => void;
|
|
9
|
+
}
|
|
10
|
+
export interface EngagementData {
|
|
11
|
+
engagement_time_seconds: number;
|
|
12
|
+
total_time_seconds: number;
|
|
13
|
+
heartbeat_count: number;
|
|
14
|
+
is_engaged: boolean;
|
|
15
|
+
is_visible: boolean;
|
|
16
|
+
time_since_last_activity: number;
|
|
17
|
+
}
|
|
18
|
+
export declare class EngagementTracker {
|
|
19
|
+
private startTime;
|
|
20
|
+
private engagementTime;
|
|
21
|
+
private lastActivityTime;
|
|
22
|
+
private lastEngagementStart;
|
|
23
|
+
private heartbeatCount;
|
|
24
|
+
private heartbeatInterval;
|
|
25
|
+
private inactivityTimeout;
|
|
26
|
+
private engagementThreshold;
|
|
27
|
+
private isVisible;
|
|
28
|
+
private isFocused;
|
|
29
|
+
private isActive;
|
|
30
|
+
private heartbeatTimer;
|
|
31
|
+
private inactivityTimer;
|
|
32
|
+
private hasTrackedLeave;
|
|
33
|
+
private readonly basePath;
|
|
34
|
+
private readonly domain;
|
|
35
|
+
private readonly onHeartbeat?;
|
|
36
|
+
private readonly onPageLeave?;
|
|
37
|
+
constructor(options?: EngagementTrackerOptions);
|
|
38
|
+
private setupEventListeners;
|
|
39
|
+
private handleVisibilityChange;
|
|
40
|
+
private handleFocus;
|
|
41
|
+
private handleBlur;
|
|
42
|
+
private pauseEngagement;
|
|
43
|
+
private handleUserActivity;
|
|
44
|
+
private resetInactivityTimer;
|
|
45
|
+
private updateEngagementTime;
|
|
46
|
+
private startHeartbeat;
|
|
47
|
+
private stopHeartbeat;
|
|
48
|
+
private sendHeartbeat;
|
|
49
|
+
private handlePageLeave;
|
|
50
|
+
private handleNavigation;
|
|
51
|
+
private getEngagementData;
|
|
52
|
+
private reset;
|
|
53
|
+
destroy(): void;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=EngagementTracker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EngagementTracker.d.ts","sourceRoot":"","sources":["../src/EngagementTracker.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;IAC7C,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;CAC9C;AAED,MAAM,WAAW,cAAc;IAC7B,uBAAuB,EAAE,MAAM,CAAC;IAChC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,eAAe,EAAE,MAAM,CAAC;IACxB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,OAAO,CAAC;IACpB,wBAAwB,EAAE,MAAM,CAAC;CAClC;AAED,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,cAAc,CAAa;IACnC,OAAO,CAAC,gBAAgB,CAAS;IACjC,OAAO,CAAC,mBAAmB,CAAuB;IAClD,OAAO,CAAC,cAAc,CAAa;IACnC,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,mBAAmB,CAAS;IACpC,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,cAAc,CAA+C;IACrE,OAAO,CAAC,eAAe,CAA8C;IACrE,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAiC;IAC9D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAiC;gBAElD,OAAO,GAAE,wBAA6B;IAsBlD,OAAO,CAAC,mBAAmB;IA6B3B,OAAO,CAAC,sBAAsB,CAW5B;IAEF,OAAO,CAAC,WAAW,CAEjB;IAEF,OAAO,CAAC,UAAU,CAIhB;IAEF,OAAO,CAAC,eAAe;IAQvB,OAAO,CAAC,kBAAkB,CAexB;IAEF,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,oBAAoB;IAO5B,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,aAAa;YAOP,aAAa;IA+B3B,OAAO,CAAC,eAAe,CAyBrB;IAEF,OAAO,CAAC,gBAAgB,CAGtB;IAEF,OAAO,CAAC,iBAAiB;IAczB,OAAO,CAAC,KAAK;IAeN,OAAO,IAAI,IAAI;CAoBvB"}
|