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

Custom 2018 05 22 #3

Closed
wants to merge 4 commits 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
6 changes: 6 additions & 0 deletions build-pbjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rm -fr build/dist/
rm -fr node_modules
yarn install
gulp build --modules=adformBidAdapter,appnexusBidAdapter,criteoBidAdapter,improvedigitalBidAdapter,ixBidAdapter,openxBidAdapter,pubmaticBidAdapter,rubiconBidAdapter,widespaceBidAdapter
mv build/dist/prebid.js build/dist/prebid.`date +"d%Y%m%d%s"`.js
open build/dist/
5 changes: 3 additions & 2 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode = function (adUnitCode) {
/**
* Set query string targeting on one or more GPT ad units.
* @param {(string|string[])} adUnit a single `adUnit.code` or multiple.
* @param {function(object)} customSlotMatching gets a GoogleTag slot and returns a filter function.
* @alias module:pbjs.setTargetingForGPTAsync
*/
$$PREBID_GLOBAL$$.setTargetingForGPTAsync = function (adUnit) {
$$PREBID_GLOBAL$$.setTargetingForGPTAsync = function (adUnit, customSlotMatching) {
utils.logInfo('Invoking $$PREBID_GLOBAL$$.setTargetingForGPTAsync', arguments);
if (!isGptPubadsDefined()) {
utils.logError('window.googletag is not defined on the page');
Expand All @@ -172,7 +173,7 @@ $$PREBID_GLOBAL$$.setTargetingForGPTAsync = function (adUnit) {
targeting.resetPresetTargeting(adUnit);

// now set new targeting keys
targeting.setTargetingForGPT(targetingSet);
targeting.setTargetingForGPT(targetingSet, customSlotMatching);

Object.keys(targetingSet).forEach((adUnitCode) => {
Object.keys(targetingSet[adUnitCode]).forEach((targetingKey) => {
Expand Down
23 changes: 21 additions & 2 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ export function newTargeting(auctionManager) {
.concat(getCustomBidTargeting(adUnitCodes, bidsReceived))
.concat(config.getConfig('enableSendAllBids') ? getBidLandscapeTargeting(adUnitCodes, bidsReceived) : []);

// make sure at least there is a entry per adUnitCode in the targeting so receivers of SET_TARGETING call's can know what ad units are being invoked

adUnitCodes.forEach(key => {
if (!targeting.some(target => {
let hasMatchingAdUnitCode = false;

Object.getOwnPropertyNames(target).forEach(targetKey => {
if (targetKey === key) {
hasMatchingAdUnitCode = true;
}
});
return hasMatchingAdUnitCode;
})) {
const emptyTargeting = {};
emptyTargeting[key] = [];
targeting.push(emptyTargeting);
}
});

// store a reference of the targeting keys
targeting.map(adUnitCode => {
Object.keys(adUnitCode).map(key => {
Expand Down Expand Up @@ -141,9 +160,9 @@ export function newTargeting(auctionManager) {
* Sets targeting for DFP
* @param {Object.<string,Object.<string,string>>} targetingConfig
*/
targeting.setTargetingForGPT = function(targetingConfig) {
targeting.setTargetingForGPT = function(targetingConfig, customSlotMatching) {
window.googletag.pubads().getSlots().forEach(slot => {
Object.keys(targetingConfig).filter(isAdUnitCodeMatchingSlot(slot))
Object.keys(targetingConfig).filter(customSlotMatching ? customSlotMatching(slot) : isAdUnitCodeMatchingSlot(slot))
.forEach(targetId =>
Object.keys(targetingConfig[targetId]).forEach(key => {
let valueArr = targetingConfig[targetId][key].split(',');
Expand Down
30 changes: 30 additions & 0 deletions test/spec/unit/core/targeting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,36 @@ describe('targeting tests', () => {
});
}); // end getAllTargeting tests

describe('getAllTargeting without bids return empty object', () => {
let amBidsReceivedStub;
let amGetAdUnitsStub;
let bidExpiryStub;

beforeEach(() => {
$$PREBID_GLOBAL$$._sendAllBids = false;
amBidsReceivedStub = sinon.stub(auctionManager, 'getBidsReceived').callsFake(function() {
return [];
});
amGetAdUnitsStub = sinon.stub(auctionManager, 'getAdUnitCodes').callsFake(function() {
return ['/123456/header-bid-tag-0'];
});
bidExpiryStub = sinon.stub(targetingModule, 'isBidExpired').returns(true);
});

afterEach(() => {
auctionManager.getBidsReceived.restore();
auctionManager.getAdUnitCodes.restore();
targetingModule.isBidExpired.restore();
});

it('returns targetingSet correctly', () => {
let targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);

// we should only get the targeting data for the one requested adunit to at least exist even though it has no keys to set
expect(Object.keys(targeting).length).to.equal(1);
});
}); // end getAllTargeting without bids return empty object

describe('Targeting in concurrent auctions', () => {
describe('check getOldestBid', () => {
let bidExpiryStub;
Expand Down
18 changes: 9 additions & 9 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ describe('Unit: Prebid Module', function () {

it('should return current targeting data for slots', function () {
$$PREBID_GLOBAL$$.setConfig({ enableSendAllBids: true });
const targeting = $$PREBID_GLOBAL$$.getAdserverTargeting();
const expected = getAdServerTargeting();
const targeting = $$PREBID_GLOBAL$$.getAdserverTargeting(['/19968336/header-bid-tag-0', '/19968336/header-bid-tag1']);
const expected = getAdServerTargeting(['/19968336/header-bid-tag-0, /19968336/header-bid-tag1']);
assert.deepEqual(targeting, expected, 'targeting ok');
});

it('should return correct targeting with default settings', () => {
var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting();
var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting(['/19968336/header-bid-tag-0', '/19968336/header-bid-tag1']);
var expected = {
'/19968336/header-bid-tag-0': {
foobar: '0x0,300x250,300x600',
Expand All @@ -230,8 +230,8 @@ describe('Unit: Prebid Module', function () {

it('should return correct targeting with bid landscape targeting on', () => {
$$PREBID_GLOBAL$$.setConfig({ enableSendAllBids: true });
var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting();
var expected = getAdServerTargeting();
var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting(['/19968336/header-bid-tag-0', '/19968336/header-bid-tag1']);
var expected = getAdServerTargeting(['/19968336/header-bid-tag-0', '/19968336/header-bid-tag1']);
assert.deepEqual(targeting, expected);
});

Expand All @@ -248,7 +248,7 @@ describe('Unit: Prebid Module', function () {

auction.getBidsReceived = function() { return _bidsReceived };

var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting();
var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting(['/19968336/header-bid-tag-0', '/19968336/header-bid-tag1']);

// Ensure targeting for both ad placements includes the custom key.
assert.equal(
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('Unit: Prebid Module', function () {
}
};

var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting();
var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting(['/19968336/header-bid-tag-0', '/19968336/header-bid-tag1']);

var expected = {
'/19968336/header-bid-tag-0': {
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('Unit: Prebid Module', function () {

auction.getBidsReceived = function() { return _bidsReceived };

var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting();
var targeting = $$PREBID_GLOBAL$$.getAdserverTargeting(['/19968336/header-bid-tag-0', '/19968336/header-bid-tag1']);

var expected = {
'/19968336/header-bid-tag-0': {
Expand Down Expand Up @@ -1488,7 +1488,7 @@ describe('Unit: Prebid Module', function () {
assert.ok(spyCallBids.calledTwice, 'When two requests for bids are made both should be' +
' callBids immediately');

let result = targeting.getAllTargeting(); // $$PREBID_GLOBAL$$.getAdserverTargeting();
let result = targeting.getAllTargeting(['/19968336/header-bid-tag-0', '/19968336/header-bid-tag1']); // $$PREBID_GLOBAL$$.getAdserverTargeting();
let expected = {
'/19968336/header-bid-tag-0': {
'foobar': '0x0,300x250,300x600',
Expand Down