Skip to content

Commit

Permalink
handle rubicon targeting and default bidder settings
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich committed Oct 25, 2016
1 parent defa7f0 commit ddc11d2
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 8 deletions.
68 changes: 62 additions & 6 deletions src/adapters/rubicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ var RubiconAdapter = function RubiconAdapter() {
bidResponse.width = size[0];
bidResponse.height = size[1];

bidResponse.rubiconTargeting = _setTargeting(slot, slot.bid.params.accountId);

// DealId
if (ad.deal) {
bidResponse.dealId = ad.deal;
Expand All @@ -180,18 +182,16 @@ var RubiconAdapter = function RubiconAdapter() {

/**
* download the rubicontag sdk
* @param {Object} options
* @param {String} options.accountId
* @param {String} accountId
* @param {Function} callback
*/
function _initSDK(options, done) {
function _initSDK(accountId, done) {
if (RUBICON_INITIALIZED) {
return;
}

RUBICON_INITIALIZED = 1;

var accountId = options.accountId;
var scripttUrl = RUBICONTAG_URL + accountId + '.js';

adloader.loadScript(scripttUrl, done, true);
Expand Down Expand Up @@ -308,13 +308,66 @@ var RubiconAdapter = function RubiconAdapter() {
});
}

/**
* Register the default bidder settings for rubicon targeting
* @param {String} accountId
*/
var _registerBidderSettings = (function() {
var _called = false;

// this function wrapped with closure to only run once
return accountId => {
if(!_called) {

bidmanager.registerDefaultBidderSetting(
RUBICON_BIDDER_CODE,
{
sendStandardTargeting: false,
suppressEmptyKeys: true,
adserverTargeting: [
'rpfl_' + accountId,
'rpfl_elemid'
].map(key => ({
key: key,
val: bidResponse => bidResponse.rubiconTargeting && bidResponse.rubiconTargeting[key]
})
)
}
);

_called = true;
}
};
})();


/**
* Gets targeting information off bid slot
* @param {RubiconSlot} slot
* @param {String} accountId
* @returns {Object} key value pairs for targeting
*/
function _setTargeting(slot, accountId) {
let getTarget = key => slot.getAdServerTargetingByKey(key)[0],
targeting = {};
[
'rpfl_' + accountId,
'rpfl_elemid'
].forEach((key) => {
targeting[key] = getTarget(key);
});

return targeting;
}

/**
* Request the specified bids from
* Rubicon
* @param {Object} bidderRequest the bidder-level params (from prebid)
* @param {Array} params.bids the bids requested
*/
function _callBids(bidderRequest) {
var accountId = bidderRequest.bids[0].params.accountId;

// start the timer; want to measure from
// even just loading the SDK
Expand All @@ -328,11 +381,14 @@ var RubiconAdapter = function RubiconAdapter() {

// on the first bid, set up the SDK
if (!RUBICON_INITIALIZED) {
_initSDK(bidderRequest.bids[0].params);
_initSDK(accountId);
}

_rready(function () {
window.rubicontag.setIntegration('$$PREBID_GLOBAL$$');
var config = window.rubicontag.setIntegration('$$PREBID_GLOBAL$$');
if(config && config.pbjsRubiconTargeting) {
_registerBidderSettings(accountId);
}

var slots = [];
var bids = bidderRequest.bids;
Expand Down
49 changes: 47 additions & 2 deletions test/spec/adapters/rubicon_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ describe("the rubicon adapter", () => {
addKW: sandbox.spy(),
getElementId: () => "/19968336/header-bid-tag-0",
getRawResponses: () => {},
getRawResponseBySizeId: () => {}
getRawResponseBySizeId: () => {},
getAdServerTargetingByKey: key => ({
"rpfl_14062": ["15_tier"],
"rpfl_elemid": ["/19968336/header-bid-tag-0"]
}[key])
};

window.rubicontag = {
cmd: {
push: cb => cb()
},
setIntegration: sandbox.spy(),
setIntegration: () => {},
run: () => {},
addEventListener: () => {},
setUserKey: sandbox.spy(),
Expand Down Expand Up @@ -154,6 +158,8 @@ describe("the rubicon adapter", () => {
describe("when doing fastlane slot configuration", () => {

beforeEach(() => {
sandbox.spy(window.rubicontag, 'setIntegration');

rubiconAdapter.callBids(bidderRequest);
});

Expand Down Expand Up @@ -217,6 +223,40 @@ describe("the rubicon adapter", () => {

});

describe("rubicon targeting", () => {

beforeEach(() => {
// need new adapter to reset private state
rubiconAdapter = new RubiconAdapter();
});

it("should register default bidder settings (once) when configured", () => {

sandbox.spy(bidManager, "registerDefaultBidderSetting");

sandbox.stub(window.rubicontag, 'setIntegration', () => ({
pbjsRubiconTargeting: true
}));

rubiconAdapter.callBids(bidderRequest);
rubiconAdapter.callBids(bidderRequest);

expect(bidManager.registerDefaultBidderSetting.calledOnce).to.equal(true);

});

it("should not register default bidder settings when not configured", () => {

sandbox.spy(bidManager, "registerDefaultBidderSetting");

rubiconAdapter.callBids(bidderRequest);

expect(bidManager.registerDefaultBidderSetting.called).to.equal(false);

})

});

describe("when handling fastlane responses", () => {

beforeEach(() => {
Expand Down Expand Up @@ -300,11 +340,16 @@ describe("the rubicon adapter", () => {
expect(bids[0].width).to.equal(300);
expect(bids[0].height).to.equal(250);
expect(bids[0].cpm).to.equal(0.811);
expect(bids[0].rubiconTargeting['rpfl_14062']).to.equal("15_tier");
expect(bids[0].rubiconTargeting['rpfl_elemid']).to.equal("/19968336/header-bid-tag-0");

expect(bids[1].bidderCode).to.equal("rubicon");
expect(bids[1].width).to.equal(320);
expect(bids[1].height).to.equal(50);
expect(bids[1].cpm).to.equal(0.59);
expect(bids[1].rubiconTargeting['rpfl_14062']).to.equal("15_tier");
expect(bids[1].rubiconTargeting['rpfl_elemid']).to.equal("/19968336/header-bid-tag-0");

})

});
Expand Down

0 comments on commit ddc11d2

Please sign in to comment.