@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
@@ -1,11 +1,11 @@
1
- import { isEmptyObject, msToNs, extend2Lev, toServerDuration, mapValues, RumEventType, LifeCycleEventType } from '@cloudcare/browser-core';
1
+ import { isEmptyObject, mapValues, toServerDuration, isNumber, RumEventType, LifeCycleEventType, extend2Lev } from '@cloudcare/browser-core';
2
2
  import { trackViews } from './trackViews';
3
3
  import { toValidEntry } from '../resource/resourceUtils';
4
- export function startViewCollection(lifeCycle, configuration, location) {
4
+ export function startViewCollection(lifeCycle, configuration, location, domMutationObservable, locationChangeObservable, initialViewOptions) {
5
5
  lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, function (view) {
6
6
  lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processViewUpdate(view));
7
7
  });
8
- return trackViews(location, lifeCycle);
8
+ return trackViews(location, lifeCycle, domMutationObservable, configuration, locationChangeObservable, !configuration.trackViewsManually, initialViewOptions);
9
9
  }
10
10
 
11
11
  function computePerformanceViewDetails(entry) {
@@ -24,35 +24,35 @@ function computePerformanceViewDetails(entry) {
24
24
  loadEventStart = validEntry.loadEventStart,
25
25
  domContentLoadedEventEnd = validEntry.domContentLoadedEventEnd;
26
26
  var details = {
27
- fmp: msToNs(validEntry.largestContentfulPaint)
27
+ fmp: toServerDuration(validEntry.largestContentfulPaint)
28
28
  };
29
29
 
30
30
  if (responseEnd !== fetchStart) {
31
- details.fpt = msToNs(responseEnd - fetchStart);
31
+ details.fpt = toServerDuration(responseEnd - fetchStart);
32
32
  var apdexLevel = parseInt((responseEnd - fetchStart) / 1000); // 秒数取整
33
33
 
34
34
  details.apdexLevel = apdexLevel > 9 ? 9 : apdexLevel;
35
35
  }
36
36
 
37
37
  if (domInteractive !== fetchStart) {
38
- details.tti = msToNs(domInteractive - fetchStart);
38
+ details.tti = toServerDuration(domInteractive - fetchStart);
39
39
  }
40
40
 
41
41
  if (domContentLoaded !== fetchStart) {
42
- details.domReady = msToNs(domContentLoaded - fetchStart);
42
+ details.dom_ready = toServerDuration(domContentLoaded - fetchStart);
43
43
  } // Make sure a connection occurred
44
44
 
45
45
 
46
46
  if (loadEventEnd !== fetchStart) {
47
- details.load = msToNs(loadEventEnd - fetchStart);
47
+ details.load = toServerDuration(loadEventEnd - fetchStart);
48
48
  }
49
49
 
50
50
  if (loadEventStart !== domContentLoadedEventEnd) {
51
- details.resourceLoadTime = msToNs(loadEventStart - domContentLoadedEventEnd);
51
+ details.resource_load_time = toServerDuration(loadEventStart - domContentLoadedEventEnd);
52
52
  }
53
53
 
54
54
  if (domComplete !== domInteractive) {
55
- details.dom = msToNs(domComplete - domInteractive);
55
+ details.dom = toServerDuration(domComplete - domInteractive);
56
56
  }
57
57
 
58
58
  return details;
@@ -61,42 +61,45 @@ function computePerformanceViewDetails(entry) {
61
61
  function processViewUpdate(view) {
62
62
  var viewEvent = {
63
63
  _dd: {
64
- documentVersion: view.documentVersion
64
+ document_version: view.documentVersion
65
65
  },
66
66
  date: view.startClocks.timeStamp,
67
67
  type: RumEventType.VIEW,
68
68
  view: {
69
69
  action: {
70
- count: view.eventCounts.userActionCount
70
+ count: view.eventCounts.actionCount
71
71
  },
72
- cumulativeLayoutShift: view.cumulativeLayoutShift,
73
- domComplete: msToNs(view.timings.domComplete),
74
- domContentLoaded: msToNs(view.timings.domContentLoaded),
75
- domInteractive: msToNs(view.timings.domInteractive),
72
+ frustration: {
73
+ count: view.eventCounts.frustrationCount
74
+ },
75
+ cumulative_layout_shift: view.cumulativeLayoutShift,
76
+ first_byte: toServerDuration(view.timings.firstByte),
77
+ dom_complete: toServerDuration(view.timings.domComplete),
78
+ dom_content_loaded: toServerDuration(view.timings.domContentLoaded),
79
+ dom_interactive: toServerDuration(view.timings.domInteractive),
76
80
  error: {
77
81
  count: view.eventCounts.errorCount
78
82
  },
79
- firstContentfulPaint: msToNs(view.timings.firstContentfulPaint),
80
- firstInputDelay: msToNs(view.timings.firstInputDelay),
81
- firstInputTime: msToNs(view.timings.firstInputTime),
82
- largestContentfulPaint: msToNs(view.timings.largestContentfulPaint),
83
- loadEventEnd: msToNs(view.timings.loadEventEnd),
84
- load_event: msToNs(view.timings.load_event),
85
- loadingTime: msToNs(view.loadingTime),
86
- loadingType: view.loadingType,
87
- isActive: view.isActive,
83
+ first_contentful_paint: toServerDuration(view.timings.firstContentfulPaint),
84
+ first_input_delay: toServerDuration(view.timings.firstInputDelay),
85
+ first_input_time: toServerDuration(view.timings.firstInputTime),
86
+ is_active: view.isActive,
88
87
  name: view.name,
89
- longTask: {
88
+ largest_contentful_paint: toServerDuration(view.timings.largestContentfulPaint),
89
+ load_event: toServerDuration(view.timings.loadEvent),
90
+ loading_time: discardNegativeDuration(toServerDuration(view.loadingTime)),
91
+ loading_type: view.loadingType,
92
+ long_task: {
90
93
  count: view.eventCounts.longTaskCount
91
94
  },
92
95
  resource: {
93
96
  count: view.eventCounts.resourceCount
94
97
  },
95
- timeSpent: msToNs(view.duration)
96
- },
97
- session: {
98
- hasReplay: view.hasReplay || undefined
99
- }
98
+ time_spent: toServerDuration(view.duration)
99
+ } // session: {
100
+ // has_replay: replayStats ? true : undefined,
101
+ // },
102
+
100
103
  };
101
104
 
102
105
  if (!isEmptyObject(view.customTimings)) {
@@ -108,6 +111,13 @@ function processViewUpdate(view) {
108
111
  });
109
112
  return {
110
113
  rawRumEvent: viewEvent,
111
- startTime: view.startClocks.relative
114
+ startTime: view.startClocks.relative,
115
+ domainContext: {
116
+ location: view.location
117
+ }
112
118
  };
119
+ }
120
+
121
+ function discardNegativeDuration(duration) {
122
+ return isNumber(duration) && duration < 0 ? undefined : duration;
113
123
  }
@@ -0,0 +1,80 @@
1
+ import { performDraw, startSessionManager, LifeCycleEventType } from '@cloudcare/browser-core';
2
+ export var RUM_SESSION_KEY = 'rum';
3
+ export var RumSessionPlan = {
4
+ LITE: 1,
5
+ PREMIUM: 2
6
+ };
7
+ export var RumTrackingType = {
8
+ NOT_TRACKED: '0',
9
+ // Note: the "tracking type" value (stored in the session cookie) does not match the "session
10
+ // plan" value (sent in RUM events). This is expected, and was done to keep retrocompatibility
11
+ // with active sessions when upgrading the SDK.
12
+ TRACKED_WITH_SESSION_REPLAY: '1',
13
+ TRACKED_WITHOUT_SESSION_REPLAY: '2'
14
+ };
15
+ export function startRumSessionManager(configuration, lifeCycle) {
16
+ var sessionManager = startSessionManager(configuration.cookieOptions, RUM_SESSION_KEY, function (rawTrackingType) {
17
+ return computeSessionState(configuration, rawTrackingType);
18
+ });
19
+ sessionManager.expireObservable.subscribe(function () {
20
+ lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED);
21
+ });
22
+ sessionManager.renewObservable.subscribe(function () {
23
+ lifeCycle.notify(LifeCycleEventType.SESSION_RENEWED);
24
+ });
25
+ return {
26
+ findTrackedSession: function findTrackedSession(startTime) {
27
+ var session = sessionManager.findActiveSession(startTime);
28
+
29
+ if (!session || !isTypeTracked(session.trackingType)) {
30
+ return;
31
+ }
32
+
33
+ return {
34
+ id: session.id
35
+ };
36
+ }
37
+ };
38
+ }
39
+ /**
40
+ * Start a tracked replay session stub
41
+ * It needs to be a premium plan in order to get long tasks
42
+ */
43
+
44
+ export function startRumSessionManagerStub() {
45
+ var session = {
46
+ id: '00000000-aaaa-0000-aaaa-000000000000'
47
+ };
48
+ return {
49
+ findTrackedSession: function findTrackedSession() {
50
+ return session;
51
+ }
52
+ };
53
+ }
54
+
55
+ function computeSessionState(configuration, rawTrackingType) {
56
+ var trackingType;
57
+
58
+ if (hasValidRumSession(rawTrackingType)) {
59
+ trackingType = rawTrackingType;
60
+ } else if (!performDraw(configuration.sampleRate)) {
61
+ trackingType = RumTrackingType.NOT_TRACKED;
62
+ } else if (!performDraw(configuration.sessionReplaySampleRate)) {
63
+ trackingType = RumTrackingType.TRACKED_WITHOUT_SESSION_REPLAY;
64
+ } else {
65
+ trackingType = RumTrackingType.TRACKED_WITH_SESSION_REPLAY;
66
+ }
67
+
68
+ return {
69
+ trackingType: trackingType,
70
+ isTracked: isTypeTracked(trackingType)
71
+ };
72
+ }
73
+
74
+ function hasValidRumSession(trackingType) {
75
+ return trackingType === RumTrackingType.NOT_TRACKED || trackingType === RumTrackingType.TRACKED_WITH_SESSION_REPLAY || trackingType === RumTrackingType.TRACKED_WITHOUT_SESSION_REPLAY;
76
+ }
77
+
78
+ function isTypeTracked(rumSessionType) {
79
+ return rumSessionType === RumTrackingType.TRACKED_WITHOUT_SESSION_REPLAY || rumSessionType === RumTrackingType.TRACKED_WITH_SESSION_REPLAY;
80
+ }
@@ -1,7 +1,7 @@
1
1
  import { base64Encode, urlParse } from '@cloudcare/browser-core'; // start SkyWalking
2
2
 
3
3
  function uuid() {
4
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
4
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
5
5
  /* tslint:disable */
6
6
  var r = Math.random() * 16 | 0;
7
7
  /* tslint:disable */
@@ -5,18 +5,17 @@ import { JaegerTracer } from './jaegerTracer';
5
5
  import { ZipkinSingleTracer } from './zipkinSingleTracer';
6
6
  import { ZipkinMultiTracer } from './zipkinMultiTracer';
7
7
  import { W3cTraceParentTracer } from './w3cTraceParentTracer';
8
- export function clearTracingIfCancelled(context) {
8
+ export function clearTracingIfNeeded(context) {
9
9
  if (context.status === 0 && !context.isAborted) {
10
10
  context.traceId = undefined;
11
11
  context.spanId = undefined;
12
12
  }
13
13
  }
14
- export function startTracer(configuration) {
14
+ export function startTracer(configuration, sessionManager) {
15
15
  return {
16
- clearTracingIfCancelled: clearTracingIfCancelled,
16
+ clearTracingIfNeeded: clearTracingIfNeeded,
17
17
  traceFetch: function traceFetch(context) {
18
- return injectHeadersIfTracingAllowed(configuration, context, function (tracingHeaders) {
19
- // if (context.input instanceof Request && (context.init))
18
+ return injectHeadersIfTracingAllowed(configuration, context, sessionManager, function (tracingHeaders) {
20
19
  if (context.input instanceof Request && (!context.init || !context.init.headers)) {
21
20
  context.input = new Request(context.input);
22
21
  each(tracingHeaders, function (value, key) {
@@ -51,7 +50,7 @@ export function startTracer(configuration) {
51
50
  });
52
51
  },
53
52
  traceXhr: function traceXhr(context, xhr) {
54
- return injectHeadersIfTracingAllowed(configuration, context, function (tracingHeaders) {
53
+ return injectHeadersIfTracingAllowed(configuration, context, sessionManager, function (tracingHeaders) {
55
54
  each(tracingHeaders, function (value, name) {
56
55
  xhr.setRequestHeader(name, value);
57
56
  });
@@ -72,8 +71,8 @@ function isAllowedUrl(configuration, requestUrl) {
72
71
  return flag;
73
72
  }
74
73
 
75
- export function injectHeadersIfTracingAllowed(configuration, context, inject) {
76
- if (!isAllowedUrl(configuration, context.url) || !configuration.traceType) {
74
+ export function injectHeadersIfTracingAllowed(configuration, context, sessionManager, inject) {
75
+ if (!isAllowedUrl(configuration, context.url) || !configuration.traceType || !sessionManager.findTrackedSession()) {
77
76
  return;
78
77
  }
79
78
 
@@ -8,19 +8,22 @@ export function trackEventCounts(lifeCycle, callback) {
8
8
  errorCount: 0,
9
9
  longTaskCount: 0,
10
10
  resourceCount: 0,
11
- userActionCount: 0
11
+ actionCount: 0,
12
+ frustrationCount: 0
12
13
  };
13
- var subscription = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, function (data) {
14
- var rawRumEvent = data.rawRumEvent;
15
-
16
- switch (rawRumEvent.type) {
14
+ var subscription = lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_COLLECTED, function (event) {
15
+ switch (event.type) {
17
16
  case RumEventType.ERROR:
18
17
  eventCounts.errorCount += 1;
19
18
  callback(eventCounts);
20
19
  break;
21
20
 
22
21
  case RumEventType.ACTION:
23
- eventCounts.userActionCount += 1;
22
+ if (event.action.frustration) {
23
+ eventCounts.frustrationCount += event.action.frustration.type.length;
24
+ }
25
+
26
+ eventCounts.actionCount += 1;
24
27
  callback(eventCounts);
25
28
  break;
26
29
 
@@ -0,0 +1,17 @@
1
+ import { getCookie, setCookie, UUID, ONE_HOUR } from '@cloudcare/browser-core';
2
+ export var ANONYMOUS_ID_COOKIE_NAME = '_dataflulx_usr_id';
3
+ export var ANONYMOUS_ID_EXPIRATION = 60 * 24 * ONE_HOUR;
4
+ export var startCacheUsrCache = function startCacheUsrCache(configuration) {
5
+ var usrCacheId = getCookie(ANONYMOUS_ID_COOKIE_NAME);
6
+
7
+ if (!usrCacheId) {
8
+ usrCacheId = UUID();
9
+ setCookie(ANONYMOUS_ID_COOKIE_NAME, usrCacheId, ANONYMOUS_ID_EXPIRATION, configuration.cookieOptions);
10
+ }
11
+
12
+ return {
13
+ getId: function getId() {
14
+ return usrCacheId;
15
+ }
16
+ };
17
+ };
@@ -0,0 +1,147 @@
1
+ import { Observable, each, timeStampNow, LifeCycleEventType, some, matchList, instrumentMethodAndCallOriginal } from '@cloudcare/browser-core'; // Delay to wait for a page activity to validate the tracking process
2
+
3
+ export var PAGE_ACTIVITY_VALIDATION_DELAY = 100; // Delay to wait after a page activity to end the tracking process
4
+
5
+ export var PAGE_ACTIVITY_END_DELAY = 100;
6
+ /**
7
+ * Wait for the page activity end
8
+ *
9
+ * Detection lifecycle:
10
+ * ```
11
+ * Wait page activity end
12
+ * .-------------------'--------------------.
13
+ * v v
14
+ * [Wait for a page activity ] [Wait for a maximum duration]
15
+ * [timeout: VALIDATION_DELAY] [ timeout: maxDuration ]
16
+ * / \ |
17
+ * v v |
18
+ * [No page activity] [Page activity] |
19
+ * | |,----------------------. |
20
+ * v v | |
21
+ * (Discard) [Wait for a page activity] | |
22
+ * [ timeout: END_DELAY ] | |
23
+ * / \ | |
24
+ * v v | |
25
+ * [No page activity] [Page activity] | |
26
+ * | | | |
27
+ * | '------------' |
28
+ * '-----------. ,--------------------'
29
+ * v
30
+ * (End)
31
+ * ```
32
+ *
33
+ * Note: by assuming that maxDuration is greater than VALIDATION_DELAY, we are sure that if the
34
+ * process is still alive after maxDuration, it has been validated.
35
+ */
36
+
37
+ export function waitPageActivityEnd(lifeCycle, domMutationObservable, configuration, pageActivityEndCallback, maxDuration) {
38
+ var pageActivityObservable = createPageActivityObservable(lifeCycle, domMutationObservable, configuration);
39
+ return doWaitPageActivityEnd(pageActivityObservable, pageActivityEndCallback, maxDuration);
40
+ }
41
+ export function doWaitPageActivityEnd(pageActivityObservable, pageActivityEndCallback, maxDuration) {
42
+ var pageActivityEndTimeoutId;
43
+ var hasCompleted = false;
44
+ var validationTimeoutId = setTimeout(function () {
45
+ complete({
46
+ hadActivity: false
47
+ });
48
+ }, PAGE_ACTIVITY_VALIDATION_DELAY);
49
+ var maxDurationTimeoutId = maxDuration && setTimeout(function () {
50
+ return complete({
51
+ hadActivity: true,
52
+ end: timeStampNow()
53
+ });
54
+ }, maxDuration);
55
+ var pageActivitySubscription = pageActivityObservable.subscribe(function (data) {
56
+ var isBusy = data.isBusy;
57
+ clearTimeout(validationTimeoutId);
58
+ clearTimeout(pageActivityEndTimeoutId);
59
+ var lastChangeTime = timeStampNow();
60
+
61
+ if (!isBusy) {
62
+ pageActivityEndTimeoutId = setTimeout(function () {
63
+ complete({
64
+ hadActivity: true,
65
+ end: lastChangeTime
66
+ });
67
+ }, PAGE_ACTIVITY_END_DELAY);
68
+ }
69
+ });
70
+
71
+ var stop = function stop() {
72
+ hasCompleted = true;
73
+ clearTimeout(validationTimeoutId);
74
+ clearTimeout(pageActivityEndTimeoutId);
75
+ clearTimeout(maxDurationTimeoutId);
76
+ pageActivitySubscription.unsubscribe();
77
+ };
78
+
79
+ function complete(event) {
80
+ if (hasCompleted) {
81
+ return;
82
+ }
83
+
84
+ stop();
85
+ pageActivityEndCallback(event);
86
+ }
87
+
88
+ return {
89
+ stop: stop
90
+ };
91
+ }
92
+ export function createPageActivityObservable(lifeCycle, domMutationObservable, configuration) {
93
+ var observable = new Observable(function () {
94
+ var subscriptions = [];
95
+ var firstRequestIndex;
96
+ var pendingRequestsCount = 0;
97
+ subscriptions.push(domMutationObservable.subscribe(notifyPageActivity), lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, function (entries) {
98
+ if (some(entries, function (entry) {
99
+ return entry.entryType === 'resource' && !isExcludedUrl(configuration, entry.name);
100
+ })) {
101
+ notifyPageActivity();
102
+ }
103
+ }), lifeCycle.subscribe(LifeCycleEventType.REQUEST_STARTED, function (startEvent) {
104
+ if (isExcludedUrl(configuration, startEvent.url)) {
105
+ return;
106
+ }
107
+
108
+ if (firstRequestIndex === undefined) {
109
+ firstRequestIndex = startEvent.requestIndex;
110
+ }
111
+
112
+ pendingRequestsCount += 1;
113
+ notifyPageActivity();
114
+ }), lifeCycle.subscribe(LifeCycleEventType.REQUEST_COMPLETED, function (request) {
115
+ if (isExcludedUrl(configuration, request.url) || firstRequestIndex === undefined || // If the request started before the tracking start, ignore it
116
+ request.requestIndex < firstRequestIndex) {
117
+ return;
118
+ }
119
+
120
+ pendingRequestsCount -= 1;
121
+ notifyPageActivity();
122
+ }));
123
+ trackWindowOpen(notifyPageActivity);
124
+ return function () {
125
+ each(subscriptions, function (s) {
126
+ s.unsubscribe();
127
+ });
128
+ };
129
+
130
+ function notifyPageActivity() {
131
+ observable.notify({
132
+ isBusy: pendingRequestsCount > 0
133
+ });
134
+ }
135
+ });
136
+ return observable;
137
+ }
138
+
139
+ function isExcludedUrl(configuration, requestUrl) {
140
+ return matchList(configuration.excludedActivityUrls, requestUrl);
141
+ }
142
+
143
+ function trackWindowOpen(callback) {
144
+ return instrumentMethodAndCallOriginal(window, 'open', {
145
+ before: callback
146
+ });
147
+ }
@@ -0,0 +1,30 @@
1
+ import { Batch, createHttpRequest, LifeCycleEventType, RumEventType } from '@cloudcare/browser-core';
2
+ export function startRumBatch(configuration, lifeCycle, reportError) {
3
+ var batch = makeRumBatch(configuration, lifeCycle, reportError);
4
+ lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_COLLECTED, function (serverRumEvent) {
5
+ if (serverRumEvent.type === RumEventType.VIEW) {
6
+ batch.upsert(serverRumEvent, serverRumEvent.view.id);
7
+ } else {
8
+ batch.add(serverRumEvent);
9
+ }
10
+ });
11
+ }
12
+
13
+ function makeRumBatch(configuration, lifeCycle, reportError) {
14
+ var primaryBatch = createRumBatch(configuration.datakitUrl, function () {
15
+ lifeCycle.notify(LifeCycleEventType.BEFORE_UNLOAD);
16
+ });
17
+
18
+ function createRumBatch(endpointUrl, unloadCallback) {
19
+ return new Batch(createHttpRequest(endpointUrl, configuration.batchBytesLimit, reportError), configuration.batchMessagesLimit, configuration.batchBytesLimit, configuration.messageBytesLimit, configuration.flushTimeout, unloadCallback);
20
+ }
21
+
22
+ return {
23
+ add: function add(message) {
24
+ primaryBatch.add(message);
25
+ },
26
+ upsert: function upsert(message, key) {
27
+ primaryBatch.upsert(message, key);
28
+ }
29
+ };
30
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@cloudcare/browser-rum",
3
- "version": "2.1.6",
3
+ "version": "2.2.0",
4
4
  "main": "cjs/index.js",
5
5
  "module": "esm/index.js",
6
6
  "dependencies": {
7
- "@cloudcare/browser-core": "1.1.3"
7
+ "@cloudcare/browser-core": "1.2.0"
8
8
  },
9
9
  "scripts": {
10
10
  "dev": "webpack serve --open --mode=development",
@@ -29,5 +29,5 @@
29
29
  "license": "MIT",
30
30
  "homepage": "https://github.com/CloudCare/dataflux-rum-sdk-javascript/blob/master/packages/rum/README.md",
31
31
  "description": "DataFlux RUM Web 端数据指标监控",
32
- "gitHead": "71ec98f918a9ca38e5af54ed274f229609e0090e"
32
+ "gitHead": "12b759bde16de07678f898b82c1bcf11c403eabe"
33
33
  }