-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathinstrumentation-client.ts
More file actions
219 lines (189 loc) · 6.09 KB
/
instrumentation-client.ts
File metadata and controls
219 lines (189 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Sim Telemetry - Client-side Instrumentation
*/
import { env } from './lib/core/config/env'
import { sanitizeEventData } from './lib/core/security/redaction'
if (typeof window !== 'undefined') {
const TELEMETRY_STATUS_KEY = 'simstudio-telemetry-status'
const BATCH_INTERVAL_MS = 10000 // Send batches every 10 seconds
const MAX_BATCH_SIZE = 50 // Max events per batch
let telemetryEnabled = true
const eventBatch: any[] = []
let batchTimer: NodeJS.Timeout | null = null
try {
if (env.NEXT_TELEMETRY_DISABLED === '1') {
telemetryEnabled = false
} else {
const storedPreference = localStorage.getItem(TELEMETRY_STATUS_KEY)
if (storedPreference) {
const status = JSON.parse(storedPreference)
telemetryEnabled = status.enabled
}
}
} catch (_e) {
telemetryEnabled = false
}
/**
* Add event to batch and schedule flush
*/
function addToBatch(event: any): void {
if (!telemetryEnabled) return
eventBatch.push(event)
if (eventBatch.length >= MAX_BATCH_SIZE) {
flushBatch()
} else if (!batchTimer) {
batchTimer = setTimeout(flushBatch, BATCH_INTERVAL_MS)
}
}
/**
* Flush batch of events to server
*/
function flushBatch(): void {
if (eventBatch.length === 0) return
const batch = eventBatch.splice(0, eventBatch.length)
if (batchTimer) {
clearTimeout(batchTimer)
batchTimer = null
}
const sanitizedBatch = batch.map(sanitizeEventData)
const payload = JSON.stringify({
category: 'batch',
action: 'client_events',
events: sanitizedBatch,
timestamp: Date.now(),
})
const payloadSize = new Blob([payload]).size
const MAX_BEACON_SIZE = 64 * 1024 // 64KB
if (navigator.sendBeacon && payloadSize < MAX_BEACON_SIZE) {
const sent = navigator.sendBeacon('/api/telemetry', payload)
if (!sent) {
fetch('/api/telemetry', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: payload,
keepalive: true,
}).catch(() => {
// Silently fail
})
}
} else {
fetch('/api/telemetry', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: payload,
keepalive: true,
}).catch(() => {
// Silently fail
})
}
}
window.addEventListener('beforeunload', flushBatch)
window.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
flushBatch()
}
})
/**
* Global event tracking function
*/
;(window as any).__SIM_TELEMETRY_ENABLED = telemetryEnabled
;(window as any).__SIM_TRACK_EVENT = (eventName: string, properties?: any) => {
if (!telemetryEnabled) return
addToBatch({
category: 'feature_usage',
action: eventName,
timestamp: Date.now(),
...(properties || {}),
})
}
if (telemetryEnabled) {
const shouldTrackVitals = Math.random() < 0.1
if (shouldTrackVitals) {
window.addEventListener(
'load',
() => {
if (typeof PerformanceObserver !== 'undefined') {
const lcpObserver = new PerformanceObserver((list) => {
const entries = list.getEntries()
const lastEntry = entries[entries.length - 1]
if (lastEntry) {
addToBatch({
category: 'performance',
action: 'web_vital',
label: 'LCP',
value: (lastEntry as any).startTime || 0,
entryType: 'largest-contentful-paint',
timestamp: Date.now(),
})
}
lcpObserver.disconnect()
})
let clsValue = 0
const clsObserver = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (!(entry as any).hadRecentInput) {
clsValue += (entry as any).value || 0
}
}
})
const fidObserver = new PerformanceObserver((list) => {
const entries = list.getEntries()
for (const entry of entries) {
const fidValue =
((entry as any).processingStart || 0) - ((entry as any).startTime || 0)
addToBatch({
category: 'performance',
action: 'web_vital',
label: 'FID',
value: fidValue,
entryType: 'first-input',
timestamp: Date.now(),
})
}
fidObserver.disconnect()
})
window.addEventListener('beforeunload', () => {
if (clsValue > 0) {
addToBatch({
category: 'performance',
action: 'web_vital',
label: 'CLS',
value: clsValue,
entryType: 'layout-shift',
timestamp: Date.now(),
})
}
clsObserver.disconnect()
})
lcpObserver.observe({ type: 'largest-contentful-paint', buffered: true })
clsObserver.observe({ type: 'layout-shift', buffered: true })
fidObserver.observe({ type: 'first-input', buffered: true })
}
},
{ once: true }
)
}
window.addEventListener('error', (event) => {
if (telemetryEnabled && !event.defaultPrevented) {
addToBatch({
category: 'error',
action: 'unhandled_error',
message: event.error?.message || event.message || 'Unknown error',
url: window.location.pathname,
timestamp: Date.now(),
})
}
})
window.addEventListener('unhandledrejection', (event) => {
if (telemetryEnabled) {
addToBatch({
category: 'error',
action: 'unhandled_rejection',
message: event.reason?.message || String(event.reason) || 'Unhandled promise rejection',
url: window.location.pathname,
timestamp: Date.now(),
})
}
})
}
}