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

Revert "Revert "fix bidcaching for 1x1"" #51

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
22 changes: 19 additions & 3 deletions src/marfeelTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ export const setLastLocationFromLastAdUnit = (adUnitArr) => {
}
}

export const getCurrentAuctionSizes = () => {
const normalizeSizes = sizesArray => sizesArray.join('x');

function is1x1Allowed(auctionSizes) {
const LARGE_SIZE_NORMALIZED = '300x250';

return (auctionSizes.map(normalizeSizes).includes(LARGE_SIZE_NORMALIZED));
}

const getCurrentAuctionSizes = () => {
const lastAdUnitUsed = [...auctionManager.getAdUnits()].pop();

if (lastAdUnitUsed &&
Expand All @@ -56,11 +64,19 @@ export const getCurrentAuctionSizes = () => {
return [];
}

const normalizeSizes = sizesArray => sizesArray.join('x');

export const isBidSizeAllowed = (bid, allowedSizes) => {
const allowedSizesNormalized = allowedSizes.map(normalizeSizes);
const bidSize = normalizeSizes([bid.width, bid.height]);

return allowedSizesNormalized.includes(bidSize);
}

function add1x1IfAllowed(auctionSizes) {
const SIZE_1_X_1 = [1, 1];

return is1x1Allowed(auctionSizes) ? [...auctionSizes, SIZE_1_X_1] : auctionSizes;
}

export function getAllowedSizes() {
return add1x1IfAllowed(getCurrentAuctionSizes())
}
4 changes: 2 additions & 2 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { auctionManager } from './auctionManager';
import { sizeSupported } from './sizeMapping';
import { ADPOD } from './mediaTypes';
import includes from 'core-js/library/fn/array/includes';
import { getLastLocation, getCurrentAuctionSizes, isBidSizeAllowed } from './marfeelTools';
import { getLastLocation, getAllowedSizes, isBidSizeAllowed } from './marfeelTools';

const utils = require('./utils.js');
var CONSTANTS = require('./constants.json');
Expand Down Expand Up @@ -372,7 +372,7 @@ export function newTargeting(auctionManager) {
}));

const bidsToFilter = bidsByReferrer[lastLocation] || filterBidsByAdUnit(bidsReceived);
const allowedSizes = getCurrentAuctionSizes();
const allowedSizes = getAllowedSizes();

bidsToProcess = bidsToFilter.filter(bid => isBidSizeAllowed(bid, allowedSizes));
}
Expand Down
34 changes: 33 additions & 1 deletion test/spec/marfeelTools_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* from Marfeel Solutions SL.
*/

import { filterBidsBySizes } from './marfeelTools';
import { isBidSizeAllowed, getAllowedSizes } from './marfeelTools';
import { auctionManager } from './auctionManager';

describe('marfeelTools', function () {
describe('isBidSizeAllowed', function() {
Expand Down Expand Up @@ -42,4 +43,35 @@ describe('marfeelTools', function () {
}])
});
});
describe('getAllowedSizes', function() {
it('adds 1x1 to allowed sizes if 300x250 is present', function() {
const currentSizes = [[100, 100], [300, 150], [300, 250]];
const fakeGetAdUnits = () => [{
mediaTypes: {
banner: {
sizes: currentSizes
}
}
}];
sinon.replace(auctionManager, 'getAdUnits', fakeGetAdUnits);
const expectedSizes = [[100, 100], [300, 150], [300, 250], [1, 1]];

assert.deepEqual(getAllowedSizes(), expectedSizes);
});

it('does not add 1x1 to allowed sizes if 300x250 is not present', function() {
const currentSizes = [[100, 100], [300, 150]];
const fakeGetAdUnits = () => [{
mediaTypes: {
banner: {
sizes: currentSizes
}
}
}];
sinon.replace(auctionManager, 'getAdUnits', fakeGetAdUnits);
const expectedSizes = [[100, 100], [300, 150]];

assert.deepEqual(getAllowedSizes(), expectedSizes);
});
});
});