@newrelic/browser-agent 1.298.0-rc.4 → 1.298.0-rc.5

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 (53) hide show
  1. package/dist/cjs/common/constants/env.cdn.js +1 -1
  2. package/dist/cjs/common/constants/env.npm.js +1 -1
  3. package/dist/cjs/common/dom/selector-path.js +60 -44
  4. package/dist/cjs/common/url/extract-url.js +21 -0
  5. package/dist/cjs/features/ajax/instrument/index.js +2 -9
  6. package/dist/cjs/features/generic_events/aggregate/index.js +29 -8
  7. package/dist/cjs/features/generic_events/aggregate/user-actions/aggregated-user-action.js +5 -3
  8. package/dist/cjs/features/generic_events/aggregate/user-actions/user-actions-aggregator.js +71 -33
  9. package/dist/cjs/features/generic_events/constants.js +2 -1
  10. package/dist/cjs/features/generic_events/instrument/index.js +45 -0
  11. package/dist/cjs/loaders/api/noticeError.js +1 -0
  12. package/dist/cjs/loaders/configure/configure.js +1 -0
  13. package/dist/esm/common/constants/env.cdn.js +1 -1
  14. package/dist/esm/common/constants/env.npm.js +1 -1
  15. package/dist/esm/common/dom/selector-path.js +58 -42
  16. package/dist/esm/common/url/extract-url.js +15 -0
  17. package/dist/esm/features/ajax/instrument/index.js +2 -9
  18. package/dist/esm/features/generic_events/aggregate/index.js +29 -8
  19. package/dist/esm/features/generic_events/aggregate/user-actions/aggregated-user-action.js +5 -3
  20. package/dist/esm/features/generic_events/aggregate/user-actions/user-actions-aggregator.js +72 -34
  21. package/dist/esm/features/generic_events/constants.js +1 -0
  22. package/dist/esm/features/generic_events/instrument/index.js +46 -1
  23. package/dist/esm/loaders/api/noticeError.js +1 -0
  24. package/dist/esm/loaders/configure/configure.js +1 -0
  25. package/dist/tsconfig.tsbuildinfo +1 -1
  26. package/dist/types/common/dom/selector-path.d.ts +6 -1
  27. package/dist/types/common/dom/selector-path.d.ts.map +1 -1
  28. package/dist/types/common/url/extract-url.d.ts +7 -0
  29. package/dist/types/common/url/extract-url.d.ts.map +1 -0
  30. package/dist/types/features/ajax/instrument/index.d.ts.map +1 -1
  31. package/dist/types/features/generic_events/aggregate/index.d.ts +1 -3
  32. package/dist/types/features/generic_events/aggregate/index.d.ts.map +1 -1
  33. package/dist/types/features/generic_events/aggregate/user-actions/aggregated-user-action.d.ts +3 -1
  34. package/dist/types/features/generic_events/aggregate/user-actions/aggregated-user-action.d.ts.map +1 -1
  35. package/dist/types/features/generic_events/aggregate/user-actions/user-actions-aggregator.d.ts +3 -0
  36. package/dist/types/features/generic_events/aggregate/user-actions/user-actions-aggregator.d.ts.map +1 -1
  37. package/dist/types/features/generic_events/constants.d.ts +1 -0
  38. package/dist/types/features/generic_events/constants.d.ts.map +1 -1
  39. package/dist/types/features/generic_events/instrument/index.d.ts +2 -0
  40. package/dist/types/features/generic_events/instrument/index.d.ts.map +1 -1
  41. package/dist/types/loaders/api/noticeError.d.ts.map +1 -1
  42. package/dist/types/loaders/configure/configure.d.ts.map +1 -1
  43. package/package.json +1 -1
  44. package/src/common/dom/selector-path.js +51 -39
  45. package/src/common/url/extract-url.js +17 -0
  46. package/src/features/ajax/instrument/index.js +2 -10
  47. package/src/features/generic_events/aggregate/index.js +23 -8
  48. package/src/features/generic_events/aggregate/user-actions/aggregated-user-action.js +5 -3
  49. package/src/features/generic_events/aggregate/user-actions/user-actions-aggregator.js +80 -24
  50. package/src/features/generic_events/constants.js +2 -0
  51. package/src/features/generic_events/instrument/index.js +51 -1
  52. package/src/loaders/api/noticeError.js +1 -0
  53. package/src/loaders/configure/configure.js +1 -0
