From 147c8373b632bc195bdffd687257c5c76be29e20 Mon Sep 17 00:00:00 2001 From: Clarisse Damon Date: Thu, 9 Jan 2025 15:09:23 +0100 Subject: [PATCH] support progress event with exact offset values --- src/vast_tracker.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/vast_tracker.js b/src/vast_tracker.js index 8f144976..dfa58518 100644 --- a/src/vast_tracker.js +++ b/src/vast_tracker.js @@ -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) @@ -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. * @@ -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;