Skip to content

Commit

Permalink
fix unload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
metal-messiah committed Sep 28, 2024
1 parent 5db3762 commit 44bde9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/features/generic_events/aggregate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class Aggregate extends AggregateBase {
super(agentIdentifier, aggregator, FEATURE_NAME)
const agentInit = getConfiguration(this.agentIdentifier)

this.shouldRunUserActions = isBrowserScope && agentInit.user_actions.enabled

this.eventsPerHarvest = 1000
this.harvestTimeSeconds = agentInit.generic_events.harvestTimeSeconds

Expand All @@ -44,6 +46,8 @@ export class Aggregate extends AggregateBase {
return
}

const preHarvestMethods = []

if (agentInit.page_action.enabled) {
registerHandler('api-addPageAction', (timestamp, name, attributes) => {
this.addEvent({
Expand All @@ -60,7 +64,7 @@ export class Aggregate extends AggregateBase {
}, this.featureName, this.ee)
}

if (isBrowserScope && agentInit.user_actions.enabled) {
if (this.shouldRunUserActions) {
this.userActionAggregator = new UserActionsAggregator()

this.addUserAction = (aggregatedUserAction) => {
Expand Down Expand Up @@ -94,10 +98,19 @@ export class Aggregate extends AggregateBase {
/** the processor will return the previously aggregated event if it has been completed by processing the current event */
this.addUserAction(this.userActionAggregator.process(evt))
}, this.featureName, this.ee)

preHarvestMethods.push((options = {}) => {
/** send whatever UserActions have been aggregated up to this point
* if we are in a final harvest. By accessing the aggregationEvent, the aggregation is then force-cleared */
if (options.isFinalHarvest) this.addUserAction(this.userActionAggregator.aggregationEvent)
})
}

this.harvestScheduler = new HarvestScheduler('ins', { onFinished: (...args) => this.onHarvestFinished(...args) }, this)
this.harvestScheduler.harvest.on('ins', (...args) => this.onHarvestStarted(...args))
this.harvestScheduler.harvest.on('ins', (...args) => {
preHarvestMethods.forEach(fn => fn(...args))
this.onHarvestStarted(...args)
})
this.harvestScheduler.startTimer(this.harvestTimeSeconds, 0)

this.drain()
Expand Down Expand Up @@ -141,9 +154,6 @@ export class Aggregate extends AggregateBase {
}

onHarvestStarted (options) {
/** send whatever UserActions have been aggregated up to this point
* if we are in a final harvest. By accessing the aggregationEvent, the aggregation is then force-cleared */
if (options.isFinalHarvest) this.addUserAction(this.userActionAggregator.aggregationEvent)
const { userAttributes, atts } = getInfo(this.agentIdentifier)
if (!this.events.hasData) return
var payload = ({
Expand Down
6 changes: 6 additions & 0 deletions tests/components/generic_events/aggregate/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ describe('sub-features', () => {
const target = document.createElement('button')
target.id = 'myBtn'
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 123456, type: 'click', target }])
// blur event to trigger aggregation to stop and add to harvest buffer
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 234567, type: 'blur', target: window }])

const harvest = genericEventsAggregate.onHarvestStarted({ isFinalHarvest: true }) // force it to put the aggregation into the event buffer
expect(harvest.body.ins[0]).toMatchObject({
Expand All @@ -175,6 +177,8 @@ describe('sub-features', () => {
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 400, type: 'click', target }])
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 500, type: 'click', target }])
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 600, type: 'click', target }])
// blur event to trigger aggregation to stop and add to harvest buffer
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 234567, type: 'blur', target: window }])

const harvest = genericEventsAggregate.onHarvestStarted({ isFinalHarvest: true }) // force it to put the aggregation into the event buffer
expect(harvest.body.ins[0]).toMatchObject({
Expand All @@ -200,6 +204,8 @@ describe('sub-features', () => {
/** even though target1 and target2 have the same tag (button) and id (myBtn), it should still NOT aggregate them because they have different nth-of-type paths */
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 100, type: 'click', target }])
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 200, type: 'click', target: target2 }])
// blur event to trigger aggregation to stop and add to harvest buffer
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 234567, type: 'blur', target: window }])

const harvest = genericEventsAggregate.onHarvestStarted({ isFinalHarvest: true }) // force it to put the aggregation into the event buffer
expect(harvest.body.ins[0]).toMatchObject({
Expand Down

0 comments on commit 44bde9b

Please sign in to comment.