Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow publisher to define backup renderer #5638

Merged
merged 19 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function Renderer(options) {

// use a function, not an arrow, in order to be able to pass "arguments" through
this.render = function () {
if (!isRendererDefinedOnAdUnit(adUnitCode)) {
if (!isRendererPreferredFromAdUnit(adUnitCode)) {
// we expect to load a renderer url once only so cache the request to load script
loadExternalScript(url, moduleCode, this.callback);
} else {
Expand Down Expand Up @@ -110,10 +110,10 @@ export function executeRenderer(renderer, bid) {
renderer.render(bid);
}

function isRendererDefinedOnAdUnit(adUnitCode) {
function isRendererPreferredFromAdUnit(adUnitCode) {
const adUnits = $$PREBID_GLOBAL$$.adUnits;
const adUnit = find(adUnits, adUnit => {
return adUnit.code === adUnitCode;
});
return !!(adUnit && adUnit.renderer && adUnit.renderer.url && adUnit.renderer.render);
return !!(adUnit && adUnit.renderer && adUnit.renderer.url && adUnit.renderer.render && !(utils.isBoolean(adUnit.renderer.backupOnly) && adUnit.renderer.backupOnly));
}
4 changes: 2 additions & 2 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} from './utils.js';
import {flatten, timestamp, adUnitsFilter, deepAccess, getBidRequest, getValue, parseUrl, isBoolean} from './utils.js';
import { getPriceBucketString } from './cpmBucketManager.js';
import { getNativeTargeting } from './native.js';
import { getCacheUrl, store } from './videoCache.js';
Expand Down Expand Up @@ -512,7 +512,7 @@ function getPreparedBidForAuction({adUnitCode, bid, bidderRequest, auctionId}) {
const bidReq = bidderRequest.bids && find(bidderRequest.bids, bid => bid.adUnitCode == adUnitCode);
const adUnitRenderer = bidReq && bidReq.renderer;

if (adUnitRenderer && adUnitRenderer.url) {
if (adUnitRenderer && adUnitRenderer.url && !(adUnitRenderer.backupOnly && isBoolean(adUnitRenderer.backupOnly) && bid.renderer)) {
bidObject.renderer = Renderer.install({ url: adUnitRenderer.url });
bidObject.renderer.setRender(adUnitRenderer.render);
}
Expand Down
23 changes: 23 additions & 0 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,29 @@ describe('auctionmanager.js', function () {
assert.equal(addedBid.renderer.url, 'renderer.js');
});

it('installs publisher-defined backup renderers on bids', function () {
let renderer = {
url: 'renderer.js',
backupOnly: true,
render: (bid) => bid
};
let bidRequests = [Object.assign({}, TEST_BID_REQS[0])];
bidRequests[0].bids[0] = Object.assign({ renderer }, bidRequests[0].bids[0]);
makeRequestsStub.returns(bidRequests);

let bids1 = Object.assign({},
bids[0],
{
bidderCode: BIDDER_CODE,
mediaType: 'video-outstream',
}
);
spec.interpretResponse.returns(bids1);
auction.callBids();
const addedBid = auction.getBidsReceived().pop();
assert.equal(addedBid.renderer.url, 'renderer.js');
});

it('bid for a regular unit and a video unit', function() {
let renderer = {
url: 'renderer.js',
Expand Down
2 changes: 1 addition & 1 deletion test/spec/modules/prebidServerBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ describe('S2S Adapter', function () {
resetSyncedStatus();
});

it('should not add outstrean without renderer', function () {
it('should not add outstream without renderer', function () {
let ortb2Config = utils.deepClone(CONFIG);
ortb2Config.endpoint = 'https://prebid.adnxs.com/pbs/v1/openrtb2/auction';

Expand Down
23 changes: 23 additions & 0 deletions test/spec/renderer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ describe('Renderer', function () {
expect(utilsSpy.callCount).to.equal(1);
});

it('should load renderer adunit renderer when backupOnly', function() {
$$PREBID_GLOBAL$$.adUnits = [{
code: 'video1',
renderer: {
url: 'http://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