22
22
* for instance the href of a link, or a search term
23
23
*/
24
24
function sampleRUM ( checkpoint , data = { } ) {
25
- const SESSION_STORAGE_KEY = 'aem-rum' ;
26
- sampleRUM . baseURL = sampleRUM . baseURL
27
- || new URL ( window . RUM_BASE == null ? 'https://rum.hlx.page' : window . RUM_BASE , window . location ) ;
25
+ sampleRUM . baseURL = sampleRUM . baseURL || new URL ( window . RUM_BASE == null ? 'https://rum.hlx.page' : window . RUM_BASE , window . location ) ;
28
26
sampleRUM . defer = sampleRUM . defer || [ ] ;
29
27
const defer = ( fnname ) => {
30
- sampleRUM [ fnname ] = sampleRUM [ fnname ] || ( ( ...args ) => sampleRUM . defer . push ( { fnname, args } ) ) ;
28
+ sampleRUM [ fnname ] = sampleRUM [ fnname ]
29
+ || ( ( ...args ) => sampleRUM . defer . push ( { fnname, args } ) ) ;
31
30
} ;
32
31
sampleRUM . drain = sampleRUM . drain
33
32
|| ( ( dfnname , fn ) => {
@@ -49,85 +48,39 @@ function sampleRUM(checkpoint, data = {}) {
49
48
window . hlx = window . hlx || { } ;
50
49
if ( ! window . hlx . rum ) {
51
50
const usp = new URLSearchParams ( window . location . search ) ;
52
- const weight = usp . get ( 'rum' ) === 'on' ? 1 : 100 ; // with parameter, weight is 1. Defaults to 100.
53
- const id = Array . from ( { length : 75 } , ( _ , i ) => String . fromCharCode ( 48 + i ) )
54
- . filter ( ( a ) => / \d | [ A - Z ] / i. test ( a ) )
55
- . filter ( ( ) => Math . random ( ) * 75 > 70 )
56
- . join ( '' ) ;
51
+ const weight = ( usp . get ( 'rum' ) === 'on' ) ? 1 : 100 ; // with parameter, weight is 1. Defaults to 100.
52
+ const id = Math . random ( ) . toString ( 36 ) . slice ( - 4 ) ;
57
53
const random = Math . random ( ) ;
58
- const isSelected = random * weight < 1 ;
54
+ const isSelected = ( random * weight < 1 ) ;
59
55
const firstReadTime = window . performance ? window . performance . timeOrigin : Date . now ( ) ;
60
56
const urlSanitizers = {
61
57
full : ( ) => window . location . href ,
62
58
origin : ( ) => window . location . origin ,
63
59
path : ( ) => window . location . href . replace ( / \? .* $ / , '' ) ,
64
60
} ;
65
- // eslint-disable-next-line max-len
66
- const rumSessionStorage = sessionStorage . getItem ( SESSION_STORAGE_KEY )
67
- ? JSON . parse ( sessionStorage . getItem ( SESSION_STORAGE_KEY ) )
68
- : { } ;
69
- // eslint-disable-next-line max-len
70
- rumSessionStorage . pages = ( rumSessionStorage . pages ? rumSessionStorage . pages : 0 )
71
- + 1
72
- /* noise */ + ( Math . floor ( Math . random ( ) * 20 ) - 10 ) ;
73
- sessionStorage . setItem ( SESSION_STORAGE_KEY , JSON . stringify ( rumSessionStorage ) ) ;
74
61
// eslint-disable-next-line object-curly-newline, max-len
75
- window . hlx . rum = {
76
- weight,
77
- id,
78
- random,
79
- isSelected,
80
- firstReadTime,
81
- sampleRUM,
82
- sanitizeURL : urlSanitizers [ window . hlx . RUM_MASK_URL || 'path' ] ,
83
- rumSessionStorage,
84
- } ;
62
+ window . hlx . rum = { weight, id, random, isSelected, firstReadTime, sampleRUM, sanitizeURL : urlSanitizers [ window . hlx . RUM_MASK_URL || 'path' ] } ;
85
63
}
86
64
87
65
const { weight, id, firstReadTime } = window . hlx . rum ;
88
66
if ( window . hlx && window . hlx . rum && window . hlx . rum . isSelected ) {
89
- const knownProperties = [
90
- 'weight' ,
91
- 'id' ,
92
- 'referer' ,
93
- 'checkpoint' ,
94
- 't' ,
95
- 'source' ,
96
- 'target' ,
97
- 'cwv' ,
98
- 'CLS' ,
99
- 'FID' ,
100
- 'LCP' ,
101
- 'INP' ,
102
- 'TTFB' ,
103
- ] ;
67
+ const knownProperties = [ 'weight' , 'id' , 'referer' , 'checkpoint' , 't' , 'source' , 'target' , 'cwv' , 'CLS' , 'FID' , 'LCP' , 'INP' , 'TTFB' ] ;
104
68
const sendPing = ( pdata = data ) => {
105
69
// eslint-disable-next-line max-len
106
- const t = Math . round (
107
- window . performance ? window . performance . now ( ) : Date . now ( ) - firstReadTime ,
108
- ) ;
70
+ const t = Math . round ( window . performance ? window . performance . now ( ) : ( Date . now ( ) - firstReadTime ) ) ;
109
71
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
110
- const body = JSON . stringify (
111
- {
112
- weight, id, referer : window . hlx . rum . sanitizeURL ( ) , checkpoint, t, ...data ,
113
- } ,
114
- knownProperties ,
115
- ) ;
72
+ const body = JSON . stringify ( { weight, id, referer : window . hlx . rum . sanitizeURL ( ) , checkpoint, t, ...data } , knownProperties ) ;
116
73
const url = new URL ( `.rum/${ weight } ` , sampleRUM . baseURL ) . href ;
117
74
navigator . sendBeacon ( url , body ) ;
118
75
// eslint-disable-next-line no-console
119
76
console . debug ( `ping:${ checkpoint } ` , pdata ) ;
120
77
} ;
121
78
sampleRUM . cases = sampleRUM . cases || {
122
- load : ( ) => sampleRUM ( 'pagesviewed' , { source : window . hlx . rum . rumSessionStorage . pages } ) || true ,
123
79
cwv : ( ) => sampleRUM . cwv ( data ) || true ,
124
80
lazy : ( ) => {
125
81
// use classic script to avoid CORS issues
126
82
const script = document . createElement ( 'script' ) ;
127
- script . src = new URL (
128
- '.rum/@adobe/helix-rum-enhancer@^1/src/index.js' ,
129
- sampleRUM . baseURL ,
130
- ) . href ;
83
+ script . src = new URL ( '.rum/@adobe/helix-rum-enhancer@^1/src/index.js' , sampleRUM . baseURL ) . href ;
131
84
document . head . appendChild ( script ) ;
132
85
return true ;
133
86
} ,
0 commit comments