Skip to content

Commit

Permalink
Merge pull request #118 from bitmovin/pw-21587-midroll-reported-as-po…
Browse files Browse the repository at this point in the history
…stroll

[PW-21587] Fix midroll reported as postroll
  • Loading branch information
wasp898 authored Dec 19, 2024
2 parents 19da007 + a0f3407 commit 38bf283
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- Mid-roll ads erroneously reported as post-rolls when mid-roll's schedule time is greater than the duration of the ad

## [6.1.0] - 2024-12-11
### Changed
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 10 additions & 16 deletions spec/tests/AdHelper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,30 @@ import * as Conviva from '@convivainc/conviva-js-coresdk';
describe(AdHelper, () => {
describe('mapAdPosition', () => {
it('should map ad position to preroll', () => {
const player = {
getDuration: () => 100,
} as PlayerAPI;
const mainContentDuration = 100;
const adBreak = {
scheduleTime: 0
} as AdBreak;

expect(AdHelper.mapCsaiAdPosition(adBreak, player)).toEqual(Conviva.Constants.AdPosition.PREROLL)
expect(AdHelper.mapCsaiAdPosition(adBreak, mainContentDuration)).toEqual(Conviva.Constants.AdPosition.PREROLL)
})

it('should map ad position to postroll', () => {
const player = {
getDuration: () => 100,
} as PlayerAPI;
const mainContentDuration = 100;
const adBreak = {
scheduleTime: 100
} as AdBreak;

expect(AdHelper.mapCsaiAdPosition(adBreak, player)).toEqual(Conviva.Constants.AdPosition.POSTROLL)
expect(AdHelper.mapCsaiAdPosition(adBreak, mainContentDuration)).toEqual(Conviva.Constants.AdPosition.POSTROLL)
})

it('should map ad position to midroll', () => {
const player = {
getDuration: () => 100,
} as PlayerAPI;
const mainContentDuration = 100;
const adBreak = {
scheduleTime: 50
} as AdBreak;

expect(AdHelper.mapCsaiAdPosition(adBreak, player)).toEqual(Conviva.Constants.AdPosition.MIDROLL)
expect(AdHelper.mapCsaiAdPosition(adBreak, mainContentDuration)).toEqual(Conviva.Constants.AdPosition.MIDROLL)
})
})

Expand Down Expand Up @@ -66,7 +60,7 @@ describe(AdHelper, () => {

describe('extractConvivaAdInfo', () => {
it('should extract minimal Conviva ad info', () => {
const player = {} as PlayerAPI;
const mainContentDuration = 100;
const adBreakEvent = {
adBreak: {
scheduleTime: 0
Expand All @@ -79,7 +73,7 @@ describe(AdHelper, () => {
},
} as AdEvent;

expect(AdHelper.extractCsaiConvivaAdInfo(player, adBreakEvent, adEvent)).toEqual({
expect(AdHelper.extractCsaiConvivaAdInfo(adBreakEvent, mainContentDuration, adEvent)).toEqual({
"c3.ad.creativeId": "NA",
"c3.ad.firstAdId": "123",
"c3.ad.firstAdSystem": "NA",
Expand All @@ -95,7 +89,7 @@ describe(AdHelper, () => {
})

it('should extract full Conviva ad info', () => {
const player = {} as PlayerAPI;
const mainContentDuration = 100;
const adBreakEvent = {
adBreak: {
scheduleTime: 0
Expand All @@ -119,7 +113,7 @@ describe(AdHelper, () => {
} as Ad | LinearAd,
} as AdEvent;

expect(AdHelper.extractCsaiConvivaAdInfo(player, adBreakEvent, adEvent)).toEqual({
expect(AdHelper.extractCsaiConvivaAdInfo(adBreakEvent, mainContentDuration, adEvent)).toEqual({
[Conviva.Constants.ASSET_NAME]: "Test title",
[Conviva.Constants.STREAM_URL]: 'https://test.com',
[Conviva.Constants.DURATION]: 100,
Expand Down
13 changes: 12 additions & 1 deletion src/ts/ConvivaAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class ConvivaAnalytics {

private readonly logger: Conviva.LoggingInterface = new Html5Logging();

/**
* Tracks the duration of main content. Needed as the player may return the ad duration instead.
*/
private mainContentDuration = 0;

public readonly ssai: Omit<ConvivaAnalyticsSsai, 'reset'>;

constructor(player: PlayerAPI | undefined, customerKey: string, config: ConvivaAnalyticsConfiguration = {}) {
Expand Down Expand Up @@ -279,7 +284,7 @@ export class ConvivaAnalytics {
private onAdStarted = (event: AdEvent) => {
this.debugLog('[ ConvivaAnalytics ] [ Player Event ] ad started', event);

const adInfo = AdHelper.extractCsaiConvivaAdInfo(this.player, this.lastAdBreakEvent, event);
const adInfo = AdHelper.extractCsaiConvivaAdInfo(this.lastAdBreakEvent, this.mainContentDuration, event);
const bitrateKbps = event.ad.data?.bitrate;

this.convivaAnalyticsTracker.trackAdStarted(adInfo, Conviva.Constants.AdType.CLIENT_SIDE, bitrateKbps);
Expand Down Expand Up @@ -364,7 +369,13 @@ export class ConvivaAnalytics {
this.releaseInternal(event);
};

private onSourceLoaded = (event: PlayerEventBase) => {
this.debugLog('[ ConvivaAnalytics ] [ Player Event ] onSourceLoaded', event);
this.mainContentDuration = this.player.getDuration();
};

private registerPlayerEvents(): void {
this.handlers.add(PlayerEvent.SourceLoaded, this.onSourceLoaded);
this.handlers.add(PlayerEvent.Play, this.onPlay);
this.handlers.add(PlayerEvent.Playing, this.onPlaying);
this.handlers.add(PlayerEvent.Paused, this.onPlaybackStateChanged);
Expand Down
13 changes: 9 additions & 4 deletions src/ts/helper/AdHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ export interface SsaiAdInfo {
export class AdHelper {
public static mapCsaiAdPosition(
adBreak: AdBreak,
player: PlayerAPI,
mainContentDuration: number,
): Conviva.valueof<Conviva.ConvivaConstants['AdPosition']> {

if (adBreak.scheduleTime <= 0) {
return Conviva.Constants.AdPosition.PREROLL;
}

if (adBreak.scheduleTime >= player.getDuration()) {
if (adBreak.scheduleTime >= mainContentDuration) {
return Conviva.Constants.AdPosition.POSTROLL;
}

Expand All @@ -76,7 +77,11 @@ export class AdHelper {
return formattedErrorParts.join(' ');
}

public static extractCsaiConvivaAdInfo(player: PlayerAPI, adBreakEvent: AdBreakEvent, adEvent: AdEvent): Conviva.ConvivaMetadata {
public static extractCsaiConvivaAdInfo(
adBreakEvent: AdBreakEvent,
mainContentDuration: number,
adEvent: AdEvent,
): Conviva.ConvivaMetadata {
const ad = adEvent.ad as Ad | LinearAd;
const adData = ad.data as undefined | AdData | VastAdData;

Expand Down Expand Up @@ -107,7 +112,7 @@ export class AdHelper {
const adInfo: Conviva.ConvivaMetadata = {
'c3.ad.id': ad.id,
'c3.ad.technology': Conviva.Constants.AdType.CLIENT_SIDE,
'c3.ad.position': AdHelper.mapCsaiAdPosition(adBreakEvent.adBreak, player),
'c3.ad.position': AdHelper.mapCsaiAdPosition(adBreakEvent.adBreak, mainContentDuration),
'c3.ad.system': adSystemName,
'c3.ad.creativeId': creativeId,
'c3.ad.firstAdId': firstAdId,
Expand Down

0 comments on commit 38bf283

Please sign in to comment.