Skip to content

Commit

Permalink
Rename duration as mainContentDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
wasp898 committed Dec 19, 2024
1 parent a1f4689 commit a0f3407
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
22 changes: 10 additions & 12 deletions spec/tests/AdHelper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions src/ts/ConvivaAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConvivaAnalyticsSsai, 'reset'>;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions src/ts/helper/AdHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export interface SsaiAdInfo {
export class AdHelper {
public static mapCsaiAdPosition(
adBreak: AdBreak,
duration: number,
mainContentDuration: number,
): Conviva.valueof<Conviva.ConvivaConstants['AdPosition']> {

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

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

Expand All @@ -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;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a0f3407

Please sign in to comment.