@cloudcare/browser-rum 2.1.5 → 2.2.0

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 (135) hide show
  1. package/README.md +1 -2
  2. package/bundle/dataflux-rum.js +1 -1
  3. package/cjs/boot/buildEnv.js +1 -1
  4. package/cjs/boot/rum.js +57 -48
  5. package/cjs/boot/rumPublicApi.js +169 -134
  6. package/cjs/domain/assembly.js +58 -44
  7. package/cjs/domain/configuration.js +67 -0
  8. package/cjs/domain/contexts/internalContext.js +45 -0
  9. package/cjs/domain/contexts/urlContexts.js +68 -0
  10. package/cjs/domain/contexts/viewContexts.js +43 -0
  11. package/cjs/domain/domMutationCollection.js +44 -31
  12. package/cjs/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +25 -25
  13. package/cjs/domain/performanceCollection.js +55 -24
  14. package/cjs/domain/requestCollection.js +117 -0
  15. package/cjs/domain/rumEventsCollection/actions/actionCollection.js +22 -4
  16. package/cjs/domain/rumEventsCollection/actions/clickChain.js +82 -0
  17. package/cjs/domain/rumEventsCollection/actions/computeFrustration.js +78 -0
  18. package/cjs/domain/rumEventsCollection/actions/getActionNameFromElement.js +60 -47
  19. package/cjs/domain/rumEventsCollection/actions/getSelectorsFromElement.js +163 -0
  20. package/cjs/domain/rumEventsCollection/actions/listenActionEvents.js +74 -0
  21. package/cjs/domain/rumEventsCollection/actions/trackClickActions.js +263 -0
  22. package/cjs/domain/rumEventsCollection/error/errorCollection.js +33 -53
  23. package/cjs/domain/rumEventsCollection/error/trackConsoleError.js +26 -0
  24. package/cjs/domain/rumEventsCollection/error/trackReportError.js +26 -0
  25. package/cjs/domain/rumEventsCollection/longTask/longTaskCollection.js +46 -17
  26. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +49 -9
  27. package/cjs/domain/rumEventsCollection/view/trackFirstHidden.js +7 -5
  28. package/cjs/domain/rumEventsCollection/view/trackInitialViewTimings.js +61 -24
  29. package/cjs/domain/rumEventsCollection/view/trackViewMetrics.js +91 -51
  30. package/cjs/domain/rumEventsCollection/view/trackViews.js +119 -86
  31. package/cjs/domain/rumEventsCollection/view/viewCollection.js +41 -31
  32. package/cjs/domain/rumSessionManager.js +95 -0
  33. package/cjs/domain/tracing/tracer.js +9 -10
  34. package/cjs/domain/trackEventCounts.js +9 -6
  35. package/cjs/domain/usr/index.js +30 -0
  36. package/cjs/domain/waitPageActivityEnd.js +163 -0
  37. package/cjs/transport/startRumBatch.js +38 -0
  38. package/esm/boot/buildEnv.js +1 -1
  39. package/esm/boot/rum.js +55 -48
  40. package/esm/boot/rumPublicApi.js +167 -135
  41. package/esm/domain/assembly.js +58 -44
  42. package/esm/domain/configuration.js +53 -0
  43. package/esm/domain/contexts/internalContext.js +38 -0
  44. package/esm/domain/contexts/urlContexts.js +58 -0
  45. package/esm/domain/contexts/viewContexts.js +32 -0
  46. package/esm/domain/domMutationCollection.js +43 -33
  47. package/esm/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +26 -24
  48. package/esm/domain/performanceCollection.js +56 -26
  49. package/esm/domain/requestCollection.js +102 -0
  50. package/esm/domain/rumEventsCollection/actions/actionCollection.js +23 -5
  51. package/esm/domain/rumEventsCollection/actions/clickChain.js +69 -0
  52. package/esm/domain/rumEventsCollection/actions/computeFrustration.js +64 -0
  53. package/esm/domain/rumEventsCollection/actions/getActionNameFromElement.js +60 -47
  54. package/esm/domain/rumEventsCollection/actions/getSelectorsFromElement.js +139 -0
  55. package/esm/domain/rumEventsCollection/actions/listenActionEvents.js +66 -0
  56. package/esm/domain/rumEventsCollection/actions/trackClickActions.js +244 -0
  57. package/esm/domain/rumEventsCollection/error/errorCollection.js +32 -54
  58. package/esm/domain/rumEventsCollection/error/trackConsoleError.js +18 -0
  59. package/esm/domain/rumEventsCollection/error/trackReportError.js +18 -0
  60. package/esm/domain/rumEventsCollection/longTask/longTaskCollection.js +30 -18
  61. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +31 -9
  62. package/esm/domain/rumEventsCollection/view/trackFirstHidden.js +8 -6
  63. package/esm/domain/rumEventsCollection/view/trackInitialViewTimings.js +43 -26
  64. package/esm/domain/rumEventsCollection/view/trackViewMetrics.js +75 -51
  65. package/esm/domain/rumEventsCollection/view/trackViews.js +120 -85
  66. package/esm/domain/rumEventsCollection/view/viewCollection.js +42 -32
  67. package/esm/domain/rumSessionManager.js +80 -0
  68. package/esm/domain/tracing/skywalkingTracer.js +1 -1
  69. package/esm/domain/tracing/tracer.js +8 -9
  70. package/esm/domain/trackEventCounts.js +9 -6
  71. package/esm/domain/usr/index.js +17 -0
  72. package/esm/domain/waitPageActivityEnd.js +147 -0
  73. package/esm/transport/startRumBatch.js +30 -0
  74. package/package.json +3 -3
  75. package/src/boot/rum.js +79 -59
  76. package/src/boot/rumPublicApi.js +165 -171
  77. package/src/domain/.DS_Store +0 -0
  78. package/src/domain/assembly.js +77 -54
  79. package/src/domain/configuration.js +69 -0
  80. package/src/domain/contexts/internalContext.js +39 -0
  81. package/src/domain/contexts/urlContexts.js +76 -0
  82. package/src/domain/contexts/viewContexts.js +37 -0
  83. package/src/domain/domMutationCollection.js +48 -32
  84. package/src/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +28 -28
  85. package/src/domain/performanceCollection.js +62 -45
  86. package/src/domain/requestCollection.js +103 -0
  87. package/src/domain/rumEventsCollection/actions/actionCollection.js +34 -11
  88. package/src/domain/rumEventsCollection/actions/clickChain.js +90 -0
  89. package/src/domain/rumEventsCollection/actions/computeFrustration.js +84 -0
  90. package/src/domain/rumEventsCollection/actions/getActionNameFromElement.js +73 -62
  91. package/src/domain/rumEventsCollection/actions/getSelectorsFromElement.js +157 -0
  92. package/src/domain/rumEventsCollection/actions/listenActionEvents.js +85 -0
  93. package/src/domain/rumEventsCollection/actions/trackClickActions.js +326 -0
  94. package/src/domain/rumEventsCollection/error/errorCollection.js +53 -69
  95. package/src/domain/rumEventsCollection/error/trackConsoleError.js +21 -0
  96. package/src/domain/rumEventsCollection/error/trackReportError.js +22 -0
  97. package/src/domain/rumEventsCollection/longTask/longTaskCollection.js +29 -18
  98. package/src/domain/rumEventsCollection/resource/resourceCollection.js +39 -23
  99. package/src/domain/rumEventsCollection/view/trackFirstHidden.js +9 -6
  100. package/src/domain/rumEventsCollection/view/trackInitialViewTimings.js +63 -42
  101. package/src/domain/rumEventsCollection/view/trackViewMetrics.js +80 -61
  102. package/src/domain/rumEventsCollection/view/trackViews.js +171 -134
  103. package/src/domain/rumEventsCollection/view/viewCollection.js +69 -49
  104. package/src/domain/rumSessionManager.js +90 -0
  105. package/src/domain/tracing/jaegerTracer.js +5 -5
  106. package/src/domain/tracing/skywalkingTracer.js +3 -3
  107. package/src/domain/tracing/tracer.js +50 -38
  108. package/src/domain/tracing/w3cTraceParentTracer.js +5 -5
  109. package/src/domain/tracing/zipkinMultiTracer.js +5 -5
  110. package/src/domain/tracing/zipkinSingleTracer.js +5 -5
  111. package/src/domain/trackEventCounts.js +10 -6
  112. package/src/domain/usr/index.js +16 -0
  113. package/src/domain/waitPageActivityEnd.js +192 -0
  114. package/src/transport/startRumBatch.js +48 -0
  115. package/cjs/domain/internalContext.js +0 -34
  116. package/cjs/domain/parentContexts.js +0 -143
  117. package/cjs/domain/requestCollections.js +0 -131
  118. package/cjs/domain/rumEventsCollection/actions/trackActions.js +0 -114
  119. package/cjs/domain/rumSession.js +0 -82
  120. package/cjs/domain/trackPageActivities.js +0 -166
  121. package/cjs/transport/batch.js +0 -81
  122. package/esm/domain/internalContext.js +0 -27
  123. package/esm/domain/parentContexts.js +0 -130
  124. package/esm/domain/requestCollections.js +0 -118
  125. package/esm/domain/rumEventsCollection/actions/trackActions.js +0 -103
  126. package/esm/domain/rumSession.js +0 -70
  127. package/esm/domain/trackPageActivities.js +0 -149
  128. package/esm/transport/batch.js +0 -74
  129. package/src/domain/internalContext.js +0 -28
  130. package/src/domain/parentContexts.js +0 -171
  131. package/src/domain/requestCollections.js +0 -118
  132. package/src/domain/rumEventsCollection/actions/trackActions.js +0 -113
  133. package/src/domain/rumSession.js +0 -92
  134. package/src/domain/trackPageActivities.js +0 -170
  135. package/src/transport/batch.js +0 -85
