Skip to content

Commit

Permalink
Add support for backupOnly option in mediaType video renderer (preb…
Browse files Browse the repository at this point in the history
…id#5972)

* Add support for `backupOnly` option in mediaType video renderer

* Improve readability
  • Loading branch information
osazos authored Nov 25, 2020
1 parent 1137a64 commit 939c455
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
18 changes: 17 additions & 1 deletion src/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,21 @@ function isRendererPreferredFromAdUnit(adUnitCode) {
const adUnit = find(adUnits, adUnit => {
return adUnit.code === adUnitCode;
});
return !!(adUnit && adUnit.renderer && adUnit.renderer.url && adUnit.renderer.render && !(utils.isBoolean(adUnit.renderer.backupOnly) && adUnit.renderer.backupOnly));

if (!adUnit) {
return false
}

// renderer defined at adUnit level
const adUnitRenderer = utils.deepAccess(adUnit, 'renderer');
const hasValidAdUnitRenderer = !!(adUnitRenderer && adUnitRenderer.url && adUnitRenderer.render);

// renderer defined at adUnit.mediaTypes level
const mediaTypeRenderer = utils.deepAccess(adUnit, 'mediaTypes.video.renderer');
const hasValidMediaTypeRenderer = !!(mediaTypeRenderer && mediaTypeRenderer.url && mediaTypeRenderer.render)

return !!(
(hasValidAdUnitRenderer && !(adUnitRenderer.backupOnly === true)) ||
(hasValidMediaTypeRenderer && !(mediaTypeRenderer.backupOnly === true))
);
}
6 changes: 3 additions & 3 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
* @property {function(): void} callBids - sends requests to all adapters for bids
*/

import {flatten, timestamp, adUnitsFilter, deepAccess, getBidRequest, getValue, parseUrl, isBoolean} from './utils.js';
import {flatten, timestamp, adUnitsFilter, deepAccess, getBidRequest, getValue, parseUrl} from './utils.js';
import { getPriceBucketString } from './cpmBucketManager.js';
import { getNativeTargeting } from './native.js';
import { getCacheUrl, store } from './videoCache.js';
Expand Down Expand Up @@ -533,9 +533,9 @@ function getPreparedBidForAuction({adUnitCode, bid, bidderRequest, auctionId}) {
var renderer = null;

// the renderer for the mediaType takes precendence
if (mediaTypeRenderer && mediaTypeRenderer.url && mediaTypeRenderer.render) {
if (mediaTypeRenderer && mediaTypeRenderer.url && !(mediaTypeRenderer.backupOnly === true && mediaTypeRenderer.render)) {
renderer = mediaTypeRenderer;
} else if (adUnitRenderer && adUnitRenderer.url && !(adUnitRenderer.backupOnly && isBoolean(adUnitRenderer.backupOnly) && bid.renderer)) {
} else if (adUnitRenderer && adUnitRenderer.url && !(adUnitRenderer.backupOnly === true && bid.renderer)) {
renderer = adUnitRenderer;
}

Expand Down
32 changes: 32 additions & 0 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,38 @@ describe('auctionmanager.js', function () {
assert.equal(addedBid.renderer.url, renderer.url);
});

it('installs bidder-defined renderer when onlyBackup is true in mediaTypes.video options ', function () {
const renderer = {
url: 'videoRenderer.js',
backupOnly: true,
render: (bid) => bid
};
let myBid = mockBid();
let bidRequest = mockBidRequest(myBid);

bidRequest.bids[0] = {
...bidRequest.bids[0],
mediaTypes: {
video: {
context: 'outstream',
renderer
}
}
};
makeRequestsStub.returns([bidRequest]);

myBid.mediaType = 'video';
myBid.renderer = {
url: 'renderer.js',
render: sinon.spy()
};
spec.interpretResponse.returns(myBid);
auction.callBids();

const addedBid = auction.getBidsReceived().pop();
assert.strictEqual(addedBid.renderer.url, myBid.renderer.url);
});

it('bid for a regular unit and a video unit', function() {
let renderer = {
url: 'renderer.js',
Expand Down
30 changes: 30 additions & 0 deletions test/spec/renderer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,36 @@ describe('Renderer', function () {
expect(loadExternalScript.called).to.be.true;
});

it('should load external script instead of publisher-defined one when backupOnly option is true in mediaTypes.video options', function() {
$$PREBID_GLOBAL$$.adUnits = [{
code: 'video1',
mediaTypes: {
video: {
context: 'outstream',
mimes: ['video/mp4'],
playerSize: [[400, 300]],
renderer: {
url: 'https://acdn.adnxs.com/video/outstream/ANOutstreamVideo.js',
backupOnly: true,
render: sinon.spy()
},
}
}
}]

let testRenderer = Renderer.install({
url: 'https://httpbin.org/post',
config: { test: 'config1' },
id: 1,
adUnitCode: 'video1'

});
testRenderer.setRender(() => {})

testRenderer.render()
expect(loadExternalScript.called).to.be.true;
});

it('should call loadExternalScript() for script not defined on adUnit, only when .render() is called', function() {
$$PREBID_GLOBAL$$.adUnits = [{
code: 'video1',
Expand Down

0 comments on commit 939c455

Please sign in to comment.