Skip to content

Commit 4194008

Browse files
author
Joan León
authored
Merge pull request #1724 from SUI-Components/feat/web-vitals-device-data
feat(packages/sui-react-web-vitals): add some device data to sent to Open Search
2 parents ee4769a + bef9b26 commit 4194008

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

packages/sui-react-web-vitals/src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export default function WebVitalsReporter({
5757
}, [onReport])
5858

5959
useMount(() => {
60+
const {deviceMemory, connection: {effectiveType} = {}, hardwareConcurrency} = window.navigator || {}
61+
6062
const getRouteid = () => {
6163
return route?.id
6264
}
@@ -102,16 +104,20 @@ export default function WebVitalsReporter({
102104
if (!isAllowed || !logger?.cwv || rating === RATING.GOOD) return
103105

104106
const target = getTarget({name, attribution})
107+
const {loadState, eventType} = attribution
105108

106109
logger.cwv({
107110
name: `cwv.${name.toLowerCase()}`,
108111
amount,
109112
path: pathname,
110-
...(routeid && {routeId: routeid}),
111113
target,
112-
loadState: attribution.loadState,
113114
visibilityState: document.visibilityState,
114-
...(attribution.eventType && {eventType: attribution.eventType})
115+
...(routeid && {routeId: routeid}),
116+
...(loadState && {loadState}),
117+
...(eventType && {eventType}),
118+
...(deviceMemory && {deviceMemory}),
119+
...(effectiveType && {effectiveType}),
120+
...(hardwareConcurrency && {hardwareConcurrency})
115121
})
116122
}
117123

packages/sui-react-web-vitals/test/browser/indexSpec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,44 @@ describe('WebVitalsReporter', () => {
278278
).to.be.true
279279
])
280280
})
281+
282+
it.skip('should track inp with deviceMemory, networkConnection, and hardwareConcurrency using logger cwv', async () => {
283+
// Mocking the visibilityState
284+
Object.defineProperty(window.document, 'visibilityState', {value: 'hidden', writable: false})
285+
286+
const logger = {
287+
cwv: sinon.spy()
288+
}
289+
290+
const reporter = {
291+
onINP: fn => {
292+
fn({
293+
attribution: {eventType: 'body'},
294+
deviceMemory: 8,
295+
effectiveType: '4g',
296+
hardwareConcurrency: 10,
297+
name: 'INP',
298+
rating: 'poor',
299+
value: 304
300+
})
301+
}
302+
}
303+
304+
render(<WebVitalsReporter metrics={[METRICS.INP]} allowed={['/']} reporter={reporter} />, {logger})
305+
await waitFor(() => [
306+
expect(
307+
logger.cwv.calledOnceWithMatch({
308+
name: 'cwv.inp',
309+
amount: 304,
310+
path: '/',
311+
target: undefined,
312+
visibilityState: 'hidden',
313+
eventType: 'body',
314+
deviceMemory: 8,
315+
effectiveType: '4g',
316+
hardwareConcurrency: 10
317+
})
318+
).to.be.true
319+
])
320+
})
281321
})

0 commit comments

Comments
 (0)