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

handle rubicon targeting and default bidder settings #3

Closed
wants to merge 1 commit into from
Closed
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
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