Skip to content

Commit

Permalink
support progress event with exact offset values
Browse files Browse the repository at this point in the history
  • Loading branch information
clarissedmn committed Jan 9, 2025
1 parent a019a76 commit 147c837
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/vast_tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class VASTTracker extends EventEmitter {
for (let i = this.lastPercentage; i < percent; i++) {
events.push(`progress-${i + 1}%`);
}
events.push(`progress-${Math.round(progress)}`);
events.push(`progress-${progress}`);
for (const quartile in this.quartiles) {
if (
this.isQuartileReached(quartile, this.quartiles[quartile], progress)
Expand Down Expand Up @@ -778,6 +778,31 @@ export class VASTTracker extends EventEmitter {
}
}

/**
* Calls the tracking URLs for progress events for the given eventName and emits the event.
*
* @param {String} eventName - The name of the event.
* @param macros - An optional Object of parameters(vast macros) to be used in the tracking calls.
* @param once - Boolean to define if the event has to be tracked only once.
*/
trackProgressEvents(eventName, macros, once) {
const eventTime = parseFloat(eventName.split('-')[1]);

const progressEvents = Object.entries(this.trackingEvents)
.filter(([key]) => key.startsWith('progress-'))
.map(([key, value]) => ({ name: key, time: parseFloat(key.split('-')[1]), urls: value }))
.filter(({ time }) => time <= eventTime);

progressEvents.forEach(({ name, urls }) => {
this.emit(name, { trackingURLTemplates: urls });
this.trackURLs(urls, macros);

if (once) {
delete this.trackingEvents[name];
}
});
}

/**
* Calls the tracking URLs for the given eventName and emits the event.
*
Expand All @@ -804,6 +829,10 @@ export class VASTTracker extends EventEmitter {
eventName = 'close';
}

if (eventName.startsWith('progress-')) {
this.trackProgressEvents(eventName, macros, once);
}

const trackingURLTemplates = this.trackingEvents[eventName];
const isAlwaysEmitEvent = this.emitAlwaysEvents.indexOf(eventName) > -1;

Expand Down

0 comments on commit 147c837

Please sign in to comment.