Skip to content

Commit

Permalink
InvibesBidAdapter - multiposition support & support for multiple id m…
Browse files Browse the repository at this point in the history
…odules (#6506)
  • Loading branch information
rcheptanariu authored and idettman committed May 21, 2021
1 parent de15feb commit 952d02a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 65 deletions.
119 changes: 62 additions & 57 deletions modules/invibesBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const CONSTANTS = {
SYNC_ENDPOINT: 'https://k.r66net.com/GetUserSync',
TIME_TO_LIVE: 300,
DEFAULT_CURRENCY: 'EUR',
PREBID_VERSION: 5,
PREBID_VERSION: 6,
METHOD: 'GET',
INVIBES_VENDOR_ID: 436,
USERID_PROVIDERS: ['pubcid', 'pubProvidedId']
USERID_PROVIDERS: ['pubcid', 'pubProvidedId', 'uid2', 'zeotapIdPlus', 'id5id']
};

const storage = getStorageManager(CONSTANTS.INVIBES_VENDOR_ID);
Expand Down Expand Up @@ -168,64 +168,76 @@ function handleResponse(responseObj, bidRequests) {
responseObj = responseObj.body || responseObj;
responseObj = responseObj.videoAdContentResult || responseObj;

let bidModel = responseObj.BidModel;
if (typeof bidModel !== 'object') {
utils.logInfo('Invibes Adapter - Bidding is not configured');
return [];
}

if (typeof invibes.bidResponse === 'object') {
utils.logInfo('Invibes Adapter - Bid response already received. Invibes only responds to one bid request per user visit');
return [];
}

invibes.bidResponse = responseObj;

let ads = responseObj.Ads;
const bidResponses = [];
for (let i = 0; i < bidRequests.length; i++) {
let bidRequest = bidRequests[i];

if (!Array.isArray(ads) || ads.length < 1) {
if (responseObj.AdReason != null) {
utils.logInfo('Invibes Adapter - ' + responseObj.AdReason);
let requestPlacement = null;
if (responseObj.AdPlacements != null) {
for (let j = 0; j < responseObj.AdPlacements.length; j++) {
let bidModel = responseObj.AdPlacements[j].BidModel;
if (bidModel != null && bidModel.PlacementId == bidRequest.params.placementId) {
requestPlacement = responseObj.AdPlacements[j];
break;
}
}
} else {
let bidModel = responseObj.BidModel;
if (bidModel != null && bidModel.PlacementId == bidRequest.params.placementId) {
requestPlacement = responseObj;
}
}

utils.logInfo('Invibes Adapter - No ads available');
return [];
let bid = createBid(bidRequest, requestPlacement);
if (bid !== null) {
bidResponses.push(bid);
}
}

let ad = ads[0];
return bidResponses;
}

if (bidModel.PlacementId == null) {
utils.logInfo('Invibes Adapter - No Placement Id in response');
return [];
function createBid(bidRequest, requestPlacement) {
if (requestPlacement === null || requestPlacement.BidModel === null) {
utils.logInfo('Invibes Adapter - Placement not configured for bidding ' + bidRequest.params.placementId);
return null;
}

const bidResponses = [];
for (let i = 0; i < bidRequests.length; i++) {
let bidRequest = bidRequests[i];

if (bidModel.PlacementId == bidRequest.params.placementId) {
let size = getBiggerSize(bidRequest.sizes);

bidResponses.push({
requestId: bidRequest.bidId,
cpm: ad.BidPrice,
width: bidModel.Width || size[0],
height: bidModel.Height || size[1],
creativeId: ad.VideoExposedId,
currency: bidModel.Currency || CONSTANTS.DEFAULT_CURRENCY,
netRevenue: true,
ttl: CONSTANTS.TIME_TO_LIVE,
ad: renderCreative(bidModel)
});

const now = Date.now();
ivLogger.info('Bid auction started at ' + bidModel.AuctionStartTime + ' . Invibes registered the bid at ' + now + ' ; bid request took a total of ' + (now - bidModel.AuctionStartTime) + ' ms.');
} else {
utils.logInfo('Invibes Adapter - Incorrect Placement Id: ' + bidRequest.params.placementId);
let bidModel = requestPlacement.BidModel;
let ads = requestPlacement.Ads;
if (!Array.isArray(ads) || ads.length < 1) {
if (requestPlacement.AdReason != null) {
utils.logInfo('Invibes Adapter - No ads ' + requestPlacement.AdReason);
}

utils.logInfo('Invibes Adapter - No ads available');
return null;
}

return bidResponses;
let ad = ads[0];
let size = getBiggerSize(bidRequest.sizes);

const now = Date.now();
utils.logInfo('Bid auction started at ' + bidModel.AuctionStartTime + ' . Invibes registered the bid at ' + now + ' ; bid request took a total of ' + (now - bidModel.AuctionStartTime) + ' ms.');

return {
requestId: bidRequest.bidId,
cpm: ad.BidPrice,
width: bidModel.Width || size[0],
height: bidModel.Height || size[1],
creativeId: ad.VideoExposedId,
currency: bidModel.Currency || CONSTANTS.DEFAULT_CURRENCY,
netRevenue: true,
ttl: CONSTANTS.TIME_TO_LIVE,
ad: renderCreative(bidModel)
};
}

function generateRandomId() {
Expand Down Expand Up @@ -357,17 +369,6 @@ function getCappedCampaignsAsString() {
.join(',');
}

const noop = function () {
};

function initLogger() {
if (storage.hasLocalStorage() && localStorage.InvibesDEBUG) {
return window.console;
}

return {info: noop, error: noop, log: noop, warn: noop, debug: noop};
}

function buildSyncUrl() {
let syncUrl = _customUserSync || CONSTANTS.SYNC_ENDPOINT;
syncUrl += '?visitId=' + invibes.visitId;
Expand All @@ -392,7 +393,7 @@ function readGdprConsent(gdprConsent) {

if (!gdprConsent.vendorData.gdprApplies || gdprConsent.vendorData.hasGlobalConsent) {
var index;
for (index = 0; index < invibes.purposes; ++index) {
for (index = 0; index < invibes.purposes.length; ++index) {
invibes.purposes[index] = true;
}

Expand Down Expand Up @@ -448,7 +449,13 @@ function tryCopyValueToArray(value, target, length) {
}

if (value.hasOwnProperty(prop)) {
target[i] = !((value[prop] === false || value[prop] === 'false' || value[prop] == null));
let parsedProp = parseInt(prop);
if (isNaN(parsedProp)) {
target[i] = !((value[prop] === false || value[prop] === 'false' || value[prop] == null));
} else {
target[parsedProp - 1] = !((value[prop] === false || value[prop] === 'false' || value[prop] == null));
}

i++;
}
}
Expand Down Expand Up @@ -515,8 +522,6 @@ function getVendorLegitimateInterest(vendorData) {
return {};
}

const ivLogger = initLogger();

/// Local domain cookie management =====================
invibes.Uid = {
generate: function () {
Expand Down
41 changes: 33 additions & 8 deletions test/spec/modules/invibesBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,30 @@ describe('invibesBidAdapter:', function () {
</html>`
}];

let multiResponse = {
AdPlacements: [{
Ads: [{
BidPrice: 0.5,
VideoExposedId: 123
}],
BidModel: {
BidVersion: 1,
PlacementId: '12345',
AuctionStartTime: Date.now(),
CreativeHtml: '<!-- Creative -->'
}
}]
};

let invalidResponse = {
AdPlacements: [{
Ads: [{
BidPrice: 0.5,
VideoExposedId: 123
}]
}]
};

context('when the response is not valid', function () {
it('handles response with no bids requested', function () {
let emptyResult = spec.interpretResponse({body: response});
Expand Down Expand Up @@ -781,24 +805,25 @@ describe('invibesBidAdapter:', function () {
let emptyResult = spec.interpretResponse({BidModel: {}, Ads: [{BidPrice: 1}]}, {bidRequests});
expect(emptyResult).to.be.empty;
});

it('handles response when bid model is missing', function () {
let emptyResult = spec.interpretResponse(invalidResponse);
expect(emptyResult).to.be.empty;
});
});

context('when the response is valid', function () {
it('responds with a valid bid', function () {
// top.window.invibes.setCookie('a', 'b', 370);
// top.window.invibes.setCookie('c', 'd', 0);
let result = spec.interpretResponse({body: response}, {bidRequests});
context('when the multiresponse is valid', function () {
it('responds with a valid multiresponse bid', function () {
let result = spec.interpretResponse({body: multiResponse}, {bidRequests});
expect(Object.keys(result[0])).to.have.members(Object.keys(expectedResponse[0]));
});

it('responds with a valid bid and uses logger', function () {
localStorage.InvibesDEBUG = true;
it('responds with a valid singleresponse bid', function () {
let result = spec.interpretResponse({body: response}, {bidRequests});
expect(Object.keys(result[0])).to.have.members(Object.keys(expectedResponse[0]));
});

it('does not make multiple bids', function () {
localStorage.InvibesDEBUG = false;
let result = spec.interpretResponse({body: response}, {bidRequests});
let secondResult = spec.interpretResponse({body: response}, {bidRequests});
expect(secondResult).to.be.empty;
Expand Down

0 comments on commit 952d02a

Please sign in to comment.