@@ -1,5 +1,8 @@
1
- import { addEventListeners, elapsed, extend, DOM_EVENT, LifeCycleEventType } from '@cloudcare/browser-core';
2
- import { trackFirstHidden } from './trackFirstHidden';
1
+ import { addEventListeners, elapsed, extend, DOM_EVENT, find, findLast, LifeCycleEventType, ONE_MINUTE } from '@cloudcare/browser-core';
2
+ import { trackFirstHidden } from './trackFirstHidden'; // Discard LCP and FCP timings above a certain delay to avoid incorrect data
3
+ // It happens in some cases like sleep mode or some browser implementations
4
+
5
+ export var TIMING_MAXIMUM_DELAY = 10 * ONE_MINUTE;
3
6
  export function trackInitialViewTimings(lifeCycle, callback) {
4
7
  var timings = {};
5
8
 
@@ -46,20 +49,22 @@ export function trackInitialViewTimings(lifeCycle, callback) {
46
49
  };
47
50
  }
48
51
  export function trackNavigationTimings(lifeCycle, callback) {
49
- var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, function (entry) {
50
- if (entry.entryType === 'navigation') {
51
- callback({
52
- fetchStart: entry.fetchStart,
53
- responseEnd: entry.responseEnd,
54
- domComplete: entry.domComplete,
55
- domContentLoaded: entry.domContentLoadedEventEnd,
56
- domInteractive: entry.domInteractive,
57
- loadEvent: entry.loadEventEnd,
58
- loadEventEnd: entry.loadEventEnd,
59
- loadEventStart: entry.loadEventStart,
60
- domContentLoadedEventEnd: entry.domContentLoadedEventEnd,
61
- domContentLoadedEventStart: entry.domContentLoadedEventStart
62
- });
52
+ var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, function (entries) {
53
+ for (var entry of entries) {
54
+ if (entry.entryType === 'navigation') {
55
+ callback({
56
+ fetchStart: entry.fetchStart,
57
+ responseEnd: entry.responseEnd,
58
+ domComplete: entry.domComplete,
59
+ domContentLoaded: entry.domContentLoadedEventEnd,
60
+ domInteractive: entry.domInteractive,
61
+ loadEvent: entry.loadEventEnd,
62
+ loadEventEnd: entry.loadEventEnd,
63
+ loadEventStart: entry.loadEventStart,
64
+ domContentLoadedEventEnd: entry.domContentLoadedEventEnd,
65
+ domContentLoadedEventStart: entry.domContentLoadedEventStart
66
+ });
67
+ }
63
68
  }
64
69
  });
65
70
  return {
@@ -68,9 +73,13 @@ export function trackNavigationTimings(lifeCycle, callback) {
68
73
  }
69
74
  export function trackFirstContentfulPaint(lifeCycle, callback) {
70
75
  var firstHidden = trackFirstHidden();
71
- var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, function (entry) {
72
- if (entry.entryType === 'paint' && entry.name === 'first-contentful-paint' && entry.startTime < firstHidden.timeStamp) {
73
- callback(entry.startTime);
76
+ var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, function (entries) {
77
+ var fcpEntry = find(entries, function (entry) {
78
+ return entry.entryType === 'paint' && entry.name === 'first-contentful-paint' && entry.startTime < firstHidden.timeStamp && entry.startTime < TIMING_MAXIMUM_DELAY;
79
+ });
80
+
81
+ if (fcpEntry) {
82
+ callback(fcpEntry.startTime);
74
83
  }
75
84
  });
76
85
  return {
@@ -99,9 +108,13 @@ export function trackLargestContentfulPaint(lifeCycle, emitter, callback) {
99
108
  });
100
109
 
101
110
  var stopEventListener = _addEventListeners.stop;
102
- var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, function (entry) {
103
- if (entry.entryType === 'largest-contentful-paint' && entry.startTime < firstInteractionTimestamp && entry.startTime < firstHidden.timeStamp) {
104
- callback(entry.startTime);
111
+ var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, function (entries) {
112
+ var lcpEntry = findLast(entries, function (entry) {
113
+ return entry.entryType === 'largest-contentful-paint' && entry.startTime < firstInteractionTimestamp && entry.startTime < firstHidden.timeStamp && entry.startTime < TIMING_MAXIMUM_DELAY;
114
+ });
115
+
116
+ if (lcpEntry) {
117
+ callback(lcpEntry.startTime);
105
118
  }
106
119
  });
107
120
  var unsubscribeLifeCycle = subscribe.unsubscribe;
@@ -123,14 +136,18 @@ export function trackLargestContentfulPaint(lifeCycle, emitter, callback) {
123
136
 
124
137
  export function trackFirstInputTimings(lifeCycle, callback) {
125
138
  var firstHidden = trackFirstHidden();
126
- var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, function (entry) {
127
- if (entry.entryType === 'first-input' && entry.startTime < firstHidden.timeStamp) {
128
- var firstInputDelay = elapsed(entry.startTime, entry.processingStart);
139
+ var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, function (entries) {
140
+ var firstInputEntry = find(entries, function (entry) {
141
+ return entry.entryType === 'first-input' && entry.startTime < firstHidden.timeStamp;
142
+ });
143
+
144
+ if (firstInputEntry) {
145
+ var firstInputDelay = elapsed(firstInputEntry.startTime, firstInputEntry.processingStart);
129
146
  callback({
130
147
  // Ensure firstInputDelay to be positive, see
131
148
  // https://bugs.chromium.org/p/chromium/issues/detail?id=1185815
132
149
  firstInputDelay: firstInputDelay >= 0 ? firstInputDelay : 0,
133
- firstInputTime: entry.startTime
150
+ firstInputTime: firstInputEntry.startTime
134
151
  });
135
152
  }
136
153
  });
@@ -1,15 +1,15 @@
1
- import { noop, elapsed, round, preferredNow, LifeCycleEventType } from '@cloudcare/browser-core';
1
+ import { noop, elapsed, round, ONE_SECOND, LifeCycleEventType, ViewLoadingType } from '@cloudcare/browser-core';
2
2
  import { supportPerformanceTimingEvent } from '../../performanceCollection';
3
3
  import { trackEventCounts } from '../../trackEventCounts';
4
- import { waitIdlePageActivity } from '../../trackPageActivities';
5
- import { ViewLoadingType } from './trackViews';
6
- export function trackViewMetrics(lifeCycle, scheduleViewUpdate, loadingType) {
4
+ import { waitPageActivityEnd } from '../../waitPageActivityEnd';
5
+ export function trackViewMetrics(lifeCycle, domMutationObservable, configuration, scheduleViewUpdate, loadingType, viewStart) {
7
6
  var viewMetrics = {
8
7
  eventCounts: {
9
8
  errorCount: 0,
10
9
  longTaskCount: 0,
11
10
  resourceCount: 0,
12
- userActionCount: 0
11
+ actionCount: 0,
12
+ frustrationCount: 0
13
13
  }
14
14
  };
15
15
 
@@ -20,24 +20,20 @@ export function trackViewMetrics(lifeCycle, scheduleViewUpdate, loadingType) {
20
20
 
21
21
  var stopEventCountsTracking = _trackEventCounts.stop;
22
22
 
23
- var _trackLoadingTime = trackLoadingTime(loadingType, function (newLoadingTime) {
23
+ var _trackLoadingTime = trackLoadingTime(lifeCycle, domMutationObservable, configuration, loadingType, viewStart, function (newLoadingTime) {
24
24
  viewMetrics.loadingTime = newLoadingTime;
25
25
  scheduleViewUpdate();
26
26
  });
27
27
 
28
- var setActivityLoadingTime = _trackLoadingTime.setActivityLoadingTime;
29
28
  var setLoadEvent = _trackLoadingTime.setLoadEvent;
30
-
31
- var _trackActivityLoadingTime = trackActivityLoadingTime(lifeCycle, setActivityLoadingTime);
32
-
33
- var stopActivityLoadingTimeTracking = _trackActivityLoadingTime.stop;
29
+ var stopLoadingTimeTracking = _trackLoadingTime.stop;
34
30
  var stopCLSTracking;
35
31
 
36
32
  if (isLayoutShiftSupported()) {
37
33
  viewMetrics.cumulativeLayoutShift = 0;
38
34
 
39
- var _trackLayoutShift = trackLayoutShift(lifeCycle, function (layoutShift) {
40
- viewMetrics.cumulativeLayoutShift = round(viewMetrics.cumulativeLayoutShift + layoutShift, 4);
35
+ var _trackLayoutShift = trackCumulativeLayoutShift(lifeCycle, function (cumulativeLayoutShift) {
36
+ viewMetrics.cumulativeLayoutShift = cumulativeLayoutShift;
41
37
  scheduleViewUpdate();
42
38
  });
43
39
 
@@ -49,7 +45,7 @@ export function trackViewMetrics(lifeCycle, scheduleViewUpdate, loadingType) {
49
45
  return {
50
46
  stop: function stop() {
51
47
  stopEventCountsTracking();
52
- stopActivityLoadingTimeTracking();
48
+ stopLoadingTimeTracking();
53
49
  stopCLSTracking();
54
50
  },
55
51
  setLoadEvent: setLoadEvent,
@@ -57,7 +53,7 @@ export function trackViewMetrics(lifeCycle, scheduleViewUpdate, loadingType) {
57
53
  };
58
54
  }
59
55
 
60
- function trackLoadingTime(loadType, callback) {
56
+ function trackLoadingTime(lifeCycle, domMutationObservable, configuration, loadType, viewStart, callback) {
61
57
  var isWaitingForLoadEvent = loadType === ViewLoadingType.INITIAL_LOAD;
62
58
  var isWaitingForActivityLoadingTime = true;
63
59
  var loadingTimeCandidates = [];
@@ -68,6 +64,19 @@ function trackLoadingTime(loadType, callback) {
68
64
  }
69
65
  }
70
66
 
67
+ var _waitPageActivityEnd = waitPageActivityEnd(lifeCycle, domMutationObservable, configuration, function (event) {
68
+ if (isWaitingForActivityLoadingTime) {
69
+ isWaitingForActivityLoadingTime = false;
70
+
71
+ if (event.hadActivity) {
72
+ loadingTimeCandidates.push(elapsed(viewStart.timeStamp, event.end));
73
+ }
74
+
75
+ invokeCallbackIfAllCandidatesAreReceived();
76
+ }
77
+ });
78
+
79
+ var stop = _waitPageActivityEnd.stop;
71
80
  return {
72
81
  setLoadEvent: function setLoadEvent(loadEvent) {
73
82
  if (isWaitingForLoadEvent) {
@@ -76,55 +85,70 @@ function trackLoadingTime(loadType, callback) {
76
85
  invokeCallbackIfAllCandidatesAreReceived();
77
86
  }
78
87
  },
79
- setActivityLoadingTime: function setActivityLoadingTime(activityLoadingTime) {
80
- if (isWaitingForActivityLoadingTime) {
81
- isWaitingForActivityLoadingTime = false;
82
-
83
- if (activityLoadingTime !== undefined) {
84
- loadingTimeCandidates.push(activityLoadingTime);
85
- }
86
-
87
- invokeCallbackIfAllCandidatesAreReceived();
88
- }
89
- }
90
- };
91
- }
92
-
93
- function trackActivityLoadingTime(lifeCycle, callback) {
94
- var startTime = preferredNow();
95
-
96
- var _waitIdlePageActivity = waitIdlePageActivity(lifeCycle, function (params) {
97
- if (params.hadActivity) {
98
- callback(elapsed(startTime, params.endTime));
99
- } else {
100
- callback(undefined);
101
- }
102
- });
103
-
104
- var stopWaitIdlePageActivity = _waitIdlePageActivity.stop;
105
- return {
106
- stop: stopWaitIdlePageActivity
88
+ stop: stop
107
89
  };
108
90
  }
109
91
  /**
110
- * Track layout shifts (LS) occurring during the Views. This yields multiple values that can be
111
- * added up to compute the cumulated layout shift (CLS).
92
+ * Track the cumulative layout shifts (CLS).
93
+ * Layout shifts are grouped into session windows.
94
+ * The minimum gap between session windows is 1 second.
95
+ * The maximum duration of a session window is 5 second.
96
+ * The session window layout shift value is the sum of layout shifts inside it.
97
+ * The CLS value is the max of session windows values.
98
+ *
99
+ * This yields a new value whenever the CLS value is updated (a higher session window value is computed).
112
100
  *
113
101
  * See isLayoutShiftSupported to check for browser support.
114
102
  *
115
- * Documentation: https://web.dev/cls/
103
+ * Documentation:
104
+ * https://web.dev/cls/
105
+ * https://web.dev/evolving-cls/
116
106
  * Reference implementation: https://github.com/GoogleChrome/web-vitals/blob/master/src/getCLS.ts
117
107
  */
118
108
 
119
109
 
120
- function trackLayoutShift(lifeCycle, callback) {
121
- var subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, function (entry) {
122
- if (entry.entryType === 'layout-shift' && !entry.hadRecentInput) {
123
- callback(entry.value);
110
+ function trackCumulativeLayoutShift(lifeCycle, callback) {
111
+ var maxClsValue = 0;
112
+ var window = slidingSessionWindow();
113
+
114
+ var _subscribe = lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, function (entries) {
115
+ for (var entry of entries) {
116
+ if (entry.entryType === 'layout-shift' && !entry.hadRecentInput) {
117
+ window.update(entry);
118
+
119
+ if (window.value() > maxClsValue) {
120
+ maxClsValue = window.value();
121
+ callback(round(maxClsValue, 4));
122
+ }
123
+ }
124
124
  }
125
125
  });
126
+
127
+ var stop = _subscribe.unsubscribe;
128
+ return {
129
+ stop: stop
130
+ };
131
+ }
132
+
133
+ function slidingSessionWindow() {
134
+ var _value = 0;
135
+ var startTime;
136
+ var endTime;
126
137
  return {
127
- stop: subscribe.unsubscribe
138
+ update: function update(entry) {
139
+ var shouldCreateNewWindow = startTime === undefined || entry.startTime - endTime >= ONE_SECOND || entry.startTime - startTime >= 5 * ONE_SECOND;
140
+
141
+ if (shouldCreateNewWindow) {
142
+ startTime = endTime = entry.startTime;
143
+ _value = entry.value;
144
+ } else {
145
+ _value += entry.value;
146
+ endTime = entry.startTime;
147
+ }
148
+ },
149
+ value: function value() {
150
+ return _value;
151
+ }
128
152
  };
129
153
  }
130
154
  /**
@@ -1,75 +1,81 @@
1
- import { elapsed, extend, UUID, ONE_MINUTE, throttle, clocksNow, clocksOrigin, timeStampNow, looksLikeRelativeTime, LifeCycleEventType } from '@cloudcare/browser-core';
1
+ import { shallowClone, assign, elapsed, UUID, ONE_MINUTE, throttle, clocksNow, clocksOrigin, timeStampNow, looksLikeRelativeTime, ViewLoadingType, LifeCycleEventType } from '@cloudcare/browser-core';
2
2
  import { trackInitialViewTimings } from './trackInitialViewTimings';
3
- import { trackLocationChanges, areDifferentLocation } from './trackLocationChanges';
4
3
  import { trackViewMetrics } from './trackViewMetrics';
5
4
  export var THROTTLE_VIEW_UPDATE_PERIOD = 3000;
6
5
  export var SESSION_KEEP_ALIVE_INTERVAL = 5 * ONE_MINUTE;
7
- export var ViewLoadingType = {
8
- INITIAL_LOAD: 'initial_load',
9
- ROUTE_CHANGE: 'route_change'
10
- };
11
- export function trackViews(location, lifeCycle) {
12
- var isRecording = false; // eslint-disable-next-line prefer-const
13
-
14
- var _trackInitialView = trackInitialView();
6
+ export function trackViews(location, lifeCycle, domMutationObservable, configuration, locationChangeObservable, areViewsTrackedAutomatically, initialViewOptions) {
7
+ var _trackInitialView = trackInitialView(initialViewOptions);
15
8
 
16
9
  var stopInitialViewTracking = _trackInitialView.stop;
17
- var currentView = _trackInitialView.initialView;
18
-
19
- var _trackLocationChanges = trackLocationChanges(function () {
20
- if (areDifferentLocation(currentView.getLocation(), location)) {
21
- // Renew view on location changes
22
- currentView.end();
23
- currentView.triggerUpdate();
24
- currentView = trackViewChange();
25
- return;
26
- }
27
-
28
- currentView.updateLocation(location);
29
- currentView.triggerUpdate();
30
- });
10
+ var initialView = _trackInitialView.initialView;
11
+ var currentView = initialView;
31
12
 
32
- var stopLocationChangesTracking = _trackLocationChanges.stop; // Renew view on session renewal
13
+ var _startViewLifeCycle = startViewLifeCycle();
33
14
 
34
- lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, function () {
35
- // do not trigger view update to avoid wrong data
36
- currentView.end();
37
- currentView = trackViewChange();
38
- }); // End the current view on page unload
15
+ var stopViewLifeCycle = _startViewLifeCycle.stop;
16
+ var locationChangeSubscription;
39
17
 
40
- lifeCycle.subscribe(LifeCycleEventType.BEFORE_UNLOAD, function () {
41
- currentView.end();
42
- currentView.triggerUpdate();
43
- });
44
- lifeCycle.subscribe(LifeCycleEventType.RECORD_STARTED, function () {
45
- isRecording = true;
46
- currentView.updateHasReplay(true);
47
- });
48
- lifeCycle.subscribe(LifeCycleEventType.RECORD_STOPPED, function () {
49
- isRecording = false;
50
- }); // Session keep alive
51
-
52
- var keepAliveInterval = window.setInterval(function () {
53
- currentView.triggerUpdate();
54
- }, SESSION_KEEP_ALIVE_INTERVAL);
18
+ if (areViewsTrackedAutomatically) {
19
+ locationChangeSubscription = renewViewOnLocationChange(locationChangeObservable);
20
+ }
55
21
 
56
- function trackInitialView() {
57
- var initialView = newView(lifeCycle, location, isRecording, ViewLoadingType.INITIAL_LOAD, document.referrer, clocksOrigin());
22
+ function trackInitialView(options) {
23
+ var initialView = newView(lifeCycle, domMutationObservable, configuration, location, ViewLoadingType.INITIAL_LOAD, clocksOrigin(), options);
58
24
 
59
25
  var _trackInitialViewTimings = trackInitialViewTimings(lifeCycle, function (timings) {
60
26
  initialView.updateTimings(timings);
61
27
  initialView.scheduleUpdate();
62
28
  });
63
29
 
64
- var stop = _trackInitialViewTimings.stop;
65
30
  return {
66
31
  initialView: initialView,
67
- stop: stop
32
+ stop: _trackInitialViewTimings.stop
33
+ };
34
+ }
35
+
36
+ function trackViewChange(startClocks, viewOptions) {
37
+ return newView(lifeCycle, domMutationObservable, configuration, location, ViewLoadingType.ROUTE_CHANGE, startClocks, viewOptions);
38
+ }
39
+
40
+ function startViewLifeCycle() {
41
+ lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, function () {
42
+ // do not trigger view update to avoid wrong data
43
+ currentView.end(); // Renew view on session renewal
44
+
45
+ currentView = trackViewChange(undefined, {
46
+ name: currentView.name,
47
+ service: currentView.service,
48
+ version: currentView.version
49
+ });
50
+ }); // End the current view on page unload
51
+
52
+ lifeCycle.subscribe(LifeCycleEventType.BEFORE_UNLOAD, function () {
53
+ currentView.end();
54
+ currentView.triggerUpdate();
55
+ }); // Session keep alive
56
+
57
+ var keepAliveInterval = window.setInterval(function () {
58
+ currentView.triggerUpdate();
59
+ }, SESSION_KEEP_ALIVE_INTERVAL);
60
+ return {
61
+ stop: function stop() {
62
+ clearInterval(keepAliveInterval);
63
+ }
68
64
  };
69
65
  }
70
66
 
71
- function trackViewChange() {
72
- return newView(lifeCycle, location, isRecording, ViewLoadingType.ROUTE_CHANGE, currentView.url);
67
+ function renewViewOnLocationChange(locationChangeObservable) {
68
+ return locationChangeObservable.subscribe(function (params) {
69
+ var oldLocation = params.oldLocation;
70
+ var newLocation = params.newLocation;
71
+
72
+ if (areDifferentLocation(oldLocation, newLocation)) {
73
+ currentView.end();
74
+ currentView.triggerUpdate();
75
+ currentView = trackViewChange();
76
+ return;
77
+ }
78
+ });
73
79
  }
74
80
 
75
81
  return {
@@ -79,80 +85,102 @@ export function trackViews(location, lifeCycle) {
79
85
  }
80
86
 
81
87
  currentView.addTiming(name, time);
88
+ currentView.scheduleUpdate();
89
+ },
90
+ startView: function startView(options, startClocks) {
91
+ currentView.end(startClocks);
82
92
  currentView.triggerUpdate();
93
+ currentView = trackViewChange(startClocks, options);
83
94
  },
84
95
  stop: function stop() {
96
+ if (locationChangeSubscription) {
97
+ locationChangeSubscription.unsubscribe();
98
+ }
99
+
85
100
  stopInitialViewTracking();
86
- stopLocationChangesTracking();
101
+ stopViewLifeCycle();
87
102
  currentView.end();
88
- clearInterval(keepAliveInterval);
89
103
  }
90
104
  };
91
105
  }
92
106
 
93
- function newView(lifeCycle, initialLocation, initialHasReplay, loadingType, referrer, startClocks, name) {
107
+ function newView(lifeCycle, domMutationObservable, configuration, initialLocation, loadingType, startClocks, viewOptions) {
108
+ // Setup initial values
94
109
  if (typeof startClocks === 'undefined') {
95
110
  startClocks = clocksNow();
96
- } // Setup initial values
97
-
111
+ }
98
112
 
99
113
  var id = UUID();
100
114
  var timings = {};
101
115
  var customTimings = {};
102
116
  var documentVersion = 0;
103
117
  var endClocks;
104
- var location = extend({}, initialLocation);
105
- var hasReplay = initialHasReplay;
118
+ var location = shallowClone(initialLocation);
119
+ var name;
120
+ var service;
121
+ var version;
122
+
123
+ if (viewOptions) {
124
+ name = viewOptions.name;
125
+ service = viewOptions.service;
126
+ version = viewOptions.version;
127
+ }
128
+
106
129
  lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, {
107
130
  id: id,
131
+ name: name,
108
132
  startClocks: startClocks,
109
- location: location,
110
- referrer: referrer
111
- });
112
- var scheduleViewUpdate = throttle(triggerViewUpdate, THROTTLE_VIEW_UPDATE_PERIOD, {
113
- leading: false
133
+ service: service,
134
+ version: version
114
135
  }); // Update the view every time the measures are changing
115
136
 
137
+ var scheduleViewUpdate = throttle(triggerViewUpdate, THROTTLE_VIEW_UPDATE_PERIOD, {
138
+ leading: false
139
+ });
116
140
  var cancelScheduleViewUpdate = scheduleViewUpdate.cancel;
117
141
 
118
- var _trackViewMetrics = trackViewMetrics(lifeCycle, scheduleViewUpdate, loadingType);
142
+ var _trackViewMetrics = trackViewMetrics(lifeCycle, domMutationObservable, configuration, scheduleViewUpdate, loadingType, startClocks);
119
143
 
120
144
  var setLoadEvent = _trackViewMetrics.setLoadEvent;
121
- var viewMetrics = _trackViewMetrics.viewMetrics;
122
- var stopViewMetricsTracking = _trackViewMetrics.stop; // Initial view update
145
+ var stopViewMetricsTracking = _trackViewMetrics.stop;
146
+ var viewMetrics = _trackViewMetrics.viewMetrics; // Initial view update
123
147
 
124
148
  triggerViewUpdate();
125
149
 
126
150
  function triggerViewUpdate() {
127
151
  documentVersion += 1;
128
152
  var currentEnd = endClocks === undefined ? timeStampNow() : endClocks.timeStamp;
129
- lifeCycle.notify(LifeCycleEventType.VIEW_UPDATED, extend({}, viewMetrics, {
153
+ lifeCycle.notify(LifeCycleEventType.VIEW_UPDATED, assign({
130
154
  customTimings: customTimings,
131
155
  documentVersion: documentVersion,
132
156
  id: id,
133
157
  name: name,
158
+ service: service,
159
+ version: version,
134
160
  loadingType: loadingType,
135
161
  location: location,
136
- hasReplay: hasReplay,
137
- referrer: referrer,
138
162
  startClocks: startClocks,
139
163
  timings: timings,
140
164
  duration: elapsed(startClocks.timeStamp, currentEnd),
141
165
  isActive: endClocks === undefined
142
- }));
166
+ }, viewMetrics));
143
167
  }
144
168
 
145
169
  return {
170
+ name: name,
171
+ service: service,
172
+ version: version,
146
173
  scheduleUpdate: scheduleViewUpdate,
147
- end: function end() {
148
- endClocks = clocksNow();
149
- stopViewMetricsTracking();
174
+ end: function end(clocks) {
175
+ if (typeof clocks === 'undefined') {
176
+ clocks = clocksNow();
177
+ }
178
+
179
+ endClocks = clocks;
150
180
  lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {
151
- endClocks: endClocks
181
+ endClocks
152
182
  });
153
- },
154
- getLocation: function getLocation() {
155
- return location;
183
+ stopViewMetricsTracking();
156
184
  },
157
185
  triggerUpdate: function triggerUpdate() {
158
186
  // cancel any pending view updates execution
@@ -169,14 +197,7 @@ function newView(lifeCycle, initialLocation, initialHasReplay, loadingType, refe
169
197
  addTiming: function addTiming(name, time) {
170
198
  var relativeTime = looksLikeRelativeTime(time) ? time : elapsed(startClocks.timeStamp, time);
171
199
  customTimings[sanitizeTiming(name)] = relativeTime;
172
- },
173
- updateLocation: function updateLocation(newLocation) {
174
- location = extend({}, newLocation);
175
- },
176
- updateHasReplay: function updateHasReplay(newHasReplay) {
177
- hasReplay = newHasReplay;
178
- },
179
- url: location.href
200
+ }
180
201
  };
181
202
  }
182
203
  /**
@@ -192,4 +213,18 @@ function sanitizeTiming(name) {
192
213
  }
193
214
 
194
215
  return sanitized;
216
+ }
217
+
218
+ function areDifferentLocation(currentLocation, otherLocation) {
219
+ return currentLocation.pathname !== otherLocation.pathname || !isHashAnAnchor(otherLocation.hash) && getPathFromHash(otherLocation.hash) !== getPathFromHash(currentLocation.hash);
220
+ }
221
+
222
+ function isHashAnAnchor(hash) {
223
+ var correspondingId = hash.substr(1);
224
+ return !!document.getElementById(correspondingId);
225
+ }
226
+
227
+ function getPathFromHash(hash) {
228
+ var index = hash.indexOf('?');
229
+ return index < 0 ? hash : hash.slice(0, index);
195
230
  }