-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
173 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function preciseFloat(value: number) { | ||
return Math.round((value + Number.EPSILON) * 100) / 100; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,138 @@ | ||
import type { InterstitialAsset } from "./types"; | ||
|
||
interface SignalingEvent { | ||
type: "impression" | "quartile" | "clickthrough"; | ||
// The types below define the SVTA Ad Signaling Spec version 2, | ||
// if we make changes here, we should make them in @superstreamer/player too. | ||
// We consume these events in the player to fire beacons per pod. | ||
|
||
interface AdTrackingEvent { | ||
type: "impression" | "quartile" | "clickthrough" | "podstart" | "podend"; | ||
start?: number; | ||
urls: string[]; | ||
} | ||
|
||
const QUARTILE_EVENTS: Record<string, number> = { | ||
start: 0, | ||
firstQuartile: 0.25, | ||
midpoint: 0.5, | ||
thirdQuartile: 0.75, | ||
complete: 1, | ||
}; | ||
interface AdTrackingSlot { | ||
type: "linear"; | ||
start: number; | ||
duration: number; | ||
tracking: AdTrackingEvent[]; | ||
} | ||
|
||
interface AdTrackingPod { | ||
start: number; | ||
duration: number; | ||
tracking: AdTrackingEvent[]; | ||
} | ||
|
||
export interface AdTrackingPodEnvelope { | ||
version: 2; | ||
type: "pod"; | ||
payload: AdTrackingPod[]; | ||
} | ||
|
||
export interface AdTrackingSlotEnvelope { | ||
version: 2; | ||
type: "slot"; | ||
payload: AdTrackingSlot[]; | ||
} | ||
|
||
export function getSignalingForAsset( | ||
export function getSlotSignaling( | ||
assets: InterstitialAsset[], | ||
asset: InterstitialAsset, | ||
) { | ||
const { duration, tracking } = asset; | ||
if (!tracking) { | ||
): AdTrackingSlotEnvelope | null { | ||
const startTime = getAssetStartTime(assets, asset); | ||
|
||
const slot = mapAssetToSlot(asset, startTime); | ||
if (!slot) { | ||
return null; | ||
} | ||
|
||
const assetIndex = assets.indexOf(asset); | ||
const startTime = assets.splice(0, assetIndex).reduce((acc, asset) => { | ||
return { | ||
version: 2, | ||
type: "slot", | ||
payload: [slot], | ||
}; | ||
} | ||
|
||
export function getPodSignaling( | ||
assets: InterstitialAsset[], | ||
start: number, | ||
): AdTrackingPodEnvelope { | ||
const duration = assets.reduce((acc, asset) => { | ||
acc += asset.duration; | ||
return acc; | ||
}, 0); | ||
|
||
const signalingEvents: SignalingEvent[] = []; | ||
return { | ||
version: 2, | ||
type: "pod", | ||
payload: [ | ||
{ | ||
start, | ||
duration, | ||
tracking: [], | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
signalingEvents.push({ | ||
type: "impression", | ||
start: 0, | ||
urls: tracking.impression, | ||
}); | ||
const QUARTILE_EVENTS: Record<string, number> = { | ||
start: 0, | ||
firstQuartile: 0.25, | ||
midpoint: 0.5, | ||
thirdQuartile: 0.75, | ||
complete: 1, | ||
}; | ||
|
||
signalingEvents.push({ | ||
type: "clickthrough", | ||
urls: tracking.clickThrough, | ||
}); | ||
function mapAssetToSlot( | ||
asset: InterstitialAsset, | ||
start: number, | ||
): AdTrackingSlot | null { | ||
if (!asset.tracking) { | ||
return null; | ||
} | ||
|
||
const events: AdTrackingEvent[] = [ | ||
{ | ||
type: "impression", | ||
start: 0, | ||
urls: asset.tracking.impression, | ||
}, | ||
{ | ||
type: "clickthrough", | ||
urls: asset.tracking.clickThrough, | ||
}, | ||
]; | ||
|
||
// Map each tracking URL to their corresponding quartile. | ||
Object.entries(tracking).forEach(([name, urls]) => { | ||
Object.entries(asset.tracking).forEach(([name, urls]) => { | ||
const offset = QUARTILE_EVENTS[name]; | ||
if (offset !== undefined) { | ||
signalingEvents.push({ | ||
events.push({ | ||
type: "quartile", | ||
start: duration * offset, | ||
start: asset.duration * offset, | ||
urls, | ||
}); | ||
} | ||
}); | ||
|
||
return { | ||
version: 2, | ||
type: "slot", | ||
payload: [ | ||
{ | ||
type: "linear", | ||
start: startTime, | ||
duration: asset.duration, | ||
tracking: signalingEvents, | ||
}, | ||
], | ||
type: "linear", | ||
start, | ||
duration: asset.duration, | ||
tracking: events, | ||
}; | ||
} | ||
|
||
function getAssetStartTime( | ||
assets: InterstitialAsset[], | ||
target: InterstitialAsset, | ||
) { | ||
let startTime = 0; | ||
for (const asset of assets) { | ||
if (asset === target) { | ||
break; | ||
} | ||
startTime += asset.duration; | ||
} | ||
return startTime; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters