Skip to content

Commit

Permalink
do not load external js if renderer defined on adUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
jaiminpanchal27 committed Nov 9, 2018
1 parent 2206ba4 commit a98a349
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
3 changes: 3 additions & 0 deletions modules/appnexusBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ function newRenderer(adUnitCode, rtbBid, rendererOptions = {}) {
url: rtbBid.renderer_url,
config: rendererOptions,
loaded: false,
adUnitCode
});

try {
Expand Down Expand Up @@ -238,6 +239,7 @@ function newRenderer(adUnitCode, rtbBid, rendererOptions = {}) {
* @return Bid
*/
function newBid(serverBid, rtbBid, bidderRequest) {
const bidRequest = utils.getBidRequest(serverBid.uuid, [bidderRequest]);
const bid = {
requestId: serverBid.uuid,
cpm: rtbBid.cpm,
Expand All @@ -246,6 +248,7 @@ function newBid(serverBid, rtbBid, bidderRequest) {
currency: 'USD',
netRevenue: true,
ttl: 300,
adUnitCode: bidRequest.adUnitCode,
appnexus: {
buyerMemberId: rtbBid.buyer_member_id,
dealPriority: rtbBid.deal_priority,
Expand Down
23 changes: 18 additions & 5 deletions src/Renderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadScript } from './adloader';
import * as utils from './utils';
import find from 'core-js/library/fn/array/find';

/**
* @typedef {object} Renderer
Expand All @@ -10,7 +11,7 @@ import * as utils from './utils';
*/

export function Renderer(options) {
const { url, config, id, callback, loaded } = options;
const { url, config, id, callback, loaded, adUnitCode } = options;
this.url = url;
this.config = config;
this.handlers = {};
Expand All @@ -35,12 +36,16 @@ export function Renderer(options) {
this.process();
});

// we expect to load a renderer url once only so cache the request to load script
loadScript(url, this.callback, true);
if (!isRendererDefinedOnAdUnit(adUnitCode)) {
// we expect to load a renderer url once only so cache the request to load script
loadScript(url, this.callback, true);
} else {
utils.logWarn(`External Js not loaded by Renderer since renderer url and callback is already defined on adUnit ${adUnitCode}`);
}
}

Renderer.install = function({ url, config, id, callback, loaded }) {
return new Renderer({ url, config, id, callback, loaded });
Renderer.install = function({ url, config, id, callback, loaded, adUnitCode }) {
return new Renderer({ url, config, id, callback, loaded, adUnitCode });
};

Renderer.prototype.getConfig = function() {
Expand Down Expand Up @@ -94,3 +99,11 @@ export function isRendererRequired(renderer) {
export function executeRenderer(renderer, bid) {
renderer.render(bid);
}

function isRendererDefinedOnAdUnit(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);
}
30 changes: 26 additions & 4 deletions test/spec/modules/appnexusBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,18 @@ describe('AppNexusAdapter', function () {
'currency': 'USD',
'ttl': 300,
'netRevenue': true,
'adUnitCode': 'code',
'appnexus': {
'buyerMemberId': 958
}
}
];
let bidderRequest;
let bidderRequest = {
bids: [{
bidId: '3db3773286ee59',
adUnitCode: 'code'
}]
}
let result = spec.interpretResponse({ body: response }, {bidderRequest});
expect(Object.keys(result[0])).to.have.members(Object.keys(expectedResponse[0]));
});
Expand Down Expand Up @@ -505,7 +511,12 @@ describe('AppNexusAdapter', function () {
}]
}]
};
let bidderRequest;
let bidderRequest = {
bids: [{
bidId: '84ab500420319d',
adUnitCode: 'code'
}]
}

let result = spec.interpretResponse({ body: response }, {bidderRequest});
expect(result[0]).to.have.property('vastUrl');
Expand Down Expand Up @@ -538,7 +549,12 @@ describe('AppNexusAdapter', function () {
},
'impression_trackers': ['http://example.com'],
};
let bidderRequest;
let bidderRequest = {
bids: [{
bidId: '3db3773286ee59',
adUnitCode: 'code'
}]
}

let result = spec.interpretResponse({ body: response1 }, {bidderRequest});
expect(result[0].native.title).to.equal('Native Creative');
Expand All @@ -554,6 +570,7 @@ describe('AppNexusAdapter', function () {

const bidderRequest = {
bids: [{
bidId: '3db3773286ee59',
renderer: {
options: {
adText: 'configured'
Expand All @@ -573,7 +590,12 @@ describe('AppNexusAdapter', function () {
responseWithDeal.tags[0].ads[0].deal_priority = 'high';
responseWithDeal.tags[0].ads[0].deal_code = '123';

let bidderRequest;
let bidderRequest = {
bids: [{
bidId: '3db3773286ee59',
adUnitCode: 'code'
}]
}
let result = spec.interpretResponse({ body: responseWithDeal }, {bidderRequest});
expect(Object.keys(result[0].appnexus)).to.include.members(['buyerMemberId', 'dealPriority', 'dealCode']);
});
Expand Down

0 comments on commit a98a349

Please sign in to comment.