Skip to content

Commit 0d52acf

Browse files
feat: Report Page Actions with Generic Events Feature (#1124)
1 parent 9fbbba1 commit 0d52acf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+375
-215
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ const options = {
7878
The following features may be disabled by adding `init` entries as shown above. Note that the `page_view_event` feature may not be disabled.
7979

8080
- `ajax`
81+
- `generic_events`
8182
- `jserrors`
8283
- `metrics`
83-
- `page_action`
8484
- `page_view_timing`
8585
- `session_replay`
8686
- `session_trace`
@@ -125,7 +125,7 @@ The following feature modules are available for inclusion in the `features` arra
125125
import { Ajax } from '@newrelic/browser-agent/features/ajax';
126126
import { JSErrors } from '@newrelic/browser-agent/features/jserrors';
127127
import { Metrics } from '@newrelic/browser-agent/features/metrics';
128-
import { PageAction } from '@newrelic/browser-agent/features/page_action';
128+
import { GenericEvents } from '@newrelic/browser-agent/features/generic_events';
129129
import { PageViewEvent } from '@newrelic/browser-agent/features/page_view_event';
130130
import { PageViewTiming } from '@newrelic/browser-agent/features/page_view_timing';
131131
import { SessionTrace } from '@newrelic/browser-agent/features/session_trace';

docs/warning-codes.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,6 @@
8686
### 42
8787
`Failed to execute setApplicationVersion. Expected <String | null>`
8888
### 43
89-
`Agent not configured properly.`
89+
`Agent not configured properly.`
90+
### 44
91+
`Invalid object passed to generic event aggregate. Missing "eventType".`

src/cdn/experimental.js

-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ import { Instrument as InstrumentXhr } from '../features/ajax/instrument'
1818
import { Instrument as InstrumentSessionTrace } from '../features/session_trace/instrument'
1919
import { Instrument as InstrumentSessionReplay } from '../features/session_replay/instrument'
2020
import { Instrument as InstrumentGenericEvents } from '../features/generic_events/instrument'
21-
// import { Instrument as InstrumentSpa } from '../features/spa/instrument'
2221
import { Instrument as InstrumentSoftNav } from '../features/soft_navigations/instrument'
23-
import { Instrument as InstrumentPageAction } from '../features/page_action/instrument'
2422
import { Instrument as InstrumentLogs } from '../features/logging/instrument'
2523

2624
new Agent({
@@ -31,11 +29,9 @@ new Agent({
3129
InstrumentSessionTrace,
3230
InstrumentSessionReplay,
3331
InstrumentMetrics,
34-
InstrumentPageAction,
3532
InstrumentErrors,
3633
InstrumentGenericEvents,
3734
InstrumentLogs,
38-
// InstrumentSpa,
3935
InstrumentSoftNav
4036
],
4137
loaderType: 'experimental'

src/cdn/polyfills/pro.js

-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { Instrument as InstrumentMetrics } from '../../features/metrics/instrume
1212
import { Instrument as InstrumentErrors } from '../../features/jserrors/instrument'
1313
import { Instrument as InstrumentXhr } from '../../features/ajax/instrument'
1414
import { Instrument as InstrumentSessionTrace } from '../../features/session_trace/instrument'
15-
import { Instrument as InstrumentPageAction } from '../../features/page_action/instrument'
1615
import { Instrument as InstrumentGenericEvents } from '../../features/generic_events/instrument'
1716
import { Instrument as InstrumentLogs } from '../../features/logging/instrument'
1817

@@ -23,7 +22,6 @@ new Agent({
2322
InstrumentSessionTrace,
2423
InstrumentXhr,
2524
InstrumentMetrics,
26-
InstrumentPageAction,
2725
InstrumentErrors,
2826
InstrumentGenericEvents,
2927
InstrumentLogs

src/cdn/polyfills/spa.js

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { Instrument as InstrumentErrors } from '../../features/jserrors/instrume
1313
import { Instrument as InstrumentXhr } from '../../features/ajax/instrument'
1414
import { Instrument as InstrumentSessionTrace } from '../../features/session_trace/instrument'
1515
import { Instrument as InstrumentSpa } from '../../features/spa/instrument'
16-
import { Instrument as InstrumentPageAction } from '../../features/page_action/instrument'
1716
import { Instrument as InstrumentGenericEvents } from '../../features/generic_events/instrument'
1817
import { Instrument as InstrumentLogs } from '../../features/logging/instrument'
1918

@@ -24,7 +23,6 @@ new Agent({
2423
InstrumentPageViewTiming,
2524
InstrumentSessionTrace,
2625
InstrumentMetrics,
27-
InstrumentPageAction,
2826
InstrumentErrors,
2927
InstrumentGenericEvents,
3028
InstrumentLogs,

src/cdn/pro.js

-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { Instrument as InstrumentErrors } from '../features/jserrors/instrument'
1212
import { Instrument as InstrumentXhr } from '../features/ajax/instrument'
1313
import { Instrument as InstrumentSessionTrace } from '../features/session_trace/instrument'
1414
import { Instrument as InstrumentSessionReplay } from '../features/session_replay/instrument'
15-
import { Instrument as InstrumentPageAction } from '../features/page_action/instrument'
1615
import { Instrument as InstrumentGenericEvents } from '../features/generic_events/instrument'
1716
import { Instrument as InstrumentLogs } from '../features/logging/instrument'
1817

@@ -24,7 +23,6 @@ new Agent({
2423
InstrumentSessionReplay,
2524
InstrumentXhr,
2625
InstrumentMetrics,
27-
InstrumentPageAction,
2826
InstrumentErrors,
2927
InstrumentGenericEvents,
3028
InstrumentLogs

src/cdn/spa.js

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { Instrument as InstrumentSessionTrace } from '../features/session_trace/
1313
import { Instrument as InstrumentSessionReplay } from '../features/session_replay/instrument'
1414
import { Instrument as InstrumentSoftNav } from '../features/soft_navigations/instrument'
1515
import { Instrument as InstrumentSpa } from '../features/spa/instrument'
16-
import { Instrument as InstrumentPageAction } from '../features/page_action/instrument'
1716
import { Instrument as InstrumentGenericEvents } from '../features/generic_events/instrument'
1817
import { Instrument as InstrumentLogs } from '../features/logging/instrument'
1918

@@ -25,7 +24,6 @@ new Agent({
2524
InstrumentSessionTrace,
2625
InstrumentSessionReplay,
2726
InstrumentMetrics,
28-
InstrumentPageAction,
2927
InstrumentErrors,
3028
InstrumentGenericEvents,
3129
InstrumentLogs,

src/common/config/state/init.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ const model = () => {
4141
allowed_origins: undefined
4242
},
4343
feature_flags: [],
44-
generic_events: { enabled: true, harvestTimeSeconds: 10, autoStart: true },
44+
generic_events: { enabled: true, harvestTimeSeconds: 30, autoStart: true },
4545
harvest: { tooManyRequestsDelay: 60 },
4646
jserrors: { enabled: true, harvestTimeSeconds: 10, autoStart: true },
4747
logging: { enabled: true, harvestTimeSeconds: 10, autoStart: true, level: LOG_LEVELS.INFO },
4848
metrics: { enabled: true, autoStart: true },
4949
obfuscate: undefined,
50-
page_action: { enabled: true, harvestTimeSeconds: 30, autoStart: true },
50+
page_action: { enabled: true },
5151
page_view_event: { enabled: true, autoStart: true },
5252
page_view_timing: { enabled: true, harvestTimeSeconds: 30, long_task: false, autoStart: true },
5353
privacy: { cookies_enabled: true }, // *cli - per discussion, default should be true

src/features/generic_events/aggregate/index.js

+32-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { isBrowserScope } from '../../../common/constants/runtime'
1111
import { AggregateBase } from '../../utils/aggregate-base'
1212
import { warn } from '../../../common/util/console'
1313
import { now } from '../../../common/timing/now'
14+
import { registerHandler } from '../../../common/event-emitter/register-handler'
1415
import { deregisterDrain } from '../../../common/drain/drain'
1516

1617
export class Aggregate extends AggregateBase {
@@ -22,27 +23,42 @@ export class Aggregate extends AggregateBase {
2223
this.eventsPerHarvest = 1000
2324
this.harvestTimeSeconds = getConfigurationValue(this.agentIdentifier, 'generic_events.harvestTimeSeconds')
2425

25-
this.referrerUrl = undefined
26+
this.referrerUrl = (isBrowserScope && document.referrer) ? cleanURL(document.referrer) : undefined
2627
this.currentEvents = []
2728

2829
this.events = []
2930
this.overflow = []
3031

3132
this.#agentRuntime = getRuntime(this.agentIdentifier)
3233

33-
if (isBrowserScope && document.referrer) this.referrerUrl = cleanURL(document.referrer)
34-
3534
this.waitForFlags(['ins']).then(([ins]) => {
3635
if (!ins) {
3736
this.blocked = true
3837
deregisterDrain(this.agentIdentifier, this.featureName)
3938
return
4039
}
4140

42-
// handle page actions and other generic events here
41+
if (getConfigurationValue(this.agentIdentifier, 'page_action.enabled')) {
42+
registerHandler('api-addPageAction', (timestamp, name, attributes) => {
43+
this.addEvent({
44+
...attributes,
45+
eventType: 'PageAction',
46+
timestamp: this.#agentRuntime.timeKeeper.convertRelativeTimestamp(timestamp),
47+
timeSinceLoad: timestamp / 1000,
48+
actionName: name,
49+
referrerUrl: this.referrerUrl,
50+
currentUrl: cleanURL('' + location),
51+
...(isBrowserScope && {
52+
browserWidth: window.document.documentElement?.clientWidth,
53+
browserHeight: window.document.documentElement?.clientHeight
54+
})
55+
})
56+
}, this.featureName, this.ee)
57+
}
58+
4359
this.harvestScheduler = new HarvestScheduler('ins', { onFinished: (...args) => this.onHarvestFinished(...args) }, this)
4460
this.harvestScheduler.harvest.on('ins', (...args) => this.onHarvestStarted(...args))
45-
// this.harvestScheduler.startTimer(this.harvestTimeSeconds, 0)
61+
this.harvestScheduler.startTimer(this.harvestTimeSeconds, 0)
4662

4763
this.drain()
4864
})
@@ -79,30 +95,31 @@ export class Aggregate extends AggregateBase {
7995
addEvent (obj = {}) {
8096
if (!obj || !Object.keys(obj).length) return
8197
if (!obj.eventType) {
82-
warn('Invalid object passed to generic event aggregate. Missing "eventType".')
98+
warn(44)
8399
return
84100
}
85101

86102
for (let key in obj) {
87103
let val = obj[key]
88-
if (key === 'timestamp') val = this.#agentRuntime.timeKeeper.correctAbsoluteTimestamp(val)
89104
obj[key] = (val && typeof val === 'object' ? stringify(val) : val)
90105
}
91106

107+
const defaultEventAttributes = {
108+
/** should be overridden by the event-specific attributes, but just in case -- set it to now() */
109+
timestamp: this.#agentRuntime.timeKeeper.convertRelativeTimestamp(now()),
110+
/** all generic events require a pageUrl */
111+
pageUrl: cleanURL(getRuntime(this.agentIdentifier).origin)
112+
}
113+
92114
const eventAttributes = {
93115
/** Agent-level custom attributes */
94116
...(getInfo(this.agentIdentifier).jsAttributes || {}),
95-
/** Common attributes shared on all generic events */
96-
referrerUrl: this.referrerUrl,
97-
currentUrl: cleanURL('' + location),
98-
pageUrl: cleanURL(getRuntime(this.agentIdentifier).origin),
99-
/** Event-specific attributes take precedence over everything else */
117+
/** Fallbacks for required properties in-case the event did not supply them, should take precedence over agent-level custom attrs */
118+
...defaultEventAttributes,
119+
/** Event-specific attributes take precedence over agent-level custom attributes and fallbacks */
100120
...obj
101121
}
102122

103-
/** should have been provided by reporting feature -- but falls back to now if not */
104-
eventAttributes.timestamp ??= this.#agentRuntime.timeKeeper.convertRelativeTimestamp(now())
105-
106123
this.events.push(eventAttributes)
107124

108125
// check if we've reached the harvest limit...

src/features/generic_events/instrument/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
* SPDX-License-Identifier: Apache-2.0
33
*/
44

5+
import { getConfigurationValue } from '../../../common/config/config'
6+
import { deregisterDrain } from '../../../common/drain/drain'
57
import { InstrumentBase } from '../../utils/instrument-base'
68
import { FEATURE_NAME } from '../constants'
79

810
export class Instrument extends InstrumentBase {
911
static featureName = FEATURE_NAME
1012
constructor (agentIdentifier, aggregator, auto = true) {
1113
super(agentIdentifier, aggregator, FEATURE_NAME, auto)
12-
this.importAggregator()
14+
const genericEventSourceConfigs = [
15+
getConfigurationValue(this.agentIdentifier, 'page_action.enabled')
16+
// other future generic event source configs to go here, like M&Ms, PageResouce, etc.
17+
]
18+
/** If any of the sources are active, import the aggregator. otherwise deregister */
19+
if (genericEventSourceConfigs.some(x => x)) this.importAggregator()
20+
else deregisterDrain(this.agentIdentifier, this.featureName)
1321
}
1422
}

src/features/page_action/aggregate/index.js

-113
This file was deleted.

src/features/page_action/instrument/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { InstrumentBase } from '../../utils/instrument-base'
7-
import { FEATURE_NAME } from '../constants'
6+
import { GenericEvents } from '../../generic_events'
87

9-
export class Instrument extends InstrumentBase {
10-
static featureName = FEATURE_NAME
8+
/**
9+
* @deprecated This feature has been replaced by Generic Events. Use/Import `GenericEvents` instead. This wrapper will be removed in a future release
10+
*/
11+
export class Instrument extends GenericEvents {
1112
constructor (agentIdentifier, aggregator, auto = true) {
12-
super(agentIdentifier, aggregator, FEATURE_NAME, auto)
13-
this.importAggregator()
13+
super(agentIdentifier, aggregator, auto)
1414
}
1515
}

src/features/page_view_event/aggregate/initialized-features.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function getActivatedFeaturesFlags (agentId) {
1818
flagArr.push('xhr'); break
1919
case FEATURE_NAMES.jserrors:
2020
flagArr.push('err'); break
21-
case FEATURE_NAMES.pageAction:
21+
case FEATURE_NAMES.genericEvents:
2222
flagArr.push('ins'); break
2323
case FEATURE_NAMES.sessionTrace:
2424
flagArr.push('stn'); break

0 commit comments

Comments
 (0)