@@ -8,12 +8,23 @@ import { Transaction } from '../transaction';
88import { msToSec } from '../utils' ;
99import { getFID } from './web-vitals/getFID' ;
1010import { getLCP } from './web-vitals/getLCP' ;
11+ import { NavigatorDeviceMemory , NavigatorNetworkInformation } from './web-vitals/types' ;
1112
1213const global = getGlobalObject < Window > ( ) ;
1314
15+ type FooNavigator = Navigator & NavigatorNetworkInformation & NavigatorDeviceMemory ;
16+
17+ type BrowserContext = {
18+ effectiveConnectionType ?: string ;
19+ deviceMemory ?: number ;
20+ // number of CPUs
21+ hardwareConcurrency ?: number ;
22+ } ;
23+
1424/** Class tracking metrics */
1525export class MetricsInstrumentation {
1626 private _measurements : Measurements = { } ;
27+ private _browserContext : BrowserContext = { } ;
1728
1829 private _performanceCursor : number = 0 ;
1930
@@ -25,6 +36,7 @@ export class MetricsInstrumentation {
2536
2637 this . _trackLCP ( ) ;
2738 this . _trackFID ( ) ;
39+ this . _trackNavigator ( ) ;
2840 }
2941 }
3042
@@ -122,10 +134,44 @@ export class MetricsInstrumentation {
122134
123135 // Measurements are only available for pageload transactions
124136 if ( transaction . op === 'pageload' ) {
137+ this . _trackNavigator ( ) ;
138+ transaction . setContexts ( { browser : this . _browserContext } ) ;
125139 transaction . setMeasurements ( this . _measurements ) ;
126140 }
127141 }
128142
143+ /**
144+ * Capture the information of the user agent.
145+ */
146+ private _trackNavigator ( ) : void {
147+ const navigator = window . navigator as null | FooNavigator ;
148+
149+ // track network connectivity
150+
151+ const connection = navigator ?. connection ;
152+ if ( connection ) {
153+ if ( connection . effectiveType ) {
154+ this . _browserContext . effectiveConnectionType = connection . effectiveType ;
155+ }
156+
157+ if ( typeof connection . rtt === 'number' ) {
158+ this . _measurements [ 'connection.rtt' ] = { value : connection . rtt } ;
159+ }
160+
161+ if ( typeof connection . downlink === 'number' ) {
162+ this . _measurements [ 'connection.downlink' ] = { value : connection . downlink } ;
163+ }
164+ }
165+
166+ if ( typeof navigator ?. deviceMemory === 'number' ) {
167+ this . _browserContext . deviceMemory = navigator . deviceMemory ;
168+ }
169+
170+ if ( typeof navigator ?. hardwareConcurrency === 'number' ) {
171+ this . _browserContext . hardwareConcurrency = navigator . hardwareConcurrency ;
172+ }
173+ }
174+
129175 /** Starts tracking the Largest Contentful Paint on the current page. */
130176 private _trackLCP ( ) : void {
131177 getLCP ( metric => {
0 commit comments