@cloudcare/browser-rum 3.0.6 → 3.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/bundle/dataflux-rum.js +1 -1
  2. package/cjs/boot/buildEnv.js +1 -1
  3. package/cjs/boot/recorderApi.js +1 -1
  4. package/cjs/boot/rum.js +16 -7
  5. package/cjs/boot/rumPublicApi.js +27 -55
  6. package/cjs/domain/assembly.js +2 -2
  7. package/cjs/domain/contexts/commonContext.js +14 -0
  8. package/cjs/domain/performanceCollection.js +3 -3
  9. package/cjs/domain/replay/record/serialize.js +7 -5
  10. package/cjs/domain/replay/segmentCollection/segmentCollection.js +2 -2
  11. package/cjs/domain/replay/segmentCollection/startDeflateWorker.js +2 -2
  12. package/cjs/domain/rumEventsCollection/actions/clickChain.js +3 -3
  13. package/cjs/domain/rumEventsCollection/actions/getActionNameFromElement.js +0 -1
  14. package/cjs/domain/rumEventsCollection/view/trackFirstHidden.js +4 -4
  15. package/cjs/domain/rumEventsCollection/view/trackInitialViewTimings.js +42 -18
  16. package/cjs/domain/rumEventsCollection/view/trackViewEventCounts.js +1 -1
  17. package/cjs/domain/rumEventsCollection/view/trackViews.js +45 -67
  18. package/cjs/domain/rumEventsCollection/view/viewCollection.js +2 -1
  19. package/cjs/domain/rumSessionManager.js +3 -1
  20. package/cjs/domain/tracing/getDocumentTraceId.js +1 -1
  21. package/cjs/domain/waitPageActivityEnd.js +9 -9
  22. package/cjs/transport/startRumBatch.js +20 -6
  23. package/esm/boot/buildEnv.js +1 -1
  24. package/esm/boot/recorderApi.js +1 -1
  25. package/esm/boot/rum.js +15 -7
  26. package/esm/boot/rumPublicApi.js +25 -54
  27. package/esm/domain/assembly.js +2 -2
  28. package/esm/domain/contexts/commonContext.js +7 -0
  29. package/esm/domain/performanceCollection.js +2 -2
  30. package/esm/domain/replay/record/serialize.js +7 -5
  31. package/esm/domain/replay/segmentCollection/segmentCollection.js +1 -1
  32. package/esm/domain/replay/segmentCollection/startDeflateWorker.js +3 -3
  33. package/esm/domain/rumEventsCollection/actions/clickChain.js +1 -1
  34. package/esm/domain/rumEventsCollection/actions/getActionNameFromElement.js +0 -1
  35. package/esm/domain/rumEventsCollection/view/trackFirstHidden.js +4 -4
  36. package/esm/domain/rumEventsCollection/view/trackInitialViewTimings.js +39 -16
  37. package/esm/domain/rumEventsCollection/view/trackViewEventCounts.js +1 -1
  38. package/esm/domain/rumEventsCollection/view/trackViews.js +46 -68
  39. package/esm/domain/rumEventsCollection/view/viewCollection.js +2 -1
  40. package/esm/domain/rumSessionManager.js +3 -1
  41. package/esm/domain/tracing/getDocumentTraceId.js +1 -1
  42. package/esm/domain/waitPageActivityEnd.js +3 -3
  43. package/esm/transport/startRumBatch.js +21 -7
  44. package/package.json +31 -31
  45. package/src/boot/recorderApi.js +1 -0
  46. package/src/boot/rum.js +29 -9
  47. package/src/boot/rumPublicApi.js +37 -51
  48. package/src/domain/assembly.js +2 -2
  49. package/src/domain/contexts/commonContext.js +11 -0
  50. package/src/domain/performanceCollection.js +4 -2
  51. package/src/domain/replay/record/serialize.js +6 -5
  52. package/src/domain/replay/segmentCollection/segmentCollection.js +3 -1
  53. package/src/domain/replay/segmentCollection/startDeflateWorker.js +8 -3
  54. package/src/domain/rumEventsCollection/actions/clickChain.js +6 -1
  55. package/src/domain/rumEventsCollection/actions/getActionNameFromElement.js +0 -1
  56. package/src/domain/rumEventsCollection/view/trackFirstHidden.js +4 -4
  57. package/src/domain/rumEventsCollection/view/trackInitialViewTimings.js +53 -17
  58. package/src/domain/rumEventsCollection/view/trackViewEventCounts.js +1 -1
  59. package/src/domain/rumEventsCollection/view/trackViews.js +69 -77
  60. package/src/domain/rumEventsCollection/view/viewCollection.js +2 -1
  61. package/src/domain/rumSessionManager.js +3 -1
  62. package/src/domain/tracing/getDocumentTraceId.js +1 -1
  63. package/src/domain/waitPageActivityEnd.js +8 -5
  64. package/src/transport/startRumBatch.js +35 -12