@@ -1,5 +1,5 @@
1
1
  export class AggregatedUserAction {
2
- constructor(evt: any, selectorPath: any, nearestTargetFields: any);
2
+ constructor(evt: any, selectorInfo: any);
3
3
  event: any;
4
4
  count: number;
5
5
  originMs: number;
@@ -8,6 +8,8 @@ export class AggregatedUserAction {
8
8
  rageClick: boolean | undefined;
9
9
  nearestTargetFields: any;
10
10
  currentUrl: string;
11
+ deadClick: boolean;
12
+ errorClick: boolean;
11
13
  /**
12
14
  * Aggregates the count and maintains the relative MS array for matching events
13
15
  * Will determine if a rage click was observed as part of the aggregation
@@ -1 +1 @@
1
- {"version":3,"file":"aggregated-user-action.d.ts","sourceRoot":"","sources":["../../../../../../src/features/generic_events/aggregate/user-actions/aggregated-user-action.js"],"names":[],"mappings":"AAOA;IACE,mEASC;IARC,WAAgB;IAChB,cAAc;IACd,iBAAyC;IACzC,qBAAqB;IACrB,kBAAgC;IAChC,+BAA0B;IAC1B,yBAA8C;IAC9C,mBAAyC;IAG3C;;;;;OAKG;IACH,eAHW,KAAK,GACH,IAAI,CAMhB;IAED;;;OAGG;IACH,eAFa,OAAO,CAKnB;CACF"}
1
+ {"version":3,"file":"aggregated-user-action.d.ts","sourceRoot":"","sources":["../../../../../../src/features/generic_events/aggregate/user-actions/aggregated-user-action.js"],"names":[],"mappings":"AAOA;IACE,yCAWC;IAVC,WAAgB;IAChB,cAAc;IACd,iBAAyC;IACzC,qBAAqB;IACrB,kBAAqC;IACrC,+BAA0B;IAC1B,yBAAqD;IACrD,mBAAyC;IACzC,mBAAsB;IACtB,oBAAuB;IAGzB;;;;;OAKG;IACH,eAHW,KAAK,GACH,IAAI,CAMhB;IAED;;;OAGG;IACH,eAFa,OAAO,CAKnB;CACF"}
@@ -1,4 +1,5 @@
1
1
  export class UserActionsAggregator {
2
+ constructor(userFrustrationsEnabled: any);
2
3
  get aggregationEvent(): AggregatedUserAction | undefined;
3
4
  /**
4
5
  * Process the event and determine if a new aggregation set should be made or if it should increment the current aggregation
@@ -6,6 +7,8 @@ export class UserActionsAggregator {
6
7
  * @returns {AggregatedUserAction|undefined} The previous aggregation set if it has been completed by processing the current event
7
8
  */
8
9
  process(evt: Event, targetFields: any): AggregatedUserAction | undefined;
10
+ markAsErrorClick(): void;
11
+ isLiveClick(): void;
9
12
  #private;
10
13
  }
11
14
  import { AggregatedUserAction } from './aggregated-user-action';
@@ -1 +1 @@
1
- {"version":3,"file":"user-actions-aggregator.d.ts","sourceRoot":"","sources":["../../../../../../src/features/generic_events/aggregate/user-actions/user-actions-aggregator.js"],"names":[],"mappings":"AAQA;IAKE,yDAQC;IAED;;;;OAIG;IACH,aAHW,KAAK,sBACH,oBAAoB,GAAC,SAAS,CAiB1C;;CACF;qCAtCoC,0BAA0B"}
1
+ {"version":3,"file":"user-actions-aggregator.d.ts","sourceRoot":"","sources":["../../../../../../src/features/generic_events/aggregate/user-actions/user-actions-aggregator.js"],"names":[],"mappings":"AAUA;IASE,0CAKC;IAED,yDAQC;IAED;;;;OAIG;IACH,aAHW,KAAK,sBACH,oBAAoB,GAAC,SAAS,CA6B1C;IAED,yBAKC;IA0CD,oBAEC;;CACF;qCAlHoC,0BAA0B"}
@@ -3,6 +3,7 @@ export const OBSERVED_EVENTS: string[];
3
3
  export const OBSERVED_WINDOW_EVENTS: string[];
4
4
  export const RAGE_CLICK_THRESHOLD_EVENTS: 4;
5
5
  export const RAGE_CLICK_THRESHOLD_MS: 1000;
6
+ export const FRUSTRATION_TIMEOUT_MS: 2000;
6
7
  export const RESERVED_EVENT_TYPES: string[];
7
8
  export namespace FEATURE_FLAGS {
8
9
  let MARKS: string;
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../src/features/generic_events/constants.js"],"names":[],"mappings":"AAMA,kCAAuD;AAEvD,uCAA6F;AAC7F,8CAAuD;AAEvD,0CAA2C,CAAC,CAAA;AAC5C,sCAAuC,IAAI,CAAA;AAE3C,4CAAsF"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../src/features/generic_events/constants.js"],"names":[],"mappings":"AAMA,kCAAuD;AAEvD,uCAA6F;AAC7F,8CAAuD;AAEvD,0CAA2C,CAAC,CAAA;AAC5C,sCAAuC,IAAI,CAAA;AAE3C,qCAAsC,IAAI,CAAA;AAE1C,4CAAsF"}
@@ -1,6 +1,8 @@
1
1
  export class Instrument extends InstrumentBase {
2
2
  static featureName: string;
3
3
  constructor(agentRef: any);
4
+ removeOnAbort: AbortController | undefined;
5
+ abortHandler: () => void;
4
6
  }
