Skip to content

Commit

Permalink
Merge pull request #30 from montevideo-tech/feature/updatedAdCreative…
Browse files Browse the repository at this point in the history
…SignalingDefinition

Updated plugin to support new Signal event definition ignoring non supported events.
Supported events: "start", "firstQuartile", "midpoint", "thirdQuartile", "complete", "progress", "impression"
More events will be available in next release
  • Loading branch information
nicolaslevy authored Feb 4, 2025
2 parents 93a85b7 + db6c126 commit d5cf6fd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 56 deletions.
16 changes: 10 additions & 6 deletions hls.js/adCreativeSignalingPlugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const SUPPORTED_TRACKING_EVENTS = ["start", "firstQuartile", "midpoint", "thirdQuartile", "complete", "progress", "impression"];

class AdSignalingManager {
constructor(hls) {
console.log("Initializing AdSignalingManager");
Expand Down Expand Up @@ -37,12 +39,14 @@ class AdSignalingManager {
const currentTime = this.assetPlayer.currentTime;

this.trackingEventsQueue.forEach((event, index) => {
const tolerance = 0.75; // time difference tolerance in seconds
if (
((Math.abs(currentTime - event.start) <= tolerance) || event?.start === undefined)
) {
Promise.all(event.urls.map((url) => this.sendTrackingEvent(url)));
this.trackingEventsQueue.splice(index, 1);
if (SUPPORTED_TRACKING_EVENTS.includes(event.type)) {
const tolerance = 0.75; // time difference tolerance in seconds
if (
(event?.offset === undefined || (Math.abs(currentTime - event.offset) <= tolerance))
) {
Promise.all(event.urls.map((url) => this.sendTrackingEvent(url)));
this.trackingEventsQueue.splice(index, 1);
}
}
});
};
Expand Down
16 changes: 10 additions & 6 deletions public/hls.js/adCreativeSignalingPlugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const SUPPORTED_TRACKING_EVENTS = ["start", "firstQuartile", "midpoint", "thirdQuartile", "complete", "progress", "impression"];

class AdSignalingManager {
constructor(hls) {
console.log("Initializing AdSignalingManager");
Expand Down Expand Up @@ -37,12 +39,14 @@ class AdSignalingManager {
const currentTime = this.assetPlayer.currentTime;

this.trackingEventsQueue.forEach((event, index) => {
const tolerance = 0.75; // time difference tolerance in seconds
if (
((Math.abs(currentTime - event.start) <= tolerance) || event?.start === undefined)
) {
Promise.all(event.urls.map((url) => this.sendTrackingEvent(url)));
this.trackingEventsQueue.splice(index, 1);
if (SUPPORTED_TRACKING_EVENTS.includes(event.type)) {
const tolerance = 0.75; // time difference tolerance in seconds
if (
(event?.offset === undefined || (Math.abs(currentTime - event.offset) <= tolerance))
) {
Promise.all(event.urls.map((url) => this.sendTrackingEvent(url)));
this.trackingEventsQueue.splice(index, 1);
}
}
});
};
Expand Down
86 changes: 42 additions & 44 deletions src/trackingEvents/tracking-events.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,85 @@
const { logger } = require("../utils/logger.js");

const IMPRESSION = "impression";
const PROGRESS = "progress";

class TrackingEvent {
constructor(type, duration, urls) {
// console.log(">>>TRACKING EVT: ", type)
// console.log(">>>TRACKING EVT: ", type)
this.type = type;
//this.duration = duration;
this.urls = urls;
//this.start = start;

if (this.type.startsWith("progress")) {

if (this.type.startsWith(PROGRESS)) {
const offsetValue = this.type.split("-")[1];
if (offsetValue !== null) {
this.offset =
this.offset =
typeof offsetValue === "string" && offsetValue.includes("%")
? (duration * parseFloat(offsetValue.replace("%", ""))) / 100
: parseFloat(offsetValue);

this.type = "progress";
? (duration * parseFloat(offsetValue.replace("%", ""))) / 100
: parseFloat(offsetValue);

if (isNaN(this.offset) || this.offset < 0 || this.offset > this.duration) {
throw new Error(`Invalid start value: ${offset}`);
this.type = PROGRESS;

if (
isNaN(this.offset) ||
this.offset < 0 ||
this.offset > this.duration
) {
throw new Error(`Invalid start value: ${offset}`);
}
}
}
else
{
switch(type)
{
case "start":
this.offset = 0;
break;

case "firstQuartile":
this.offset = duration * 0.25;
break;

case "midpoint":
this.offset = duration * 0.5;
break;

case "thirdQuartile":
this.offset = duration * 0.75;
break;

case "complete":
this.offset = duration;
} else {
switch (type) {
case "start":
this.offset = 0;
break;

case "firstQuartile":
this.offset = duration * 0.25;
break;

case "midpoint":
this.offset = duration * 0.5;
break;

case "thirdQuartile":
this.offset = duration * 0.75;
break;

case "complete":
this.offset = duration;
break;
}
}


}
}



class AdCreativeSignalingMapper {
constructor(ad) {
this.ad = ad;
console.log(">>>> Ad Tracking", this.ad.trackingEvents);
}

map() {
const newTrackingEvents = this.ad.trackingEvents;

if(this.ad?.impressions.length > 0)
newTrackingEvents[IMPRESSION] = this.ad.impressions.map((impression) => impression.url);

if (this.ad?.impressions.length > 0)
newTrackingEvents[IMPRESSION] = this.ad.impressions.map(
(impression) => impression.url
);

const trackingEvents = Object.entries(this.ad.trackingEvents)
.map(([eventType, urls]) => {
try {
const event = new TrackingEvent( eventType, this.ad.duration, urls );
const event = new TrackingEvent(eventType, this.ad.duration, urls);
//console.log(">>> track evt: ", event)
return event;
} catch (error) {
logger.warn(
`Skipping invalid event: ${eventType}, Error: ${error.message}`
);

return null;
}
})
Expand All @@ -93,5 +91,5 @@ class AdCreativeSignalingMapper {

module.exports = {
AdCreativeSignalingMapper,
TrackingEvent
TrackingEvent,
};

0 comments on commit d5cf6fd

Please sign in to comment.