@cloudcare/browser-rum 2.1.6 → 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 (134) 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 +8 -9
  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 +7 -8
  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/assembly.js +77 -54
  78. package/src/domain/configuration.js +69 -0
  79. package/src/domain/contexts/internalContext.js +39 -0
  80. package/src/domain/contexts/urlContexts.js +76 -0
  81. package/src/domain/contexts/viewContexts.js +37 -0
  82. package/src/domain/domMutationCollection.js +48 -32
  83. package/src/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +28 -28
  84. package/src/domain/performanceCollection.js +62 -45
  85. package/src/domain/requestCollection.js +103 -0
  86. package/src/domain/rumEventsCollection/actions/actionCollection.js +34 -11
  87. package/src/domain/rumEventsCollection/actions/clickChain.js +90 -0
  88. package/src/domain/rumEventsCollection/actions/computeFrustration.js +84 -0
  89. package/src/domain/rumEventsCollection/actions/getActionNameFromElement.js +73 -62
  90. package/src/domain/rumEventsCollection/actions/getSelectorsFromElement.js +157 -0
  91. package/src/domain/rumEventsCollection/actions/listenActionEvents.js +85 -0
  92. package/src/domain/rumEventsCollection/actions/trackClickActions.js +326 -0
  93. package/src/domain/rumEventsCollection/error/errorCollection.js +53 -69
  94. package/src/domain/rumEventsCollection/error/trackConsoleError.js +21 -0
  95. package/src/domain/rumEventsCollection/error/trackReportError.js +22 -0
  96. package/src/domain/rumEventsCollection/longTask/longTaskCollection.js +29 -18
  97. package/src/domain/rumEventsCollection/resource/resourceCollection.js +39 -23
  98. package/src/domain/rumEventsCollection/view/trackFirstHidden.js +9 -6
  99. package/src/domain/rumEventsCollection/view/trackInitialViewTimings.js +63 -42
  100. package/src/domain/rumEventsCollection/view/trackViewMetrics.js +80 -61
  101. package/src/domain/rumEventsCollection/view/trackViews.js +171 -134
  102. package/src/domain/rumEventsCollection/view/viewCollection.js +69 -49
  103. package/src/domain/rumSessionManager.js +90 -0
  104. package/src/domain/tracing/jaegerTracer.js +5 -5
  105. package/src/domain/tracing/skywalkingTracer.js +3 -3
  106. package/src/domain/tracing/tracer.js +50 -38
  107. package/src/domain/tracing/w3cTraceParentTracer.js +5 -5
  108. package/src/domain/tracing/zipkinMultiTracer.js +5 -5
  109. package/src/domain/tracing/zipkinSingleTracer.js +5 -5
  110. package/src/domain/trackEventCounts.js +10 -6
  111. package/src/domain/usr/index.js +16 -0
  112. package/src/domain/waitPageActivityEnd.js +192 -0
  113. package/src/transport/startRumBatch.js +48 -0
  114. package/cjs/domain/internalContext.js +0 -34
  115. package/cjs/domain/parentContexts.js +0 -143
  116. package/cjs/domain/requestCollections.js +0 -131
  117. package/cjs/domain/rumEventsCollection/actions/trackActions.js +0 -114
  118. package/cjs/domain/rumSession.js +0 -82
  119. package/cjs/domain/trackPageActivities.js +0 -166
  120. package/cjs/transport/batch.js +0 -81
  121. package/esm/domain/internalContext.js +0 -27
  122. package/esm/domain/parentContexts.js +0 -130
  123. package/esm/domain/requestCollections.js +0 -118
  124. package/esm/domain/rumEventsCollection/actions/trackActions.js +0 -103
  125. package/esm/domain/rumSession.js +0 -70
  126. package/esm/domain/trackPageActivities.js +0 -149
  127. package/esm/transport/batch.js +0 -74
  128. package/src/domain/internalContext.js +0 -28
  129. package/src/domain/parentContexts.js +0 -171
  130. package/src/domain/requestCollections.js +0 -118
  131. package/src/domain/rumEventsCollection/actions/trackActions.js +0 -113
  132. package/src/domain/rumSession.js +0 -92
  133. package/src/domain/trackPageActivities.js +0 -170
  134. package/src/transport/batch.js +0 -85
