Skip to content

Commit

Permalink
includes a type property to all events (prebid#9771)
Browse files Browse the repository at this point in the history
  • Loading branch information
karimMourra authored May 12, 2023
1 parent 8fe7115 commit 5db8560
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
14 changes: 14 additions & 0 deletions libraries/video/shared/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { videoKey } from '../constants/constants.js'

export function getExternalVideoEventName(eventName) {
if (!eventName) {
return '';
}
return videoKey + eventName.replace(/^./, eventName[0].toUpperCase());
}

export function getExternalVideoEventPayload(eventName, payload) {
if (!payload) {
payload = {};
}

if (!payload.type) {
payload.type = eventName;
}
return payload;
}
4 changes: 2 additions & 2 deletions modules/videoModule/adQueue.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AD_BREAK_END, AUCTION_AD_LOAD_ATTEMPT, AUCTION_AD_LOAD_QUEUED, SETUP_COMPLETE } from '../../libraries/video/constants/events.js'
import { getExternalVideoEventName } from '../../libraries/video/shared/helpers.js'
import { getExternalVideoEventName, getExternalVideoEventPayload } from '../../libraries/video/shared/helpers.js'

export function AdQueueCoordinator(videoCore, pbEvents) {
const storage = {};
Expand Down Expand Up @@ -58,6 +58,6 @@ export function AdQueueCoordinator(videoCore, pbEvents) {

function triggerEvent(eventName, adTagUrl, options) {
const payload = Object.assign({ adTagUrl }, options);
pbEvents.emit(getExternalVideoEventName(eventName), payload);
pbEvents.emit(getExternalVideoEventName(eventName), getExternalVideoEventPayload(eventName, payload));
}
}
8 changes: 4 additions & 4 deletions modules/videoModule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { videoCoreFactory } from './coreVideo.js';
import { gamSubmoduleFactory } from './gamAdServerSubmodule.js';
import { videoImpressionVerifierFactory } from './videoImpressionVerifier.js';
import { AdQueueCoordinator } from './adQueue.js';
import { getExternalVideoEventName } from '../../libraries/video/shared/helpers.js'
import { getExternalVideoEventName, getExternalVideoEventPayload } from '../../libraries/video/shared/helpers.js'

const allVideoEvents = Object.keys(videoEvents).map(eventKey => videoEvents[eventKey]);
events.addEvents(allVideoEvents.concat([AUCTION_AD_LOAD_ATTEMPT, AUCTION_AD_LOAD_QUEUED, AUCTION_AD_LOAD_ABORT, BID_IMPRESSION, BID_ERROR]).map(getExternalVideoEventName));
Expand Down Expand Up @@ -54,7 +54,7 @@ export function PbVideo(videoCore_, getConfig_, pbGlobal_, pbEvents_, videoEvent
adQueueCoordinator.registerProvider(divId);
videoCore.initProvider(divId);
videoCore.onEvents(videoEvents, (type, payload) => {
pbEvents.emit(getExternalVideoEventName(type), payload);
pbEvents.emit(getExternalVideoEventName(type), getExternalVideoEventPayload(type, payload));
}, divId);

const adServerConfig = provider.adServer;
Expand Down Expand Up @@ -199,7 +199,7 @@ export function PbVideo(videoCore_, getConfig_, pbGlobal_, pbEvents_, videoEvent

const highestCpmBids = pbGlobal.getHighestCpmBids(adUnitCode);
if (!highestCpmBids.length) {
pbEvents.emit(getExternalVideoEventName(AUCTION_AD_LOAD_ABORT), options);
pbEvents.emit(getExternalVideoEventName(AUCTION_AD_LOAD_ABORT), getExternalVideoEventPayload(AUCTION_AD_LOAD_ABORT, options));
return;
}

Expand All @@ -223,7 +223,7 @@ export function PbVideo(videoCore_, getConfig_, pbGlobal_, pbEvents_, videoEvent
}

pbGlobal.markWinningBidAsUsed(bid);
pbEvents.emit(getExternalVideoEventName(eventName), { bid, adEvent: adEventPayload });
pbEvents.emit(getExternalVideoEventName(eventName), getExternalVideoEventPayload(eventName, { bid, adEvent: adEventPayload }));
}

function getBid(adPayload) {
Expand Down
22 changes: 22 additions & 0 deletions test/spec/modules/videoModule/shared/helpers_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getExternalVideoEventName, getExternalVideoEventPayload } from 'libraries/video/shared/helpers.js';
import { expect } from 'chai';

describe('Helpers', function () {
describe('getExternalVideoEventName', function () {
it('should append video prefix and stay camelcase', function () {
expect(getExternalVideoEventName('eventName')).to.equal('videoEventName');
expect(getExternalVideoEventName(null)).to.equal('');
});
});

describe('getExternalVideoEventPayload', function () {
it('should include type in payload when absent', function () {
const testType = 'testType';
const payloadWithType = { datum: 'datum', type: 'existingType' };
expect(getExternalVideoEventPayload(testType, payloadWithType).type).to.equal('existingType');

const payloadWithoutType = { datum: 'datum' };
expect(getExternalVideoEventPayload(testType, payloadWithoutType).type).to.equal(testType);
});
});
});

0 comments on commit 5db8560

Please sign in to comment.