Skip to content

Commit aa50487

Browse files
committed
support progress event with exact offset values
1 parent a019a76 commit aa50487

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/vast_tracker.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class VASTTracker extends EventEmitter {
3838
this.impressed = false;
3939
this.skippable = false;
4040
this.trackingEvents = {};
41+
this.trackedProgressEvents = [];
4142
// We need to keep the last percentage of the tracker in order to
4243
// calculate to trigger the events when the VAST duration is short
4344
this.lastPercentage = 0;
@@ -211,7 +212,7 @@ export class VASTTracker extends EventEmitter {
211212
for (let i = this.lastPercentage; i < percent; i++) {
212213
events.push(`progress-${i + 1}%`);
213214
}
214-
events.push(`progress-${Math.round(progress)}`);
215+
events.push(`progress-${progress}`);
215216
for (const quartile in this.quartiles) {
216217
if (
217218
this.isQuartileReached(quartile, this.quartiles[quartile], progress)
@@ -228,6 +229,9 @@ export class VASTTracker extends EventEmitter {
228229

229230
if (progress < this.progress) {
230231
this.track('rewind', { macros });
232+
if (this.trackedProgressEvents) {
233+
this.trackedProgressEvents.splice(0);
234+
}
231235
}
232236
}
233237

@@ -778,12 +782,42 @@ export class VASTTracker extends EventEmitter {
778782
}
779783
}
780784

785+
/**
786+
* Calls the tracking URLs for progress events for the given eventName and emits the event.
787+
*
788+
* @param {String} eventName - The name of the event.
789+
* @param macros - An optional Object of parameters (vast macros) to be used in the tracking calls.
790+
* @param once - Boolean to define if the event has to be tracked only once.
791+
*/
792+
trackProgressEvents(eventName, macros, once) {
793+
const eventTime = parseFloat(eventName.split('-')[1]);
794+
795+
const progressEvents = Object.entries(this.trackingEvents)
796+
.filter(([key]) => key.startsWith('progress-'))
797+
.map(([key, value]) => ({ name: key, time: parseFloat(key.split('-')[1]), urls: value }))
798+
.filter(({ time }) => time <= eventTime && time > this.progress);
799+
800+
progressEvents.forEach(({ name, urls }) => {
801+
if (!once && this.trackedProgressEvents.includes(name)) {
802+
return;
803+
}
804+
this.emit(name, { trackingURLTemplates: urls });
805+
this.trackURLs(urls, macros);
806+
807+
if (once) {
808+
delete this.trackingEvents[name];
809+
} else {
810+
this.trackedProgressEvents.push(name);
811+
}
812+
});
813+
}
814+
781815
/**
782816
* Calls the tracking URLs for the given eventName and emits the event.
783817
*
784818
* @param {String} eventName - The name of the event.
785819
* @param {Object} options
786-
* @param {Object} [options.macros={}] - An optional Object of parameters(vast macros) to be used in the tracking calls.
820+
* @param {Object} [options.macros={}] - An optional Object of parameters (vast macros) to be used in the tracking calls.
787821
* @param {Boolean} [options.once=false] - Boolean to define if the event has to be tracked only once.
788822
*
789823
*/
@@ -804,6 +838,10 @@ export class VASTTracker extends EventEmitter {
804838
eventName = 'close';
805839
}
806840

841+
if (eventName.startsWith('progress-') && !eventName.endsWith("%")) {
842+
this.trackProgressEvents(eventName, macros, once);
843+
}
844+
807845
const trackingURLTemplates = this.trackingEvents[eventName];
808846
const isAlwaysEmitEvent = this.emitAlwaysEvents.indexOf(eventName) > -1;
809847

0 commit comments

Comments
 (0)