5
7
  export const GenericEvents: typeof Instrument;
6
8
  import { InstrumentBase } from '../../utils/instrument-base';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/features/generic_events/instrument/index.js"],"names":[],"mappings":"AAiBA;IACE,2BAAiC;IACjC,2BA2CC;CACF;AAED,8CAAuC;+BAnDR,6BAA6B"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/features/generic_events/instrument/index.js"],"names":[],"mappings":"AAuBA;IACE,2BAAiC;IACjC,2BAuFC;IArCG,2CAA0C;IAG5C,yBAGC;CAgCJ;AAED,8CAAuC;+BArGR,6BAA6B"}
@@ -1 +1 @@
1
- {"version":3,"file":"noticeError.d.ts","sourceRoot":"","sources":["../../../../src/loaders/api/noticeError.js"],"names":[],"mappings":"AAUA,sDAEC;AAED,6HAGC"}
1
+ {"version":3,"file":"noticeError.d.ts","sourceRoot":"","sources":["../../../../src/loaders/api/noticeError.js"],"names":[],"mappings":"AAUA,sDAEC;AAED,6HAIC"}
@@ -1 +1 @@
1
- {"version":3,"file":"configure.d.ts","sourceRoot":"","sources":["../../../../src/loaders/configure/configure.js"],"names":[],"mappings":"AAkBA;;;GAGG;AACH,oGA6DC"}
1
+ {"version":3,"file":"configure.d.ts","sourceRoot":"","sources":["../../../../src/loaders/configure/configure.js"],"names":[],"mappings":"AAkBA;;;GAGG;AACH,oGA8DC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newrelic/browser-agent",
3
- "version": "1.298.0-rc.4",
3
+ "version": "1.298.0-rc.5",
4
4
  "private": false,
5
5
  "author": "New Relic Browser Agent Team <browser-agent@newrelic.com>",
6
6
  "description": "New Relic Browser Agent",
@@ -4,57 +4,69 @@
4
4
  */
5
5
 
6
6
  /**
7
- * Generates a CSS selector path for the given element, if possible
7
+ * Generates a CSS selector path for the given element, if possible.
8
+ * Also gather metadata about the element's nearest fields, and whether there are any links or buttons in the path.
9
+ *
10
+ * Starts with simple cases like window or document and progresses to more complex dom-tree traversals as needed.
11
+ * Will return path: undefined if no other path can be determined.
12
+ *
8
13
  * @param {HTMLElement} elem
9
- * @param {boolean} includeId
10
- * @param {boolean} includeClass
11
- * @returns {string|undefined}
14
+ * @param {Array<string>} [targetFields=[]] specifies which fields to gather from the nearest element in the path
15
+ * @returns {{path: (undefined|string), nearestFields: {}, hasButton: boolean, hasLink: boolean}}
12
16
  */
13
- export const generateSelectorPath = (elem, targetFields = []) => {
14
- if (!elem) return { path: undefined, nearestFields: {} }
15
-
16
- const getNthOfTypeIndex = (node) => {
17
- try {
18
- let i = 1
19
- const { tagName } = node
20
- while (node.previousElementSibling) {
21
- if (node.previousElementSibling.tagName === tagName) i++
22
- node = node.previousElementSibling
23
- }
24
- return i
25
- } catch (err) {
26
- // do nothing for now. An invalid child count will make the path selector not return a nth-of-type selector statement
27
- }
28
- }
17
+ export const analyzeElemPath = (elem, targetFields = []) => {
18
+ const result = { path: undefined, nearestFields: {}, hasButton: false, hasLink: false }
19
+ if (!elem) return result
20
+ if (elem === window) { result.path = 'window'; return result }
21
+ if (elem === document) { result.path = 'document'; return result }
29
22
 
30
23
  let pathSelector = ''
31
- let index = getNthOfTypeIndex(elem)
24
+ const index = getNthOfTypeIndex(elem)
32
25
 
33
- const nearestFields = {}
34
26
  try {
35
27
  while (elem?.tagName) {
36
- const { id, localName } = elem
37
- targetFields.forEach(field => { nearestFields[nearestAttrName(field)] ||= (elem[field]?.baseVal || elem[field]) })
38
- const selector = [
39
- localName,
40
- id ? `#${id}` : '',
41
- pathSelector ? `>${pathSelector}` : ''
42
- ].join('')
43
-
44
- pathSelector = selector
28
+ const tagName = elem.tagName.toLowerCase()
29
+ result.hasLink ||= tagName === 'a'
30
+ result.hasButton ||= tagName === 'button' || (tagName === 'input' && elem.type.toLowerCase() === 'button')
31
+
32
+ targetFields.forEach(field => { result.nearestFields[nearestAttrName(field)] ||= (elem[field]?.baseVal || elem[field]) })
33
+ pathSelector = buildPathSelector(elem, pathSelector)
45
34
  elem = elem.parentNode
46
35
  }
47
36
  } catch (err) {
48
- // do nothing for now
37
+ // do nothing for now
49
38
  }
50
39
 
51
- const path = pathSelector ? index ? `${pathSelector}:nth-of-type(${index})` : pathSelector : undefined
52
- return { path, nearestFields }
40
+ result.path = pathSelector ? index ? `${pathSelector}:nth-of-type(${index})` : pathSelector : undefined
41
+ return result
42
+ }
43
+
44
+ function buildPathSelector (elem, pathSelector) {
45
+ const { id, localName } = elem
46
+ return [
47
+ localName,
48
+ id ? `#${id}` : '',
49
+ pathSelector ? `>${pathSelector}` : ''
50
+ ].join('')
51
+ }
53
52
 
54
- function nearestAttrName (originalFieldName) {
55
- /** preserve original renaming structure for pre-existing field maps */
56
- if (originalFieldName === 'tagName') originalFieldName = 'tag'
57
- if (originalFieldName === 'className') originalFieldName = 'class'
58
- return `nearest${originalFieldName.charAt(0).toUpperCase() + originalFieldName.slice(1)}`
53
+ function getNthOfTypeIndex (node) {
54
+ try {
55
+ let i = 1
56
+ const { tagName } = node
57
+ while (node.previousElementSibling) {
58
+ if (node.previousElementSibling.tagName === tagName) i++
59
+ node = node.previousElementSibling
60
+ }
61
+ return i
62
+ } catch (err) {
63
+ // do nothing for now. An invalid child count will make the path selector not return a nth-of-type selector statement
59
64
  }
60
65
  }
66
+
67
+ function nearestAttrName (originalFieldName) {
68
+ /** preserve original renaming structure for pre-existing field maps */
69
+ if (originalFieldName === 'tagName') originalFieldName = 'tag'
70
+ if (originalFieldName === 'className') originalFieldName = 'class'
71
+ return `nearest${originalFieldName.charAt(0).toUpperCase() + originalFieldName.slice(1)}`
72
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Copyright 2020-2025 New Relic, Inc. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { globalScope } from '../constants/runtime'
6
+ import { gosNREUMOriginals } from '../window/nreum'
7
+
8
+ /**
9
+ * Extracts a URL from various target types.
10
+ * @param {string|Request|URL} target - The target to extract the URL from. It can be a string, a Fetch Request object, or a URL object.
11
+ * @returns {string|undefined} The extracted URL as a string, or undefined if the target type is not supported.
12
+ */
13
+ export function extractUrl (target) {
14
+ if (typeof target === 'string') return target
15
+ else if (target instanceof gosNREUMOriginals().o.REQ) return target.url
16
+ else if (globalScope?.URL && target instanceof URL) return target.href
17
+ }
@@ -19,6 +19,7 @@ import { FEATURE_NAMES } from '../../../loaders/features/features'
19
19
  import { SUPPORTABILITY_METRIC } from '../../metrics/constants'
20
20
  import { now } from '../../../common/timing/now'
21
21
  import { hasUndefinedHostname } from '../../../common/deny-list/deny-list'
22
+ import { extractUrl } from '../../../common/url/extract-url'
22
23
 
23
24
  var handlers = ['load', 'error', 'abort', 'timeout']
24
25
  var handlersLen = handlers.length
@@ -318,16 +319,7 @@ function subscribeToEvents (agentRef, ee, handler, dt) {
318
319
 
319
320
  var opts = this.opts || {}
320
321
  var target = this.target
321
-
322
- var url
323
- if (typeof target === 'string') {
324
- url = target
325
- } else if (typeof target === 'object' && target instanceof origRequest) {
326
- url = target.url
327
- } else if (globalScope?.URL && typeof target === 'object' && target instanceof URL) {
328
- url = target.href
329
- }
330
- addUrl(this, url)
322
+ addUrl(this, extractUrl(target))
331
323
 
332
324
  var method = ('' + ((target && target instanceof origRequest && target.method) ||
333
325
  opts.method || 'GET')).toUpperCase()
@@ -17,6 +17,8 @@ import { isPureObject } from '../../../common/util/type-check'
17
17
 
18
18
  export class Aggregate extends AggregateBase {
19
19
  static featureName = FEATURE_NAME
20
+ #userActionAggregator
21
+
20
22
  constructor (agentRef) {
21
23
  super(agentRef, FEATURE_NAME)
22
24
  this.referrerUrl = (isBrowserScope && document.referrer) ? cleanURL(document.referrer) : undefined
@@ -28,7 +30,7 @@ export class Aggregate extends AggregateBase {
28
30
  return
29
31
  }
30
32
 
31
- this.trackSupportabilityMetrics()
33
+ this.#trackSupportabilityMetrics()
32
34
 
33
35
  registerHandler('api-recordCustomEvent', (timestamp, eventType, attributes) => {
34
36
  if (RESERVED_EVENT_TYPES.includes(eventType)) return warn(46)
@@ -59,8 +61,8 @@ export class Aggregate extends AggregateBase {
59
61
 
60
62
  let addUserAction = () => { /** no-op */ }
61
63
  if (isBrowserScope && agentRef.init.user_actions.enabled) {
62
- this.userActionAggregator = new UserActionsAggregator()
63
- this.harvestOpts.beforeUnload = () => addUserAction?.(this.userActionAggregator.aggregationEvent)
64
+ this.#userActionAggregator = new UserActionsAggregator(agentRef.init.feature_flags.includes('user_frustrations'))
65
+ this.harvestOpts.beforeUnload = () => addUserAction?.(this.#userActionAggregator.aggregationEvent)
64
66
 
65
67
  addUserAction = (aggregatedUserAction) => {
66
68
  try {
@@ -68,7 +70,7 @@ export class Aggregate extends AggregateBase {
68
70
  * so we still need to validate that an event was given to this method before we try to add */
69
71
  if (aggregatedUserAction?.event) {
70
72
  const { target, timeStamp, type } = aggregatedUserAction.event
71
- this.addEvent({
73
+ const userActionEvent = {
72
74
  eventType: 'UserAction',
73
75
  timestamp: this.toEpoch(timeStamp),
74
76
  action: type,
@@ -84,8 +86,12 @@ export class Aggregate extends AggregateBase {
84
86
  if (canTrustTargetAttribute(field)) acc[targetAttrName(field)] = String(target[field]).trim().slice(0, 128)
85
87
  return acc
86
88
  }, {})),
87
- ...aggregatedUserAction.nearestTargetFields
88
- })
89
+ ...aggregatedUserAction.nearestTargetFields,
90
+ ...(aggregatedUserAction.deadClick && { deadClick: true }),
91
+ ...(aggregatedUserAction.errorClick && { errorClick: true })
92
+ }
93
+ this.addEvent(userActionEvent)
94
+ this.#trackUserActionSM(userActionEvent)
89
95
 
90
96
  /**
91
97
  * Returns the original target field name with `target` prepended and camelCased
@@ -116,8 +122,11 @@ export class Aggregate extends AggregateBase {
116
122
 
117
123
  registerHandler('ua', (evt) => {
118
124
  /** the processor will return the previously aggregated event if it has been completed by processing the current event */
119
- addUserAction(this.userActionAggregator.process(evt, this.agentRef.init.user_actions.elementAttributes))
125
+ addUserAction(this.#userActionAggregator.process(evt, this.agentRef.init.user_actions.elementAttributes))
120
126
  }, this.featureName, this.ee)
127
+ registerHandler('navChange', () => { this.#userActionAggregator.isLiveClick() }, this.featureName, this.ee)
128
+ registerHandler('uaXhr', () => { this.#userActionAggregator.isLiveClick() }, this.featureName, this.ee)
129
+ registerHandler('uaErr', () => this.#userActionAggregator.markAsErrorClick(), this.featureName, this.ee)
121
130
  }
122
131
 
123
132
  /**
@@ -296,7 +305,7 @@ export class Aggregate extends AggregateBase {
296
305
  return Math.floor(this.agentRef.runtime.timeKeeper.correctRelativeTimestamp(timestamp))
297
306
  }
298
307
 
299
- trackSupportabilityMetrics () {
308
+ #trackSupportabilityMetrics () {
300
309
  /** track usage SMs to improve these experimental features */
301
310
  const configPerfTag = 'Config/Performance/'
302
311
  if (this.agentRef.init.performance.capture_marks) this.reportSupportabilityMetric(configPerfTag + 'CaptureMarks/Enabled')
@@ -306,4 +315,10 @@ export class Aggregate extends AggregateBase {
306
315
  if (this.agentRef.init.performance.resources.first_party_domains?.length !== 0) this.reportSupportabilityMetric(configPerfTag + 'Resources/FirstPartyDomains/Changed')
307
316
  if (this.agentRef.init.performance.resources.ignore_newrelic === false) this.reportSupportabilityMetric(configPerfTag + 'Resources/IgnoreNewrelic/Changed')
308
317
  }
318
+
319
+ #trackUserActionSM (ua) {
320
+ if (ua.rageClick) this.reportSupportabilityMetric('UserAction/RageClick/Seen')
321
+ if (ua.deadClick) this.reportSupportabilityMetric('UserAction/DeadClick/Seen')
322
+ if (ua.errorClick) this.reportSupportabilityMetric('UserAction/ErrorClick/Seen')
323
+ }
309
324
  }
@@ -6,15 +6,17 @@ import { RAGE_CLICK_THRESHOLD_EVENTS, RAGE_CLICK_THRESHOLD_MS } from '../../cons
6
6
  import { cleanURL } from '../../../../common/url/clean-url'
7
7
 
8
8
  export class AggregatedUserAction {
9
- constructor (evt, selectorPath, nearestTargetFields) {
9
+ constructor (evt, selectorInfo) {
10
10
  this.event = evt
11
11
  this.count = 1
12
12
  this.originMs = Math.floor(evt.timeStamp)
13
13
  this.relativeMs = [0]
14
- this.selectorPath = selectorPath
14
+ this.selectorPath = selectorInfo.path
15
15
  this.rageClick = undefined
16
- this.nearestTargetFields = nearestTargetFields
16
+ this.nearestTargetFields = selectorInfo.nearestFields
17
17
  this.currentUrl = cleanURL('' + location)
18
+ this.deadClick = false
19
+ this.errorClick = false
18
20
  }
19
21
 
20
22
  /**
@@ -2,14 +2,27 @@
2
2
  * Copyright 2020-2025 New Relic, Inc. All rights reserved.
3
3
  * SPDX-License-Identifier: Apache-2.0
4
4
  */
5
- import { generateSelectorPath } from '../../../../common/dom/selector-path'
6
- import { OBSERVED_WINDOW_EVENTS } from '../../constants'
5
+ import { analyzeElemPath } from '../../../../common/dom/selector-path'
6
+ import { FRUSTRATION_TIMEOUT_MS, OBSERVED_WINDOW_EVENTS } from '../../constants'
7
7
  import { AggregatedUserAction } from './aggregated-user-action'
8
+ import { Timer } from '../../../../common/timer/timer'
9
+ import { gosNREUMOriginals } from '../../../../common/window/nreum'
8
10
 
9
11
  export class UserActionsAggregator {
10
12
  /** @type {AggregatedUserAction=} */
11
13
  #aggregationEvent = undefined
12
14
  #aggregationKey = ''
15
+ #ufEnabled = false
16
+ #deadClickTimer = undefined
17
+ #domObserver = undefined
18
+ #errorClickTimer = undefined
19
+
20
+ constructor (userFrustrationsEnabled) {
21
+ if (userFrustrationsEnabled && gosNREUMOriginals().o.MO) {
22
+ this.#domObserver = new MutationObserver(this.isLiveClick.bind(this))
23
+ this.#ufEnabled = true
24
+ }
25
+ }
13
26
 
14
27
  get aggregationEvent () {
15
28
  // if this is accessed externally, we need to be done aggregating on it
@@ -28,40 +41,83 @@ export class UserActionsAggregator {
28
41
  */
29
42
  process (evt, targetFields) {
30
43
  if (!evt) return
31
- const { selectorPath, nearestTargetFields } = getSelectorPath(evt, targetFields)
32
- const aggregationKey = getAggregationKey(evt, selectorPath)
44
+ const targetElem = OBSERVED_WINDOW_EVENTS.includes(evt.type) ? window : evt.target
45
+ const selectorInfo = analyzeElemPath(targetElem, targetFields)
46
+
47
+ // if selectorInfo.path is undefined, aggregation will be skipped for this event
48
+ const aggregationKey = getAggregationKey(evt, selectorInfo.path)
33
49
  if (!!aggregationKey && aggregationKey === this.#aggregationKey) {
34
50
  // an aggregation exists already, so lets just continue to increment
35
51
  this.#aggregationEvent.aggregate(evt)
36
52
  } else {
37
53
  // return the prev existing one (if there is one)
38
54
  const finishedEvent = this.#aggregationEvent
39
- // then set as this new event aggregation
55
+ if (this.#ufEnabled) {
56
+ this.#deadClickCleanup()
57
+ this.#errorClickCleanup()
58
+ }
59
+
60
+ // then start new event aggregation
40
61
  this.#aggregationKey = aggregationKey
41
- this.#aggregationEvent = new AggregatedUserAction(evt, selectorPath, nearestTargetFields)
62
+ this.#aggregationEvent = new AggregatedUserAction(evt, selectorInfo)
63
+ if (this.#ufEnabled && evt.type === 'click' && (selectorInfo.hasButton || selectorInfo.hasLink)) {
64
+ this.#deadClickSetup(this.#aggregationEvent)
65
+ this.#errorClickSetup()
66
+ }
42
67
  return finishedEvent
43
68
  }
44
69
  }
45
- }
46
70
 
47
- /**
48
- * Generates a selector path for the event, starting with simple cases like window or document and getting more complex for dom-tree traversals as needed.
49
- * Will return a random selector path value if no other path can be determined, to force the aggregator to skip aggregation for this event.
50
- * @param {Event} evt
51
- * @returns {string}
52
- */
53
- function getSelectorPath (evt, targetFields) {
54
- let selectorPath; let nearestTargetFields = {}
55
- if (OBSERVED_WINDOW_EVENTS.includes(evt.type) || evt.target === window) selectorPath = 'window'
56
- else if (evt.target === document) selectorPath = 'document'
57
- // if still no selectorPath, generate one from target tree that includes elem ids
58
- else {
59
- const { path, nearestFields } = generateSelectorPath(evt.target, targetFields)
60
- selectorPath = path
61
- nearestTargetFields = nearestFields
71
+ markAsErrorClick () {
72
+ if (this.#aggregationEvent && this.#errorClickTimer) {
73
+ this.#aggregationEvent.errorClick = true
74
+ this.#errorClickCleanup()
75
+ }
76
+ }
77
+
78
+ #errorClickSetup () {
79
+ this.#errorClickTimer = new Timer({
80
+ onEnd: () => {
81
+ this.#errorClickCleanup()
82
+ }
83
+ }, FRUSTRATION_TIMEOUT_MS)
84
+ }
85
+
86
+ #errorClickCleanup () {
87
+ this.#errorClickTimer?.clear()
88
+ this.#errorClickTimer = undefined
89
+ }
90
+
91
+ #deadClickSetup (userAction) {
92
+ if (this.#isEvaluatingDeadClick() || !this.#domObserver) return
93
+
94
+ this.#domObserver.observe(document, {
95
+ attributes: true,
96
+ characterData: true,
97
+ childList: true,
98
+ subtree: true
99
+ })
100
+ this.#deadClickTimer = new Timer({
101
+ onEnd: () => {
102
+ userAction.deadClick = true
103
+ this.#deadClickCleanup()
104
+ }
105
+ }, FRUSTRATION_TIMEOUT_MS)
106
+ }
107
+
108
+ #deadClickCleanup () {
109
+ this.#domObserver?.disconnect()
110
+ this.#deadClickTimer?.clear()
111
+ this.#deadClickTimer = undefined
112
+ }
113
+
114
+ #isEvaluatingDeadClick () {
115
+ return this.#deadClickTimer !== undefined
116
+ }
117
+
118
+ isLiveClick () {
119
+ if (this.#isEvaluatingDeadClick()) this.#deadClickCleanup()
62
120
  }
63
- // if STILL no selectorPath, it will return undefined which will skip aggregation for this event
64
- return { selectorPath, nearestTargetFields }
65
121
  }
66
122
 
67
123
  /**
@@ -12,6 +12,8 @@ export const OBSERVED_WINDOW_EVENTS = ['focus', 'blur']
12
12
  export const RAGE_CLICK_THRESHOLD_EVENTS = 4
13
13
  export const RAGE_CLICK_THRESHOLD_MS = 1000
14
14
 
15
+ export const FRUSTRATION_TIMEOUT_MS = 2000
16
+
15
17
  export const RESERVED_EVENT_TYPES = ['PageAction', 'UserAction', 'BrowserPerformance']
16
18
 
17
19
  export const FEATURE_FLAGS = {
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { globalScope, isBrowserScope } from '../../../common/constants/runtime'
7
7
  import { handle } from '../../../common/event-emitter/handle'
8
- import { windowAddEventListener } from '../../../common/event-listener/event-listener-opts'
8
+ import { eventListenerOpts, windowAddEventListener } from '../../../common/event-listener/event-listener-opts'
9
9
  import { debounce } from '../../../common/util/invoke'
10
10
  import { setupAddPageActionAPI } from '../../../loaders/api/addPageAction'
11
11
  import { setupFinishedAPI } from '../../../loaders/api/finished'
@@ -14,6 +14,12 @@ import { setupRegisterAPI } from '../../../loaders/api/register'
14
14
  import { setupMeasureAPI } from '../../../loaders/api/measure'
15
15
  import { InstrumentBase } from '../../utils/instrument-base'
16
16
  import { FEATURE_NAME, OBSERVED_EVENTS, OBSERVED_WINDOW_EVENTS } from '../constants'
17
+ import { FEATURE_NAMES } from '../../../loaders/features/features'
18
+ import { wrapHistory } from '../../../common/wrap/wrap-history'
19
+ import { wrapFetch } from '../../../common/wrap/wrap-fetch'
20
+ import { wrapXhr } from '../../../common/wrap/wrap-xhr'
21
+ import { parseUrl } from '../../../common/url/parse-url'
22
+ import { extractUrl } from '../../../common/url/extract-url'
17
23
 
18
24
  export class Instrument extends InstrumentBase {
19
25
  static featureName = FEATURE_NAME
@@ -55,6 +61,50 @@ export class Instrument extends InstrumentBase {
55
61
  })
56
62
  observer.observe({ type: 'resource', buffered: true })
57
63
  }
64
+
65
+ const historyEE = wrapHistory(this.ee)
66
+ historyEE.on('pushState-end', navigationChange)
67
+ historyEE.on('replaceState-end', navigationChange)
68
+ window.addEventListener('hashchange', navigationChange, eventListenerOpts(true, this.removeOnAbort?.signal))
69
+ window.addEventListener('popstate', navigationChange, eventListenerOpts(true, this.removeOnAbort?.signal))
70
+ function navigationChange () {
71
+ historyEE.emit('navChange')
72
+ }
73
+ }
74
+
75
+ try {
76
+ this.removeOnAbort = new AbortController()
77
+ } catch (e) {}
78
+
79
+ this.abortHandler = () => {
80
+ this.removeOnAbort?.abort()
81
+ this.abortHandler = undefined // weakly allow this abort op to run only once
82
+ }
83
+
84
+ globalScope.addEventListener('error', () => {
85
+ handle('uaErr', [], undefined, FEATURE_NAMES.genericEvents, this.ee)
86
+ }, eventListenerOpts(false, this.removeOnAbort?.signal))
87
+
88
+ wrapFetch(this.ee)
89
+ wrapXhr(this.ee)
90
+ this.ee.on('open-xhr-start', (args, xhr) => {
91
+ if (!isInternalTraffic(args[1])) {
92
+ xhr.addEventListener('readystatechange', () => {
93
+ if (xhr.readyState === 2) { // HEADERS_RECEIVED
94
+ handle('uaXhr', [], undefined, FEATURE_NAMES.genericEvents, this.ee)
95
+ }
96
+ })
97
+ }
98
+ })
99
+ this.ee.on('fetch-start', (fetchArguments) => {
100
+ if (fetchArguments.length >= 1 && !isInternalTraffic(extractUrl(fetchArguments[0]))) {
101
+ handle('uaXhr', [], undefined, FEATURE_NAMES.genericEvents, this.ee)
102
+ }
103
+ })
104
+
105
+ function isInternalTraffic (url) {
106
+ const parsedUrl = parseUrl(url)
107
+ return agentRef.beacons.includes(parsedUrl.hostname + ':' + parsedUrl.port)
58
108
  }
59
109
 
60
110
  /** If any of the sources are active, import the aggregator. otherwise deregister */
@@ -15,4 +15,5 @@ export function setupNoticeErrorAPI (agent) {
15
15
  export function noticeError (err, customAttributes, agentRef, targetEntityGuid, timestamp = now()) {
16
16
  if (typeof err === 'string') err = new Error(err)
17
17
  handle('err', [err, timestamp, false, customAttributes, agentRef.runtime.isRecording, undefined, targetEntityGuid], undefined, FEATURE_NAMES.jserrors, agentRef.ee)
18
+ handle('uaErr', [], undefined, FEATURE_NAMES.genericEvents, agentRef.ee)
18
19
  }
@@ -50,6 +50,7 @@ export function configure (agent, opts = {}, loaderType, forceDrain) {
50
50
  internalTrafficList.push(updatedInit.proxy.assets)
51
51
  }
52
52
  if (updatedInit.proxy.beacon) internalTrafficList.push(updatedInit.proxy.beacon)
53
+ agent.beacons = [...internalTrafficList]
53
54
 
54
55
  setTopLevelCallers(agent) // no need to set global APIs on newrelic obj more than once
55
56
  addToNREUM('activatedFeatures', activatedFeatures)