diff --git a/spec/tests/AdHelper.spec.ts b/spec/tests/AdHelper.spec.ts index e76fc42..21464c2 100644 --- a/spec/tests/AdHelper.spec.ts +++ b/spec/tests/AdHelper.spec.ts @@ -5,30 +5,30 @@ import * as Conviva from '@convivainc/conviva-js-coresdk'; describe(AdHelper, () => { describe('mapAdPosition', () => { it('should map ad position to preroll', () => { - const duration = 100; + const mainContentDuration = 100; const adBreak = { scheduleTime: 0 } as AdBreak; - expect(AdHelper.mapCsaiAdPosition(adBreak, duration)).toEqual(Conviva.Constants.AdPosition.PREROLL) + expect(AdHelper.mapCsaiAdPosition(adBreak, mainContentDuration)).toEqual(Conviva.Constants.AdPosition.PREROLL) }) it('should map ad position to postroll', () => { - const duration = 100; + const mainContentDuration = 100; const adBreak = { scheduleTime: 100 } as AdBreak; - expect(AdHelper.mapCsaiAdPosition(adBreak, duration)).toEqual(Conviva.Constants.AdPosition.POSTROLL) + expect(AdHelper.mapCsaiAdPosition(adBreak, mainContentDuration)).toEqual(Conviva.Constants.AdPosition.POSTROLL) }) it('should map ad position to midroll', () => { - const duration = 100; + const mainContentDuration = 100; const adBreak = { scheduleTime: 50 } as AdBreak; - expect(AdHelper.mapCsaiAdPosition(adBreak, duration)).toEqual(Conviva.Constants.AdPosition.MIDROLL) + expect(AdHelper.mapCsaiAdPosition(adBreak, mainContentDuration)).toEqual(Conviva.Constants.AdPosition.MIDROLL) }) }) @@ -60,8 +60,7 @@ describe(AdHelper, () => { describe('extractConvivaAdInfo', () => { it('should extract minimal Conviva ad info', () => { - const player = {} as PlayerAPI; - const duration = 100; + const mainContentDuration = 100; const adBreakEvent = { adBreak: { scheduleTime: 0 @@ -74,7 +73,7 @@ describe(AdHelper, () => { }, } as AdEvent; - expect(AdHelper.extractCsaiConvivaAdInfo(player, adBreakEvent, duration, adEvent)).toEqual({ + expect(AdHelper.extractCsaiConvivaAdInfo(adBreakEvent, mainContentDuration, adEvent)).toEqual({ "c3.ad.creativeId": "NA", "c3.ad.firstAdId": "123", "c3.ad.firstAdSystem": "NA", @@ -90,8 +89,7 @@ describe(AdHelper, () => { }) it('should extract full Conviva ad info', () => { - const player = {} as PlayerAPI; - const duration = 100; + const mainContentDuration = 100; const adBreakEvent = { adBreak: { scheduleTime: 0 @@ -115,7 +113,7 @@ describe(AdHelper, () => { } as Ad | LinearAd, } as AdEvent; - expect(AdHelper.extractCsaiConvivaAdInfo(player, adBreakEvent, duration, 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, diff --git a/src/ts/ConvivaAnalytics.ts b/src/ts/ConvivaAnalytics.ts index 0ff2771..ac886f5 100644 --- a/src/ts/ConvivaAnalytics.ts +++ b/src/ts/ConvivaAnalytics.ts @@ -45,7 +45,10 @@ export class ConvivaAnalytics { private readonly logger: Conviva.LoggingInterface = new Html5Logging(); - private duration: number = 0; + /** + * Tracks the duration of main content. Needed as the player may return the ad duration instead. + */ + private mainContentDuration = 0; public readonly ssai: Omit; @@ -281,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, this.duration, 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); @@ -368,7 +371,7 @@ export class ConvivaAnalytics { private onSourceLoaded = (event: PlayerEventBase) => { this.debugLog('[ ConvivaAnalytics ] [ Player Event ] onSourceLoaded', event); - this.duration = this.player.getDuration(); + this.mainContentDuration = this.player.getDuration(); }; private registerPlayerEvents(): void { diff --git a/src/ts/helper/AdHelper.ts b/src/ts/helper/AdHelper.ts index a3e5c4a..26614f3 100644 --- a/src/ts/helper/AdHelper.ts +++ b/src/ts/helper/AdHelper.ts @@ -43,14 +43,14 @@ export interface SsaiAdInfo { export class AdHelper { public static mapCsaiAdPosition( adBreak: AdBreak, - duration: number, + mainContentDuration: number, ): Conviva.valueof { if (adBreak.scheduleTime <= 0) { return Conviva.Constants.AdPosition.PREROLL; } - if (adBreak.scheduleTime >= duration) { + if (adBreak.scheduleTime >= mainContentDuration) { return Conviva.Constants.AdPosition.POSTROLL; } @@ -77,7 +77,11 @@ export class AdHelper { return formattedErrorParts.join(' '); } - public static extractCsaiConvivaAdInfo(player: PlayerAPI, adBreakEvent: AdBreakEvent, duration: number, 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; @@ -108,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, duration), + 'c3.ad.position': AdHelper.mapCsaiAdPosition(adBreakEvent.adBreak, mainContentDuration), 'c3.ad.system': adSystemName, 'c3.ad.creativeId': creativeId, 'c3.ad.firstAdId': firstAdId,