Skip to content

Commit

Permalink
fix harvesting
Browse files Browse the repository at this point in the history
  • Loading branch information
metal-messiah committed Feb 3, 2025
1 parent d9e4ff7 commit a065270
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
33 changes: 24 additions & 9 deletions src/features/generic_events/aggregate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ export class Aggregate extends AggregateBase {
}

const { userJourneyPaths, userJourneyTimestamps } = agentRef.runtime.session.read()
const splitPaths = userJourneyPaths?.split('>')
const splitPaths = userJourneyPaths?.split('>').filter(x => x)

function pad (num, chars) {
var s = '000' + num
return s.slice(s.length - chars)
}

this.userJourney = {
needsHarvest: true,
host: globalScope.location?.host,
paths: userJourneyPaths,
timestamps: userJourneyTimestamps,
Expand Down Expand Up @@ -80,16 +81,18 @@ export class Aggregate extends AggregateBase {

this.userJourney[pad(this.userJourney.navs++, 4)] = (pathname || '/') + hash

this.userJourney.needsHarvest = true

this.syncWithSessionManager({ userJourneyPaths: this.userJourney.paths, userJourneyTimestamps: this.userJourney.timestamps })
}, this.featureName, this.ee)
this.beforeUnloadFns.push(() => {
if (!this.userJourney.paths) return
const { paths, ...rest } = this.userJourney
this.addEvent({
eventType: 'SessionMetadata',
...rest
}, true, true)
})
// this.beforeUnloadFns.push(() => {
// if (!this.userJourney.paths) return
// const { paths, ...rest } = this.userJourney
// this.addEvent({
// eventType: 'SessionMetadata',
// ...rest
// }, true, true)
// })

registerHandler('api-recordCustomEvent', (timestamp, eventType, attributes) => {
if (RESERVED_EVENT_TYPES.includes(eventType)) return warn(46)
Expand Down Expand Up @@ -267,6 +270,18 @@ export class Aggregate extends AggregateBase {
})
}

/** hi-jacking this method for the POC to inject the userJourney aggregation */
preHarvestChecks (opts) {
const { paths, needsHarvest, ...rest } = this.userJourney
if (!paths || !needsHarvest) return true
this.addEvent({
eventType: 'SessionMetadata',
...rest
}, true, true)
this.userJourney.needsHarvest = false
return true
}

// WARNING: Insights times are in seconds. EXCEPT timestamp, which is in ms.
/** Some keys are set by the query params or request headers sent with the harvest and override the body values, so check those before adding new standard body values...
* see harvest.js#baseQueryString for more info on the query params
Expand Down
2 changes: 1 addition & 1 deletion src/features/utils/aggregate-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export class AggregateBase extends FeatureBase {
* @returns {Array} Final payload tagged with their targeting browser app. The value of `payload` can be undefined if there are no pending events for an app. This should be a minimum length of 1.
*/
makeHarvestPayload (shouldRetryOnFail = false, opts = {}) {
if (this.events.isEmpty(this.harvestOpts, opts.target)) return
// Other conditions and things to do when preparing harvest that is required.
if (this.preHarvestChecks && !this.preHarvestChecks(opts)) return
if (this.events.isEmpty(this.harvestOpts, opts.target)) return

if (shouldRetryOnFail) this.events.save(this.harvestOpts, opts.target)
const returnedDataArr = this.events.get(this.harvestOpts, opts.target)
Expand Down

0 comments on commit a065270

Please sign in to comment.