@@ -18,9 +18,13 @@ function sampleRUM(checkpoint, data) {
18
18
window . hlx = window . hlx || { } ;
19
19
sampleRUM . enhance = ( ) => { } ;
20
20
if ( ! window . hlx . rum ) {
21
- const weight = new URLSearchParams ( window . location . search ) . get ( 'rum' ) === 'on' ? 1 : 100 ;
21
+ const param = new URLSearchParams ( window . location . search ) . get ( 'rum' ) ;
22
+ const weight = ( window . SAMPLE_PAGEVIEWS_AT_RATE === 'high' && 10 )
23
+ || ( window . SAMPLE_PAGEVIEWS_AT_RATE === 'low' && 1000 )
24
+ || ( param === 'on' && 1 )
25
+ || 100 ;
22
26
const id = Math . random ( ) . toString ( 36 ) . slice ( - 4 ) ;
23
- const isSelected = Math . random ( ) * weight < 1 ;
27
+ const isSelected = param !== 'off' && Math . random ( ) * weight < 1 ;
24
28
// eslint-disable-next-line object-curly-newline, max-len
25
29
window . hlx . rum = {
26
30
weight,
@@ -32,23 +36,39 @@ function sampleRUM(checkpoint, data) {
32
36
collector : ( ...args ) => window . hlx . rum . queue . push ( args ) ,
33
37
} ;
34
38
if ( isSelected ) {
35
- [ 'error' , 'unhandledrejection' ] . forEach ( ( event ) => {
36
- window . addEventListener ( event , ( { reason, error } ) => {
37
- const errData = { source : 'undefined error' } ;
38
- try {
39
- errData . target = ( reason || error ) . toString ( ) ;
40
- errData . source = ( reason || error ) . stack
41
- . split ( '\n' )
42
- . filter ( ( line ) => line . match ( / h t t p s ? : \/ \/ / ) )
43
- . shift ( )
44
- . replace ( / a t ( [ ^ ] + ) \( ( .+ ) \) / , '$1@$2' )
45
- . trim ( ) ;
46
- } catch ( err ) {
47
- /* error structure was not as expected */
48
- }
49
- sampleRUM ( 'error' , errData ) ;
50
- } ) ;
39
+ const dataFromErrorObj = ( error ) => {
40
+ const errData = { source : 'undefined error' } ;
41
+ try {
42
+ errData . target = error . toString ( ) ;
43
+ errData . source = error . stack
44
+ . split ( '\n' )
45
+ . filter ( ( line ) => line . match ( / h t t p s ? : \/ \/ / ) )
46
+ . shift ( )
47
+ . replace ( / a t ( [ ^ ] + ) \( ( .+ ) \) / , '$1@$2' )
48
+ . replace ( / a t / , '@' )
49
+ . trim ( ) ;
50
+ } catch ( err ) {
51
+ /* error structure was not as expected */
52
+ }
53
+ return errData ;
54
+ } ;
55
+
56
+ window . addEventListener ( 'error' , ( { error } ) => {
57
+ const errData = dataFromErrorObj ( error ) ;
58
+ sampleRUM ( 'error' , errData ) ;
51
59
} ) ;
60
+
61
+ window . addEventListener ( 'unhandledrejection' , ( { reason } ) => {
62
+ let errData = {
63
+ source : 'Unhandled Rejection' ,
64
+ target : reason || 'Unknown' ,
65
+ } ;
66
+ if ( reason instanceof Error ) {
67
+ errData = dataFromErrorObj ( reason ) ;
68
+ }
69
+ sampleRUM ( 'error' , errData ) ;
70
+ } ) ;
71
+
52
72
sampleRUM . baseURL = sampleRUM . baseURL || new URL ( window . RUM_BASE || '/' , new URL ( 'https://rum.hlx.page' ) ) ;
53
73
sampleRUM . collectBaseURL = sampleRUM . collectBaseURL || sampleRUM . baseURL ;
54
74
sampleRUM . sendPing = ( ck , time , pingData = { } ) => {
@@ -61,7 +81,13 @@ function sampleRUM(checkpoint, data) {
61
81
t : time ,
62
82
...pingData ,
63
83
} ) ;
64
- const { href : url , origin } = new URL ( `.rum/${ weight } ` , sampleRUM . collectBaseURL ) ;
84
+ const urlParams = window . RUM_PARAMS
85
+ ? `?${ new URLSearchParams ( window . RUM_PARAMS ) . toString ( ) } `
86
+ : '' ;
87
+ const { href : url , origin } = new URL (
88
+ `.rum/${ weight } ${ urlParams } ` ,
89
+ sampleRUM . collectBaseURL ,
90
+ ) ;
65
91
const body = origin === window . location . origin
66
92
? new Blob ( [ rumData ] , { type : 'application/json' } )
67
93
: rumData ;
@@ -72,9 +98,16 @@ function sampleRUM(checkpoint, data) {
72
98
sampleRUM . sendPing ( 'top' , timeShift ( ) ) ;
73
99
74
100
sampleRUM . enhance = ( ) => {
101
+ // only enhance once
102
+ if ( document . querySelector ( 'script[src*="rum-enhancer"]' ) ) return ;
103
+ const { enhancerVersion, enhancerHash } = sampleRUM . enhancerContext || { } ;
75
104
const script = document . createElement ( 'script' ) ;
105
+ if ( enhancerHash ) {
106
+ script . integrity = enhancerHash ;
107
+ script . setAttribute ( 'crossorigin' , 'anonymous' ) ;
108
+ }
76
109
script . src = new URL (
77
- ' .rum/@adobe/helix-rum-enhancer@^2 /src/index.js' ,
110
+ ` .rum/@adobe/helix-rum-enhancer@${ enhancerVersion || '^2' } /src/index.js` ,
78
111
sampleRUM . baseURL ,
79
112
) . href ;
80
113
document . head . appendChild ( script ) ;
@@ -89,7 +122,7 @@ function sampleRUM(checkpoint, data) {
89
122
}
90
123
document . dispatchEvent ( new CustomEvent ( 'rum' , { detail : { checkpoint, data } } ) ) ;
91
124
} catch ( error ) {
92
- // something went wrong
125
+ // something went awry
93
126
}
94
127
}
95
128
@@ -665,6 +698,9 @@ async function loadSections(element) {
665
698
for ( let i = 0 ; i < sections . length ; i += 1 ) {
666
699
// eslint-disable-next-line no-await-in-loop
667
700
await loadSection ( sections [ i ] ) ;
701
+ if ( i === 0 && sampleRUM . enhance ) {
702
+ sampleRUM . enhance ( ) ;
703
+ }
668
704
}
669
705
}
670
706
0 commit comments