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 27, 2025
1 parent a019a76 commit e5e1ce3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions spec/samples/inline_trackers.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export const inlineTrackersParsed = {
close: ['http://example.com/linear-close'],
thirdQuartile: ['http://example.com/linear-thirdQuartile'],
'progress-30': ['http://example.com/linear-progress-30sec'],
'progress-42.333': ['http://example.com/linear-progress-42.333sec'],
'progress-60%': ['http://example.com/linear-progress-60%'],
otherAdInteraction: [
'http://example.com/linear-otherAdInteraction',
Expand Down
2 changes: 1 addition & 1 deletion spec/vast_trackers.spec.js → spec/vast_tracker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ describe('VASTTracker', function () {
expect(spyEmitter).toHaveBeenCalledWith('skip-countdown', 0);
});

it('should track rewind when set to 2', () => {
it('should track rewind when set to 2', () => {
vastTracker.setProgress(2);
expect(spyTrack).toHaveBeenCalledWith('rewind', expect.any(Object));
});
Expand Down
42 changes: 40 additions & 2 deletions src/vast_tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class VASTTracker extends EventEmitter {
this.impressed = false;
this.skippable = false;
this.trackingEvents = {};
this.trackedProgressEvents = [];
// We need to keep the last percentage of the tracker in order to
// calculate to trigger the events when the VAST duration is short
this.lastPercentage = 0;
Expand Down Expand Up @@ -211,7 +212,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 All @@ -228,6 +229,9 @@ export class VASTTracker extends EventEmitter {

if (progress < this.progress) {
this.track('rewind', { macros });
if (this.trackedProgressEvents) {
this.trackedProgressEvents.splice(0);
}
}
}

Expand Down Expand Up @@ -778,12 +782,42 @@ 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 && time > this.progress);

progressEvents.forEach(({ name, urls }) => {
if (!once && this.trackedProgressEvents.includes(name)) {
return;
}
this.emit(name, { trackingURLTemplates: urls });
this.trackURLs(urls, macros);

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

/**
* Calls the tracking URLs for the given eventName and emits the event.
*
* @param {String} eventName - The name of the event.
* @param {Object} options
* @param {Object} [options.macros={}] - An optional Object of parameters(vast macros) to be used in the tracking calls.
* @param {Object} [options.macros={}] - An optional Object of parameters (vast macros) to be used in the tracking calls.
* @param {Boolean} [options.once=false] - Boolean to define if the event has to be tracked only once.
*
*/
Expand All @@ -804,6 +838,10 @@ export class VASTTracker extends EventEmitter {
eventName = 'close';
}

if (eventName.startsWith('progress-') && !eventName.endsWith("%")) {
this.trackProgressEvents(eventName, macros, once);
}

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

Expand Down

0 comments on commit e5e1ce3

Please sign in to comment.