@@ -6,6 +6,9 @@ import {
6
6
  includes,
7
7
  each,
8
8
  extend,
9
+ filter,
10
+ dateNow,
11
+ relativeNow,
9
12
  DOM_EVENT,
10
13
  LifeCycleEventType
11
14
  } from '@cloudcare/browser-core'
@@ -26,28 +29,41 @@ export function supportPerformanceTimingEvent(entryType) {
26
29
  }
27
30
  export function startPerformanceCollection(lifeCycle, configuration) {
28
31
  retrieveInitialDocumentResourceTiming(function (timing) {
29
- handleRumPerformanceEntry(lifeCycle, configuration, timing)
32
+ handleRumPerformanceEntries(lifeCycle, configuration, [timing])
30
33
  })
31
34
 
32
35
  if (supportPerformanceObject()) {
33
- handlePerformanceEntries(lifeCycle, configuration, performance.getEntries())
36
+ var performanceEntries = performance.getEntries()
37
+ // Because the performance entry list can be quite large
38
+ // delay the computation to prevent the SDK from blocking the main thread on init
39
+ setTimeout(function() {
40
+ handleRumPerformanceEntries(lifeCycle, configuration, performanceEntries)
41
+ })
34
42
  }
35
43
  if (window.PerformanceObserver) {
36
- var observer = new PerformanceObserver(function (entries) {
37
- handlePerformanceEntries(lifeCycle, configuration, entries.getEntries())
38
- })
39
- var entryTypes = [
40
- 'resource',
41
- 'navigation',
42
- 'longtask',
43
- 'paint',
44
- 'largest-contentful-paint',
45
- 'first-input',
46
- 'layout-shift'
47
- ]
48
-
49
- observer.observe({ entryTypes: entryTypes })
50
-
44
+ var handlePerformanceEntryList = function(entries) {
45
+ handleRumPerformanceEntries(lifeCycle, configuration, entries.getEntries())
46
+ }
47
+ var mainEntries = ['resource', 'navigation', 'longtask', 'paint']
48
+ var experimentalEntries = ['largest-contentful-paint', 'first-input', 'layout-shift']
49
+ try {
50
+ // Experimental entries are not retrieved by performance.getEntries()
51
+ // use a single PerformanceObserver with buffered flag by type
52
+ // to get values that could happen before SDK init
53
+ each(experimentalEntries, function(type) {
54
+ var observer = new PerformanceObserver(handlePerformanceEntryList)
55
+ observer.observe({ type:type, buffered: true })
56
+ })
57
+
58
+ } catch (e) {
59
+ // Some old browser versions (ex: chrome 67) don't support the PerformanceObserver type and buffered options
60
+ // In these cases, fallback to PerformanceObserver with entryTypes
61
+ each(experimentalEntries, function(type) {
62
+ mainEntries.push(type)
63
+ })
64
+ }
65
+ var mainObserver = new PerformanceObserver(handlePerformanceEntryList)
66
+ mainObserver.observe({ entryTypes: mainEntries })
51
67
  if (supportPerformanceObject() && 'addEventListener' in performance) {
52
68
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1559377
53
69
  performance.addEventListener('resourcetimingbufferfull', function () {
@@ -57,12 +73,12 @@ export function startPerformanceCollection(lifeCycle, configuration) {
57
73
  }
58
74
  if (!supportPerformanceTimingEvent('navigation')) {
59
75
  retrieveNavigationTiming(function (timing) {
60
- handleRumPerformanceEntry(lifeCycle, configuration, timing)
76
+ handleRumPerformanceEntries(lifeCycle, configuration, [timing])
61
77
  })
62
78
  }
63
79
  if (!supportPerformanceTimingEvent('first-input')) {
64
- retrieveFirstInputTiming((timing) => {
65
- handleRumPerformanceEntry(lifeCycle, configuration, timing)
80
+ retrieveFirstInputTiming(function(timing) {
81
+ return handleRumPerformanceEntries(lifeCycle, configuration, [timing])
66
82
  })
67
83
  }
68
84
  }
@@ -117,7 +133,7 @@ function retrieveNavigationTiming(callback) {
117
133
  * https://github.com/GoogleChrome/web-vitals/blob/master/src/lib/polyfills/firstInputPolyfill.ts
118
134
  */
119
135
  function retrieveFirstInputTiming(callback) {
120
- var startTimeStamp = Date.now()
136
+ var startTimeStamp = dateNow()
121
137
  var timingSent = false
122
138
 
123
139
  var listeners = addEventListeners(
@@ -140,7 +156,7 @@ function retrieveFirstInputTiming(callback) {
140
156
  // (e.g. performance.now()).
141
157
  var timing = {
142
158
  entryType: 'first-input',
143
- processingStart: performance.now(),
159
+ processingStart: relativeNow(),
144
160
  startTime: evt.timeStamp
145
161
  }
146
162
 
@@ -183,7 +199,7 @@ function retrieveFirstInputTiming(callback) {
183
199
  // - https://github.com/GoogleChromeLabs/first-input-delay/issues/6
184
200
  // - https://github.com/GoogleChromeLabs/first-input-delay/issues/7
185
201
  var delay = timing.processingStart - timing.startTime
186
- if (delay >= 0 && delay < Date.now() - startTimeStamp) {
202
+ if (delay >= 0 && delay < dateNow() - startTimeStamp) {
187
203
  callback(timing)
188
204
  }
189
205
  }
@@ -216,32 +232,28 @@ function computeRelativePerformanceTiming() {
216
232
  return result
217
233
  }
218
234
 
219
- function handlePerformanceEntries(lifeCycle, configuration, entries) {
220
- each(entries, function (entry) {
221
- if (
222
- entry.entryType === 'resource' ||
223
- entry.entryType === 'navigation' ||
224
- entry.entryType === 'paint' ||
225
- entry.entryType === 'longtask' ||
226
- entry.entryType === 'largest-contentful-paint' ||
227
- entry.entryType === 'first-input' ||
228
- entry.entryType === 'layout-shift'
229
- ) {
230
- handleRumPerformanceEntry(lifeCycle, configuration, entry)
231
- }
232
- })
233
- }
235
+ function handleRumPerformanceEntries(lifeCycle, configuration, entries) {
236
+ var rumPerformanceEntries = filter(entries,
237
+ function(entry){
238
+ return entry.entryType === 'resource' ||
239
+ entry.entryType === 'navigation' ||
240
+ entry.entryType === 'paint' ||
241
+ entry.entryType === 'longtask' ||
242
+ entry.entryType === 'largest-contentful-paint' ||
243
+ entry.entryType === 'first-input' ||
244
+ entry.entryType === 'layout-shift'
245
+ }
246
+ )
234
247
 
235
- function handleRumPerformanceEntry(lifeCycle, configuration, entry) {
236
- if (
237
- isIncompleteNavigation(entry) ||
238
- isForbiddenResource(configuration, entry)
239
- ) {
240
- return
248
+ var rumAllowedPerformanceEntries = filter(rumPerformanceEntries, function(entry) { return !isIncompleteNavigation(entry) && !isForbiddenResource(configuration, entry)})
249
+
250
+ if (rumAllowedPerformanceEntries.length) {
251
+ lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, rumAllowedPerformanceEntries)
241
252
  }
242
- lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, entry)
243
253
  }
244
254
 
255
+
256
+
245
257
  function isIncompleteNavigation(entry) {
246
258
  return entry.entryType === 'navigation' && entry.loadEventEnd <= 0
247
259
  }
@@ -252,3 +264,8 @@ function isForbiddenResource(configuration, entry) {
252
264
  !isAllowedRequestUrl(configuration, entry.name)
253
265
  )
254
266
  }
267
+ export function supportPerformanceEntry() {
268
+ // Safari 10 doesn't support PerformanceEntry
269
+ return typeof PerformanceEntry === 'function'
270
+ }
271
+
@@ -0,0 +1,103 @@
1
+
2
+ import { RequestType, initFetchObservable, initXhrObservable, LifeCycleEventType } from '@cloudcare/browser-core'
3
+ import { isAllowedRequestUrl } from './rumEventsCollection/resource/resourceUtils'
4
+ import { startTracer } from './tracing/tracer'
5
+
6
+ let nextRequestIndex = 1
7
+
8
+ export function startRequestCollection(
9
+ lifeCycle,
10
+ configuration,
11
+ sessionManager
12
+ ) {
13
+ var tracer = startTracer(configuration, sessionManager)
14
+ trackXhr(lifeCycle, configuration, tracer)
15
+ trackFetch(lifeCycle, configuration, tracer)
16
+ }
17
+
18
+ export function trackXhr(lifeCycle, configuration, tracer) {
19
+ var subscription = initXhrObservable().subscribe(function(rawContext) {
20
+ var context = rawContext
21
+ if (!isAllowedRequestUrl(configuration, context.url)) {
22
+ return
23
+ }
24
+
25
+ switch (context.state) {
26
+ case 'start':
27
+ tracer.traceXhr(context, context.xhr)
28
+ context.requestIndex = getNextRequestIndex()
29
+
30
+ lifeCycle.notify(LifeCycleEventType.REQUEST_STARTED, {
31
+ requestIndex: context.requestIndex,
32
+ url: context.url,
33
+ })
34
+ break
35
+ case 'complete':
36
+ tracer.clearTracingIfNeeded(context)
37
+ lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, {
38
+ duration: context.duration,
39
+ method: context.method,
40
+ requestIndex: context.requestIndex,
41
+ spanId: context.spanId,
42
+ startClocks: context.startClocks,
43
+ status: context.status,
44
+ traceId: context.traceId,
45
+ traceSampled: context.traceSampled,
46
+ type: RequestType.XHR,
47
+ url: context.url,
48
+ xhr: context.xhr,
49
+ })
50
+ break
51
+ }
52
+ })
53
+
54
+ return { stop: function() { return subscription.unsubscribe() } }
55
+ }
56
+
57
+ export function trackFetch(lifeCycle, configuration, tracer) {
58
+ var subscription = initFetchObservable().subscribe(function(rawContext) {
59
+ var context = rawContext
60
+ if (!isAllowedRequestUrl(configuration, context.url)) {
61
+ return
62
+ }
63
+
64
+ switch (context.state) {
65
+ case 'start':
66
+ tracer.traceFetch(context)
67
+ context.requestIndex = getNextRequestIndex()
68
+
69
+ lifeCycle.notify(LifeCycleEventType.REQUEST_STARTED, {
70
+ requestIndex: context.requestIndex,
71
+ url: context.url,
72
+ })
73
+ break
74
+ case 'complete':
75
+ tracer.clearTracingIfNeeded(context)
76
+
77
+ lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, {
78
+ duration: context.duration,
79
+ method: context.method,
80
+ requestIndex: context.requestIndex,
81
+ responseType: context.responseType,
82
+ spanId: context.spanId,
83
+ startClocks: context.startClocks,
84
+ status: context.status,
85
+ traceId: context.traceId,
86
+ traceSampled: context.traceSampled,
87
+ type: RequestType.FETCH,
88
+ url: context.url,
89
+ response: context.response,
90
+ init: context.init,
91
+ input: context.input,
92
+ })
93
+ break
94
+ }
95
+ })
96
+ return { stop: function() { return subscription.unsubscribe() } }
97
+ }
98
+
99
+ function getNextRequestIndex() {
100
+ var result = nextRequestIndex
101
+ nextRequestIndex += 1
102
+ return result
103
+ }
@@ -2,14 +2,18 @@ import {
2
2
  toServerDuration,
3
3
  extend,
4
4
  extend2Lev,
5
- preferredTimeStamp,
6
5
  ActionType,
7
6
  RumEventType,
8
- LifeCycleEventType
7
+ LifeCycleEventType,
8
+ UUID,
9
+ noop
9
10
  } from '@cloudcare/browser-core'
10
- import { trackActions } from './trackActions'
11
-
12
- export function startActionCollection(lifeCycle, configuration) {
11
+ import { trackClickActions } from './trackClickActions'
12
+ export function startActionCollection(
13
+ lifeCycle,
14
+ domMutationObservable,
15
+ configuration
16
+ ) {
13
17
  lifeCycle.subscribe(
14
18
  LifeCycleEventType.AUTO_ACTION_COMPLETED,
15
19
  function (action) {
@@ -19,11 +23,17 @@ export function startActionCollection(lifeCycle, configuration) {
19
23
  )
20
24
  }
21
25
  )
26
+
27
+ var actionContexts = { findActionId: noop }
22
28
  if (configuration.trackInteractions) {
23
- trackActions(lifeCycle)
29
+ actionContexts = trackClickActions(
30
+ lifeCycle,
31
+ domMutationObservable,
32
+ configuration
33
+ ).actionContexts
24
34
  }
25
-
26
35
  return {
36
+ actionContexts: actionContexts,
27
37
  addAction: function (action, savedCommonContext) {
28
38
  lifeCycle.notify(
29
39
  LifeCycleEventType.RAW_RUM_EVENT_COLLECTED,
@@ -45,23 +55,33 @@ function processAction(action) {
45
55
  },
46
56
  id: action.id,
47
57
  loadingTime: toServerDuration(action.duration),
58
+ frustration: {
59
+ type: action.frustrationTypes
60
+ },
48
61
  long_task: {
49
62
  count: action.counts.longTaskCount
50
63
  },
51
64
  resource: {
52
65
  count: action.counts.resourceCount
53
66
  }
67
+ },
68
+ _dd: {
69
+ action: {
70
+ target: action.target,
71
+ position: action.position
72
+ }
54
73
  }
55
74
  }
56
75
  : {
57
- action: {
58
- loadingTime: 0
76
+ action: {
77
+ loadingTime: 0
78
+ }
59
79
  }
60
- }
61
80
  var customerContext = !isAutoAction(action) ? action.context : undefined
62
81
  var actionEvent = extend2Lev(
63
82
  {
64
83
  action: {
84
+ id: UUID(),
65
85
  target: {
66
86
  name: action.name
67
87
  },
@@ -75,7 +95,10 @@ function processAction(action) {
75
95
  return {
76
96
  customerContext: customerContext,
77
97
  rawRumEvent: actionEvent,
78
- startTime: action.startClocks.relative
98
+ startTime: action.startClocks.relative,
99
+ domainContext: isAutoAction(action)
100
+ ? { event: action.event, events: action.events }
101
+ : {}
79
102
  }
80
103
  }
81
104
 
@@ -0,0 +1,90 @@
1
+ import { ONE_SECOND, every } from '@cloudcare/browser-core'
2
+
3
+ export var MAX_DURATION_BETWEEN_CLICKS = ONE_SECOND
4
+ export var MAX_DISTANCE_BETWEEN_CLICKS = 100
5
+
6
+ var ClickChainStatus = {
7
+ WaitingForMoreClicks: 'WaitingForMoreClicks',
8
+ WaitingForClicksToStop: 'WaitingForClicksToStop',
9
+ Finalized: 'Finalized'
10
+ }
11
+
12
+ export function createClickChain(firstClick, onFinalize) {
13
+ var bufferedClicks = []
14
+ var status = ClickChainStatus.WaitingForMoreClicks
15
+ var maxDurationBetweenClicksTimeout
16
+ appendClick(firstClick)
17
+
18
+ function appendClick(click) {
19
+ click.stopObservable.subscribe(tryFinalize)
20
+ bufferedClicks.push(click)
21
+ clearTimeout(maxDurationBetweenClicksTimeout)
22
+ maxDurationBetweenClicksTimeout = setTimeout(
23
+ dontAcceptMoreClick,
24
+ MAX_DURATION_BETWEEN_CLICKS
25
+ )
26
+ }
27
+
28
+ function tryFinalize() {
29
+ if (
30
+ status === ClickChainStatus.WaitingForClicksToStop &&
31
+ every(bufferedClicks, function (click) {
32
+ return click.isStopped()
33
+ })
34
+ ) {
35
+ status = ClickChainStatus.Finalized
36
+ onFinalize(bufferedClicks)
37
+ }
38
+ }
39
+
40
+ function dontAcceptMoreClick() {
41
+ clearTimeout(maxDurationBetweenClicksTimeout)
42
+ if (status === ClickChainStatus.WaitingForMoreClicks) {
43
+ status = ClickChainStatus.WaitingForClicksToStop
44
+ tryFinalize()
45
+ }
46
+ }
47
+
48
+ return {
49
+ tryAppend: function (click) {
50
+ if (status !== ClickChainStatus.WaitingForMoreClicks) {
51
+ return false
52
+ }
53
+
54
+ if (
55
+ bufferedClicks.length > 0 &&
56
+ !areEventsSimilar(
57
+ bufferedClicks[bufferedClicks.length - 1].event,
58
+ click.event
59
+ )
60
+ ) {
61
+ dontAcceptMoreClick()
62
+ return false
63
+ }
64
+
65
+ appendClick(click)
66
+ return true
67
+ },
68
+ stop: function () {
69
+ dontAcceptMoreClick()
70
+ }
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Checks whether two events are similar by comparing their target, position and timestamp
76
+ */
77
+ function areEventsSimilar(first, second) {
78
+ return (
79
+ first.target === second.target &&
80
+ mouseEventDistance(first, second) <= MAX_DISTANCE_BETWEEN_CLICKS &&
81
+ first.timeStamp - second.timeStamp <= MAX_DURATION_BETWEEN_CLICKS
82
+ )
83
+ }
84
+
85
+ function mouseEventDistance(origin, other) {
86
+ return Math.sqrt(
87
+ Math.pow(origin.clientX - other.clientX, 2) +
88
+ Math.pow(origin.clientY - other.clientY, 2)
89
+ )
90
+ }
@@ -0,0 +1,84 @@
1
+ import {
2
+ elementMatches,
3
+ ONE_SECOND,
4
+ FrustrationType,
5
+ some,
6
+ each
7
+ } from '@cloudcare/browser-core'
8
+
9
+ var MIN_CLICKS_PER_SECOND_TO_CONSIDER_RAGE = 3
10
+
11
+ export function computeFrustration(clicks, rageClick) {
12
+ if (isRage(clicks)) {
13
+ rageClick.addFrustration(FrustrationType.RAGE_CLICK)
14
+ if (clicks.some(isDead)) {
15
+ rageClick.addFrustration(FrustrationType.DEAD_CLICK)
16
+ }
17
+ if (rageClick.hasError) {
18
+ rageClick.addFrustration(FrustrationType.ERROR_CLICK)
19
+ }
20
+ return { isRage: true }
21
+ }
22
+
23
+ var hasSelectionChanged = some(clicks, function (click) {
24
+ return click.getUserActivity().selection
25
+ })
26
+ each(clicks, function (click) {
27
+ if (click.hasError) {
28
+ click.addFrustration(FrustrationType.ERROR_CLICK)
29
+ }
30
+ if (
31
+ isDead(click) &&
32
+ // Avoid considering clicks part of a double-click or triple-click selections as dead clicks
33
+ !hasSelectionChanged
34
+ ) {
35
+ click.addFrustration(FrustrationType.DEAD_CLICK)
36
+ }
37
+ })
38
+
39
+ return { isRage: false }
40
+ }
41
+
42
+ export function isRage(clicks) {
43
+ if (
44
+ some(clicks, function (click) {
45
+ return click.getUserActivity().selection
46
+ })
47
+ ) {
48
+ return false
49
+ }
50
+ for (
51
+ var i = 0;
52
+ i < clicks.length - (MIN_CLICKS_PER_SECOND_TO_CONSIDER_RAGE - 1);
53
+ i += 1
54
+ ) {
55
+ if (
56
+ clicks[i + MIN_CLICKS_PER_SECOND_TO_CONSIDER_RAGE - 1].event.timeStamp -
57
+ clicks[i].event.timeStamp <=
58
+ ONE_SECOND
59
+ ) {
60
+ return true
61
+ }
62
+ }
63
+ return false
64
+ }
65
+
66
+ var DEAD_CLICK_EXCLUDE_SELECTOR =
67
+ // inputs that don't trigger a meaningful event like "input" when clicked, including textual
68
+ // inputs (using a negative selector is shorter here)
69
+ 'input:not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="range"]),' +
70
+ 'textarea,' +
71
+ 'select,' +
72
+ // canvas, as there is no good way to detect activity occurring on them
73
+ 'canvas,' +
74
+ // links that are interactive (have an href attribute) or any of their descendants, as they can
75
+ // open a new tab or navigate to a hash without triggering a meaningful event
76
+ 'a[href],' +
77
+ 'a[href] *'
78
+
79
+ export function isDead(click) {
80
+ if (click.hasPageActivity || click.getUserActivity().input) {
81
+ return false
82
+ }
83
+ return !elementMatches(click.event.target, DEAD_CLICK_EXCLUDE_SELECTOR)
84
+ }