22
22
* for instance the href of a link, or a search term
23
23
*/
24
24
function sampleRUM ( checkpoint , data = { } ) {
25
- sampleRUM . baseURL = sampleRUM . baseURL || new URL ( window . RUM_BASE == null ? 'https://rum.hlx.page' : window . RUM_BASE , window . location ) ;
25
+ sampleRUM . baseURL = sampleRUM . baseURL
26
+ || new URL ( window . RUM_BASE == null ? 'https://rum.hlx.page' : window . RUM_BASE , window . location ) ;
26
27
sampleRUM . defer = sampleRUM . defer || [ ] ;
27
28
const defer = ( fnname ) => {
28
- sampleRUM [ fnname ] = sampleRUM [ fnname ]
29
- || ( ( ...args ) => sampleRUM . defer . push ( { fnname, args } ) ) ;
29
+ sampleRUM [ fnname ] = sampleRUM [ fnname ] || ( ( ...args ) => sampleRUM . defer . push ( { fnname, args } ) ) ;
30
30
} ;
31
31
sampleRUM . drain = sampleRUM . drain
32
32
|| ( ( dfnname , fn ) => {
@@ -48,28 +48,57 @@ function sampleRUM(checkpoint, data = {}) {
48
48
window . hlx = window . hlx || { } ;
49
49
if ( ! window . hlx . rum ) {
50
50
const usp = new URLSearchParams ( window . location . search ) ;
51
- const weight = ( usp . get ( 'rum' ) === 'on' ) ? 1 : 100 ; // with parameter, weight is 1. Defaults to 100.
51
+ const weight = usp . get ( 'rum' ) === 'on' ? 1 : 100 ; // with parameter, weight is 1. Defaults to 100.
52
52
const id = Math . random ( ) . toString ( 36 ) . slice ( - 4 ) ;
53
53
const random = Math . random ( ) ;
54
- const isSelected = ( random * weight < 1 ) ;
54
+ const isSelected = random * weight < 1 ;
55
55
const firstReadTime = window . performance ? window . performance . timeOrigin : Date . now ( ) ;
56
56
const urlSanitizers = {
57
57
full : ( ) => window . location . href ,
58
58
origin : ( ) => window . location . origin ,
59
59
path : ( ) => window . location . href . replace ( / \? .* $ / , '' ) ,
60
60
} ;
61
61
// eslint-disable-next-line object-curly-newline, max-len
62
- window . hlx . rum = { weight, id, random, isSelected, firstReadTime, sampleRUM, sanitizeURL : urlSanitizers [ window . hlx . RUM_MASK_URL || 'path' ] } ;
62
+ window . hlx . rum = {
63
+ weight,
64
+ id,
65
+ random,
66
+ isSelected,
67
+ firstReadTime,
68
+ sampleRUM,
69
+ sanitizeURL : urlSanitizers [ window . hlx . RUM_MASK_URL || 'path' ] ,
70
+ } ;
63
71
}
64
72
65
73
const { weight, id, firstReadTime } = window . hlx . rum ;
66
74
if ( window . hlx && window . hlx . rum && window . hlx . rum . isSelected ) {
67
- const knownProperties = [ 'weight' , 'id' , 'referer' , 'checkpoint' , 't' , 'source' , 'target' , 'cwv' , 'CLS' , 'FID' , 'LCP' , 'INP' , 'TTFB' ] ;
75
+ const knownProperties = [
76
+ 'weight' ,
77
+ 'id' ,
78
+ 'referer' ,
79
+ 'checkpoint' ,
80
+ 't' ,
81
+ 'source' ,
82
+ 'target' ,
83
+ 'cwv' ,
84
+ 'CLS' ,
85
+ 'FID' ,
86
+ 'LCP' ,
87
+ 'INP' ,
88
+ 'TTFB' ,
89
+ ] ;
68
90
const sendPing = ( pdata = data ) => {
69
91
// eslint-disable-next-line max-len
70
- const t = Math . round ( window . performance ? window . performance . now ( ) : ( Date . now ( ) - firstReadTime ) ) ;
92
+ const t = Math . round (
93
+ window . performance ? window . performance . now ( ) : Date . now ( ) - firstReadTime ,
94
+ ) ;
71
95
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
72
- const body = JSON . stringify ( { weight, id, referer : window . hlx . rum . sanitizeURL ( ) , checkpoint, t, ...data } , knownProperties ) ;
96
+ const body = JSON . stringify (
97
+ {
98
+ weight, id, referer : window . hlx . rum . sanitizeURL ( ) , checkpoint, t, ...data ,
99
+ } ,
100
+ knownProperties ,
101
+ ) ;
73
102
const url = new URL ( `.rum/${ weight } ` , sampleRUM . baseURL ) . href ;
74
103
navigator . sendBeacon ( url , body ) ;
75
104
// eslint-disable-next-line no-console
@@ -80,7 +109,10 @@ function sampleRUM(checkpoint, data = {}) {
80
109
lazy : ( ) => {
81
110
// use classic script to avoid CORS issues
82
111
const script = document . createElement ( 'script' ) ;
83
- script . src = new URL ( '.rum/@adobe/helix-rum-enhancer@^1/src/index.js' , sampleRUM . baseURL ) . href ;
112
+ script . src = new URL (
113
+ '.rum/@adobe/helix-rum-enhancer@^1/src/index.js' ,
114
+ sampleRUM . baseURL ,
115
+ ) . href ;
84
116
document . head . appendChild ( script ) ;
85
117
return true ;
86
118
} ,
@@ -133,11 +165,15 @@ function init() {
133
165
const errData = { source : 'undefined error' } ;
134
166
try {
135
167
errData . target = ( reason || error ) . toString ( ) ;
136
- errData . source = ( reason || error ) . stack . split ( '\n' )
137
- . filter ( ( line ) => line . match ( / h t t p s ? : \/ \/ / ) ) . shift ( )
168
+ errData . source = ( reason || error ) . stack
169
+ . split ( '\n' )
170
+ . filter ( ( line ) => line . match ( / h t t p s ? : \/ \/ / ) )
171
+ . shift ( )
138
172
. replace ( / a t ( [ ^ ] + ) \( ( .+ ) \) / , '$1@$2' )
139
173
. trim ( ) ;
140
- } catch ( err ) { /* error structure was not as expected */ }
174
+ } catch ( err ) {
175
+ /* error structure was not as expected */
176
+ }
141
177
sampleRUM ( 'error' , errData ) ;
142
178
} ) ;
143
179
} ) ;
0 commit comments