@@ -1,23 +1,39 @@
1
1
  import {
2
2
  makePublicApi,
3
- assign,
4
3
  areCookiesAuthorized,
5
4
  BoundedBuffer,
6
5
  display,
7
6
  buildCookieOptions,
8
7
  createContextManager,
8
+ CustomerDataType,
9
9
  clocksNow,
10
10
  timeStampNow,
11
11
  ActionType,
12
12
  deepClone,
13
- createHandlingStack
13
+ createHandlingStack,
14
+ sanitizeUser,
15
+ checkUser,
16
+ noop
14
17
  } from '@cloudcare/browser-core'
15
18
  import { validateAndBuildRumConfiguration } from '../domain/configuration'
19
+ import { buildCommonContext } from '../domain/contexts/commonContext'
16
20
  export function makeRumPublicApi(startRumImpl, recorderApi) {
17
21
  var isAlreadyInitialized = false
18
22
 
19
- var globalContextManager = createContextManager()
20
- var userContextManager = createContextManager()
23
+ var globalContextManager = createContextManager(
24
+ CustomerDataType.GlobalContext
25
+ )
26
+ var userContextManager = createContextManager(CustomerDataType.User)
27
+
28
+ var getInternalContextStrategy = function () {
29
+ return undefined
30
+ }
31
+ var getInitConfigurationStrategy = function () {
32
+ return undefined
33
+ }
34
+ var stopSessionStrategy = function () {
35
+ return noop()
36
+ }
21
37
  var bufferApiCalls = new BoundedBuffer()
22
38
  var addTimingStrategy = function (name, time) {
23
39
  if (typeof time === 'undefined') {
@@ -37,10 +53,11 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
37
53
  }
38
54
  var addActionStrategy = function (action, commonContext) {
39
55
  if (typeof commonContext == 'undefined') {
40
- commonContext = {
41
- context: globalContextManager.getContext(),
42
- user: userContextManager.getContext()
43
- }
56
+ commonContext = buildCommonContext(
57
+ globalContextManager,
58
+ userContextManager,
59
+ recorderApi
60
+ )
44
61
  }
45
62
  bufferApiCalls.add(function () {
46
63
  return addActionStrategy(action, commonContext)
@@ -48,23 +65,17 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
48
65
  }
49
66
  var addErrorStrategy = function (providedError, commonContext) {
50
67
  if (typeof commonContext == 'undefined') {
51
- commonContext = {
52
- context: globalContextManager.getContext(),
53
- user: userContextManager.getContext()
54
- }
68
+ commonContext = buildCommonContext(
69
+ globalContextManager,
70
+ userContextManager,
71
+ recorderApi
72
+ )
55
73
  }
56
74
  bufferApiCalls.add(function () {
57
75
  return addErrorStrategy(providedError, commonContext)
58
76
  })
59
77
  }
60
78
 
61
- var getInternalContextStrategy = function () {
62
- return undefined
63
- }
64
- var getInitConfigurationStrategy = function () {
65
- return undefined
66
- }
67
-
68
79
  function initRum(initConfiguration) {
69
80
  if (!canHandleSession(initConfiguration)) {
70
81
  return
@@ -102,14 +113,9 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
102
113
  function doStartRum(configuration, initialViewOptions) {
103
114
  var startRumResults = startRumImpl(
104
115
  configuration,
105
- function () {
106
- return {
107
- user: userContextManager.getContext(),
108
- context: globalContextManager.getContext(),
109
- hasReplay: recorderApi.isRecording() ? true : undefined
110
- }
111
- },
112
116
  recorderApi,
117
+ globalContextManager,
118
+ userContextManager,
113
119
  initialViewOptions
114
120
  )
115
121
  startViewStrategy = startRumResults.startView
@@ -117,6 +123,7 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
117
123
  addErrorStrategy = startRumResults.addError
118
124
  addTimingStrategy = startRumResults.addTiming
119
125
  getInternalContextStrategy = startRumResults.getInternalContext
126
+ stopSessionStrategy = startRumResults.stopSession
120
127
  bufferApiCalls.drain()
121
128
  recorderApi.onRumStart(
122
129
  startRumResults.lifeCycle,
@@ -167,14 +174,6 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
167
174
  type: ActionType.CUSTOM
168
175
  })
169
176
  },
170
-
171
- /**
172
- * @deprecated use addAction instead
173
- */
174
- // addUserAction: function (name, context) {
175
- // rumPublicApi.addAction(name, context)
176
- // },
177
-
178
177
  addError: function (error, context) {
179
178
  var handlingStack = createHandlingStack()
180
179
  addErrorStrategy({
@@ -188,9 +187,7 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
188
187
  addTimingStrategy(name, time)
189
188
  },
190
189
  setUser: function (newUser) {
191
- if (typeof newUser !== 'object' || !newUser) {
192
- display.error('Unsupported user:', newUser)
193
- } else {
190
+ if (checkUser(newUser)) {
194
191
  userContextManager.setContext(sanitizeUser(newUser))
195
192
  }
196
193
  },
@@ -207,24 +204,13 @@ export function makeRumPublicApi(startRumImpl, recorderApi) {
207
204
  removeUser: userContextManager.clearContext,
208
205
  clearUser: userContextManager.clearContext,
209
206
  startView: startView,
207
+ stopSession: function () {
208
+ stopSessionStrategy()
209
+ },
210
210
  startSessionReplayRecording: recorderApi.start,
211
211
  stopSessionReplayRecording: recorderApi.stop
212
212
  })
213
213
  return rumPublicApi
214
-
215
- function sanitizeUser(newUser) {
216
- var shallowClonedUser = assign(newUser, {})
217
- if ('id' in shallowClonedUser) {
218
- shallowClonedUser.id = String(shallowClonedUser.id)
219
- }
220
- if ('name' in shallowClonedUser) {
221
- shallowClonedUser.name = String(shallowClonedUser.name)
222
- }
223
- if ('email' in shallowClonedUser) {
224
- shallowClonedUser.email = String(shallowClonedUser.email)
225
- }
226
- return shallowClonedUser
227
- }
228
214
  function canHandleSession(initConfiguration) {
229
215
  if (!areCookiesAuthorized(buildCookieOptions(initConfiguration))) {
230
216
  display.warn('Cookies are not authorized, we will not send any data.')
@@ -40,7 +40,7 @@ export function startRumAssembly(
40
40
  viewContexts,
41
41
  urlContexts,
42
42
  actionContexts,
43
- getCommonContext,
43
+ buildCommonContext,
44
44
  reportError
45
45
  ) {
46
46
  var eventRateLimiters = {}
@@ -69,7 +69,7 @@ export function startRumAssembly(
69
69
  )
70
70
  if (session && viewContext && urlContext) {
71
71
  var actionId = actionContexts.findActionId(startTime)
72
- var commonContext = savedCommonContext || getCommonContext()
72
+ var commonContext = savedCommonContext || buildCommonContext()
73
73
  var rumContext = {
74
74
  _dd: {
75
75
  sdkName: configuration.sdkName,
@@ -0,0 +1,11 @@
1
+ export function buildCommonContext(
2
+ globalContextManager,
3
+ userContextManager,
4
+ recorderApi
5
+ ) {
6
+ return {
7
+ context: globalContextManager.getContext(),
8
+ user: userContextManager.getContext(),
9
+ hasReplay: recorderApi.isRecording() ? true : undefined
10
+ }
11
+ }
@@ -10,7 +10,9 @@ import {
10
10
  relativeNow,
11
11
  DOM_EVENT,
12
12
  LifeCycleEventType,
13
- runOnReadyState
13
+ runOnReadyState,
14
+ setTimeout,
15
+ addEventListener
14
16
  } from '@cloudcare/browser-core'
15
17
  import {
16
18
  FAKE_INITIAL_DOCUMENT,
@@ -73,7 +75,7 @@ export function startPerformanceCollection(lifeCycle, configuration) {
73
75
  mainObserver.observe({ entryTypes: mainEntries })
74
76
  if (supportPerformanceObject() && 'addEventListener' in performance) {
75
77
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1559377
76
- performance.addEventListener('resourcetimingbufferfull', function () {
78
+ addEventListener(performance, 'resourcetimingbufferfull', function () {
77
79
  performance.clearResourceTimings()
78
80
  })
79
81
  }
@@ -369,12 +369,13 @@ function isSVGElement(el) {
369
369
  return el.tagName === 'svg' || el instanceof SVGElement
370
370
  }
371
371
  function transformAttribute(doc, tagName, name, value) {
372
+ if (!value) return value
372
373
  if (
373
374
  name === 'src' ||
374
- (name === 'href' && value && !(tagName === 'use' && value[0] === '#'))
375
+ (name === 'href' && !(tagName === 'use' && value[0] === '#'))
375
376
  ) {
376
377
  return absoluteToDoc(doc, value)
377
- } else if (name === 'xlink:href' && value && value[0] !== '#') {
378
+ } else if (name === 'xlink:href' && value[0] !== '#') {
378
379
  return absoluteToDoc(doc, value)
379
380
  } else if (
380
381
  name === 'background' &&
@@ -382,11 +383,11 @@ function transformAttribute(doc, tagName, name, value) {
382
383
  (tagName === 'table' || tagName === 'td' || tagName === 'th')
383
384
  ) {
384
385
  return absoluteToDoc(doc, value)
385
- } else if (name === 'srcset' && value) {
386
+ } else if (name === 'srcset') {
386
387
  return getAbsoluteSrcsetString(doc, value)
387
- } else if (name === 'style' && value) {
388
+ } else if (name === 'style') {
388
389
  return switchToAbsoluteUrl(value, getHref())
389
- } else if (tagName === 'object' && name === 'data' && value) {
390
+ } else if (tagName === 'object' && name === 'data') {
390
391
  return absoluteToDoc(doc, value)
391
392
  } else {
392
393
  return value
@@ -1,7 +1,9 @@
1
1
  import {
2
2
  ONE_SECOND,
3
3
  LifeCycleEventType,
4
- isPageExitReason
4
+ isPageExitReason,
5
+ setTimeout,
6
+ clearTimeout
5
7
  } from '@cloudcare/browser-core'
6
8
  import { buildReplayPayload } from './buildReplayPayload'
7
9
  import { Segment } from './segment'
@@ -1,4 +1,9 @@
1
- import { display, includes, each } from '@cloudcare/browser-core'
1
+ import {
2
+ display,
3
+ includes,
4
+ each,
5
+ addEventListener
6
+ } from '@cloudcare/browser-core'
2
7
  import { createDeflateWorker } from './deflateWorker'
3
8
 
4
9
  /**
@@ -55,8 +60,8 @@ export function doStartDeflateWorker(createDeflateWorkerImpl) {
55
60
  }
56
61
  try {
57
62
  var worker = createDeflateWorkerImpl()
58
- worker.addEventListener('error', onError)
59
- worker.addEventListener('message', function (event) {
63
+ addEventListener(worker, 'error', onError)
64
+ addEventListener(worker, 'message', function (event) {
60
65
  var data = event.data
61
66
  if (data.type === 'errored') {
62
67
  onError(data.error)
@@ -1,4 +1,9 @@
1
- import { ONE_SECOND, every } from '@cloudcare/browser-core'
1
+ import {
2
+ ONE_SECOND,
3
+ every,
4
+ setTimeout,
5
+ clearTimeout
6
+ } from '@cloudcare/browser-core'
2
7
 
3
8
  export var MAX_DURATION_BETWEEN_CLICKS = ONE_SECOND
4
9
  export var MAX_DISTANCE_BETWEEN_CLICKS = 100
@@ -261,7 +261,6 @@ function getTextualContent(element, userProgrammaticAttribute) {
261
261
  * from a parent element innerText.
262
262
  *
263
263
  * [1]: https://web.archive.org/web/20210602165716/http://perfectionkills.com/the-poor-misunderstood-innerText/#diff-with-textContent
264
- * [2]: https://github.com/DataDog/browser-sdk/issues/1084
265
264
  */
266
265
  function supportsInnerTextScriptAndStyleRemoval() {
267
266
  return !isIE()
@@ -2,9 +2,9 @@ import { DOM_EVENT, addEventListeners } from '@cloudcare/browser-core'
2
2
  var trackFirstHiddenSingleton
3
3
  var stopListeners
4
4
 
5
- export function trackFirstHidden(emitter) {
6
- if (typeof emitter === 'undefined') {
7
- emitter = window
5
+ export function trackFirstHidden(eventTarget) {
6
+ if (typeof eventTarget === 'undefined') {
7
+ eventTarget = window
8
8
  }
9
9
  if (!trackFirstHiddenSingleton) {
10
10
  if (document.visibilityState === 'hidden') {
@@ -14,7 +14,7 @@ export function trackFirstHidden(emitter) {
14
14
  timeStamp: Infinity
15
15
  }
16
16
  var listeners = addEventListeners(
17
- emitter,
17
+ eventTarget,
18
18
  [DOM_EVENT.PAGE_HIDE, DOM_EVENT.VISIBILITY_CHANGE],
19
19
  function (event) {
20
20
  if (
@@ -6,35 +6,55 @@ import {
6
6
  find,
7
7
  findLast,
8
8
  LifeCycleEventType,
9
- ONE_MINUTE
9
+ ONE_MINUTE,
10
+ setTimeout,
11
+ relativeNow
10
12
  } from '@cloudcare/browser-core'
11
13
  import { trackFirstHidden } from './trackFirstHidden'
12
14
  // Discard LCP and FCP timings above a certain delay to avoid incorrect data
13
15
  // It happens in some cases like sleep mode or some browser implementations
14
16
  export var TIMING_MAXIMUM_DELAY = 10 * ONE_MINUTE
15
- export function trackInitialViewTimings(lifeCycle, callback) {
17
+ /**
18
+ * The initial view can finish quickly, before some metrics can be produced (ex: before the page load
19
+ * event, or the first input). Also, we don't want to trigger a view update indefinitely, to avoid
20
+ * updates on views that ended a long time ago. Keep watching for metrics after the view ends for a
21
+ * limited amount of time.
22
+ */
23
+ export const KEEP_TRACKING_TIMINGS_AFTER_VIEW_DELAY = 5 * ONE_MINUTE
24
+ export function trackInitialViewTimings(
25
+ lifeCycle,
26
+ setLoadEvent,
27
+ scheduleViewUpdate
28
+ ) {
16
29
  var timings = {}
17
30
  function setTimings(newTimings) {
18
31
  timings = extend(timings, newTimings)
19
- callback(timings)
32
+ scheduleViewUpdate()
20
33
  }
21
- var _trackNavigationTimings = trackNavigationTimings(lifeCycle, setTimings)
34
+ var _trackNavigationTimings = trackNavigationTimings(
35
+ lifeCycle,
36
+ function (newTimings) {
37
+ setLoadEvent(newTimings.loadEvent)
38
+ setTimings(newTimings)
39
+ }
40
+ )
22
41
  var stopNavigationTracking = _trackNavigationTimings.stop
23
- var _trackFirstContentfulPaint = trackFirstContentfulPaint(
42
+
43
+ var _trackFirstContentfulPaintTiming = trackFirstContentfulPaintTiming(
24
44
  lifeCycle,
25
45
  function (firstContentfulPaint) {
26
46
  setTimings({ firstContentfulPaint: firstContentfulPaint })
27
47
  }
28
48
  )
29
- var stopFCPTracking = _trackFirstContentfulPaint.stop
30
- var _trackLargestContentfulPaint = trackLargestContentfulPaint(
49
+ var stopFCPTracking = _trackFirstContentfulPaintTiming.stop
50
+ var _trackLargestContentfulPaintTiming = trackLargestContentfulPaintTiming(
31
51
  lifeCycle,
32
52
  window,
33
53
  function (largestContentfulPaint) {
34
54
  setTimings({ largestContentfulPaint: largestContentfulPaint })
35
55
  }
36
56
  )
37
- var stopLCPTracking = _trackLargestContentfulPaint.stop
57
+ var stopLCPTracking = _trackLargestContentfulPaintTiming.stop
38
58
  var _trackFirstInputTimings = trackFirstInputTimings(
39
59
  lifeCycle,
40
60
  function (firttime) {
@@ -45,13 +65,17 @@ export function trackInitialViewTimings(lifeCycle, callback) {
45
65
  }
46
66
  )
47
67
  var stopFIDTracking = _trackFirstInputTimings.stop
48
-
68
+ function stop() {
69
+ stopNavigationTracking()
70
+ stopFCPTracking()
71
+ stopLCPTracking()
72
+ stopFIDTracking()
73
+ }
49
74
  return {
50
- stop: function () {
51
- stopNavigationTracking()
52
- stopFCPTracking()
53
- stopLCPTracking()
54
- stopFIDTracking()
75
+ stop: stop,
76
+ timings: timings,
77
+ scheduleStop: function () {
78
+ setTimeout(stop, KEEP_TRACKING_TIMINGS_AFTER_VIEW_DELAY)
55
79
  }
56
80
  }
57
81
  }
@@ -73,7 +97,15 @@ export function trackNavigationTimings(lifeCycle, callback) {
73
97
  loadEventEnd: entry.loadEventEnd,
74
98
  loadEventStart: entry.loadEventStart,
75
99
  domContentLoadedEventEnd: entry.domContentLoadedEventEnd,
76
- domContentLoadedEventStart: entry.domContentLoadedEventStart
100
+ domContentLoadedEventStart: entry.domContentLoadedEventStart,
101
+ // In some cases the value reported is negative or is larger
102
+ // than the current page time. Ignore these cases:
103
+ // https://github.com/GoogleChrome/web-vitals/issues/137
104
+ // https://github.com/GoogleChrome/web-vitals/issues/162
105
+ firstByte:
106
+ entry.responseStart >= 0 && entry.responseStart <= relativeNow()
107
+ ? entry.responseStart
108
+ : undefined
77
109
  })
78
110
  }
79
111
  }
@@ -83,7 +115,7 @@ export function trackNavigationTimings(lifeCycle, callback) {
83
115
  return { stop: subscribe.unsubscribe }
84
116
  }
85
117
 
86
- export function trackFirstContentfulPaint(lifeCycle, callback) {
118
+ export function trackFirstContentfulPaintTiming(lifeCycle, callback) {
87
119
  var firstHidden = trackFirstHidden()
88
120
  var subscribe = lifeCycle.subscribe(
89
121
  LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED,
@@ -110,7 +142,11 @@ export function trackFirstContentfulPaint(lifeCycle, callback) {
110
142
  * Documentation: https://web.dev/lcp/
111
143
  * Reference implementation: https://github.com/GoogleChrome/web-vitals/blob/master/src/getLCP.ts
112
144
  */
113
- export function trackLargestContentfulPaint(lifeCycle, emitter, callback) {
145
+ export function trackLargestContentfulPaintTiming(
146
+ lifeCycle,
147
+ emitter,
148
+ callback
149
+ ) {
114
150
  var firstHidden = trackFirstHidden()
115
151
 
116
152
  // Ignore entries that come after the first user interaction. According to the documentation, the
@@ -1,4 +1,4 @@
1
- import { ONE_MINUTE } from '@cloudcare/browser-core'
1
+ import { ONE_MINUTE, setTimeout } from '@cloudcare/browser-core'
2
2
  import { trackEventCounts } from '../../trackEventCounts'
3
3
 
4
4
  // Some events are not being counted as they transcend views. To reduce the occurrence;