1
1
/*
2
- * Copyright 2023 Adobe. All rights reserved.
2
+ * Copyright 2024 Adobe. All rights reserved.
3
3
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
4
* you may not use this file except in compliance with the License. You may obtain a copy
5
5
* of the License at http://www.apache.org/licenses/LICENSE-2.0
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
28
sampleRUM . defer = sampleRUM . defer || [ ] ;
26
29
const defer = ( fnname ) => {
27
30
sampleRUM [ fnname ] = sampleRUM [ fnname ] || ( ( ...args ) => sampleRUM . defer . push ( { fnname, args } ) ) ;
@@ -53,12 +56,21 @@ function sampleRUM(checkpoint, data = {}) {
53
56
. join ( '' ) ;
54
57
const random = Math . random ( ) ;
55
58
const isSelected = random * weight < 1 ;
56
- const firstReadTime = Date . now ( ) ;
59
+ const firstReadTime = window . performance ? window . performance . timeOrigin : Date . now ( ) ;
57
60
const urlSanitizers = {
58
61
full : ( ) => window . location . href ,
59
62
origin : ( ) => window . location . origin ,
60
63
path : ( ) => window . location . href . replace ( / \? .* $ / , '' ) ,
61
64
} ;
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 ) ) ;
62
74
// eslint-disable-next-line object-curly-newline, max-len
63
75
window . hlx . rum = {
64
76
weight,
@@ -68,8 +80,10 @@ function sampleRUM(checkpoint, data = {}) {
68
80
firstReadTime,
69
81
sampleRUM,
70
82
sanitizeURL : urlSanitizers [ window . hlx . RUM_MASK_URL || 'path' ] ,
83
+ rumSessionStorage,
71
84
} ;
72
85
}
86
+
73
87
const { weight, id, firstReadTime } = window . hlx . rum ;
74
88
if ( window . hlx && window . hlx . rum && window . hlx . rum . isSelected ) {
75
89
const knownProperties = [
@@ -85,32 +99,35 @@ function sampleRUM(checkpoint, data = {}) {
85
99
'FID' ,
86
100
'LCP' ,
87
101
'INP' ,
102
+ 'TTFB' ,
88
103
] ;
89
104
const sendPing = ( pdata = data ) => {
105
+ // eslint-disable-next-line max-len
106
+ const t = Math . round (
107
+ window . performance ? window . performance . now ( ) : Date . now ( ) - firstReadTime ,
108
+ ) ;
90
109
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
91
110
const body = JSON . stringify (
92
111
{
93
- weight,
94
- id,
95
- referer : window . hlx . rum . sanitizeURL ( ) ,
96
- checkpoint,
97
- t : Date . now ( ) - firstReadTime ,
98
- ...data ,
112
+ weight, id, referer : window . hlx . rum . sanitizeURL ( ) , checkpoint, t, ...data ,
99
113
} ,
100
114
knownProperties ,
101
115
) ;
102
- const url = `https://rum.hlx.page/.rum/${ weight } ` ;
103
- // eslint-disable-next-line no-unused-expressions
116
+ const url = new URL ( `.rum/${ weight } ` , sampleRUM . baseURL ) . href ;
104
117
navigator . sendBeacon ( url , body ) ;
105
118
// eslint-disable-next-line no-console
106
119
console . debug ( `ping:${ checkpoint } ` , pdata ) ;
107
120
} ;
108
121
sampleRUM . cases = sampleRUM . cases || {
122
+ load : ( ) => sampleRUM ( 'pagesviewed' , { source : window . hlx . rum . rumSessionStorage . pages } ) || true ,
109
123
cwv : ( ) => sampleRUM . cwv ( data ) || true ,
110
124
lazy : ( ) => {
111
125
// use classic script to avoid CORS issues
112
126
const script = document . createElement ( 'script' ) ;
113
- script . src = 'https://rum.hlx.page/.rum/@adobe/helix-rum-enhancer@^1/src/index.js' ;
127
+ script . src = new URL (
128
+ '.rum/@adobe/helix-rum-enhancer@^1/src/index.js' ,
129
+ sampleRUM . baseURL ,
130
+ ) . href ;
114
131
document . head . appendChild ( script ) ;
115
132
return true ;
116
133
} ,
0 commit comments