@@ -11,6 +11,7 @@ import { isBrowserScope } from '../../../common/constants/runtime'
11
11
import { AggregateBase } from '../../utils/aggregate-base'
12
12
import { warn } from '../../../common/util/console'
13
13
import { now } from '../../../common/timing/now'
14
+ import { registerHandler } from '../../../common/event-emitter/register-handler'
14
15
import { deregisterDrain } from '../../../common/drain/drain'
15
16
16
17
export class Aggregate extends AggregateBase {
@@ -22,27 +23,42 @@ export class Aggregate extends AggregateBase {
22
23
this . eventsPerHarvest = 1000
23
24
this . harvestTimeSeconds = getConfigurationValue ( this . agentIdentifier , 'generic_events.harvestTimeSeconds' )
24
25
25
- this . referrerUrl = undefined
26
+ this . referrerUrl = ( isBrowserScope && document . referrer ) ? cleanURL ( document . referrer ) : undefined
26
27
this . currentEvents = [ ]
27
28
28
29
this . events = [ ]
29
30
this . overflow = [ ]
30
31
31
32
this . #agentRuntime = getRuntime ( this . agentIdentifier )
32
33
33
- if ( isBrowserScope && document . referrer ) this . referrerUrl = cleanURL ( document . referrer )
34
-
35
34
this . waitForFlags ( [ 'ins' ] ) . then ( ( [ ins ] ) => {
36
35
if ( ! ins ) {
37
36
this . blocked = true
38
37
deregisterDrain ( this . agentIdentifier , this . featureName )
39
38
return
40
39
}
41
40
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
+
43
59
this . harvestScheduler = new HarvestScheduler ( 'ins' , { onFinished : ( ...args ) => this . onHarvestFinished ( ...args ) } , this )
44
60
this . harvestScheduler . harvest . on ( 'ins' , ( ...args ) => this . onHarvestStarted ( ...args ) )
45
- // this.harvestScheduler.startTimer(this.harvestTimeSeconds, 0)
61
+ this . harvestScheduler . startTimer ( this . harvestTimeSeconds , 0 )
46
62
47
63
this . drain ( )
48
64
} )
@@ -79,30 +95,31 @@ export class Aggregate extends AggregateBase {
79
95
addEvent ( obj = { } ) {
80
96
if ( ! obj || ! Object . keys ( obj ) . length ) return
81
97
if ( ! obj . eventType ) {
82
- warn ( 'Invalid object passed to generic event aggregate. Missing "eventType".' )
98
+ warn ( 44 )
83
99
return
84
100
}
85
101
86
102
for ( let key in obj ) {
87
103
let val = obj [ key ]
88
- if ( key === 'timestamp' ) val = this . #agentRuntime. timeKeeper . correctAbsoluteTimestamp ( val )
89
104
obj [ key ] = ( val && typeof val === 'object' ? stringify ( val ) : val )
90
105
}
91
106
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
+
92
114
const eventAttributes = {
93
115
/** Agent-level custom attributes */
94
116
...( 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 */
100
120
...obj
101
121
}
102
122
103
- /** should have been provided by reporting feature -- but falls back to now if not */
104
- eventAttributes . timestamp ??= this . #agentRuntime. timeKeeper . convertRelativeTimestamp ( now ( ) )
105
-
106
123
this . events . push ( eventAttributes )
107
124
108
125
// check if we've reached the harvest limit...
0 commit comments