Skip to content

Commit

Permalink
Relaido Bid Adapter: Modified to response ads in a single API request. (
Browse files Browse the repository at this point in the history
prebid#7598)

* add relaido adapter

* remove event listener

* fixed UserSyncs and e.data

* fix conflicts

* make single requests.

Co-authored-by: ishigami_shingo <[email protected]>
Co-authored-by: cmertv-sishigami <[email protected]>
Co-authored-by: t_bun <[email protected]>
  • Loading branch information
4 people authored and Chris Pabst committed Jan 10, 2022
1 parent e23c040 commit ab2a932
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 128 deletions.
151 changes: 86 additions & 65 deletions modules/relaidoBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getStorageManager } from '../src/storageManager.js';

const BIDDER_CODE = 'relaido';
const BIDDER_DOMAIN = 'api.relaido.jp';
const ADAPTER_VERSION = '1.0.6';
const ADAPTER_VERSION = '1.0.7';
const DEFAULT_TTL = 300;
const UUID_KEY = 'relaido_uuid';

Expand All @@ -31,14 +31,14 @@ function isBidRequestValid(bid) {
}

function buildRequests(validBidRequests, bidderRequest) {
let bidRequests = [];
const bids = [];
let imuid = null;
let bidDomain = null;
let bidder = null;
let count = null;

for (let i = 0; i < validBidRequests.length; i++) {
const bidRequest = validBidRequests[i];
const placementId = getBidIdParameter('placementId', bidRequest.params);
const bidDomain = bidRequest.params.domain || BIDDER_DOMAIN;
const bidUrl = `https://${bidDomain}/bid/v1/prebid/${placementId}`;
const uuid = getUuid();
let mediaType = '';
let width = 0;
let height = 0;
Expand All @@ -55,46 +55,63 @@ function buildRequests(validBidRequests, bidderRequest) {
mediaType = BANNER;
}

let payload = {
version: ADAPTER_VERSION,
timeout_ms: bidderRequest.timeout,
ad_unit_code: bidRequest.adUnitCode,
auction_id: bidRequest.auctionId,
bidder: bidRequest.bidder,
bidder_request_id: bidRequest.bidderRequestId,
bid_requests_count: bidRequest.bidRequestsCount,
bid_id: bidRequest.bidId,
transaction_id: bidRequest.transactionId,
media_type: mediaType,
uuid: uuid,
width: width,
height: height,
pv: '$prebid.version$'
};
if (!imuid) {
const pickImuid = deepAccess(bidRequest, 'userId.imuid');
if (pickImuid) {
imuid = pickImuid;
}
}

if (!bidDomain) {
bidDomain = bidRequest.params.domain;
}

if (!bidder) {
bidder = bidRequest.bidder
}

if (!bidder) {
bidder = bidRequest.bidder
}

const imuid = deepAccess(bidRequest, 'userId.imuid');
if (imuid) {
payload.imuid = imuid;
if (!count) {
count = bidRequest.bidRequestsCount;
}

// It may not be encoded, so add it at the end of the payload
payload.ref = bidderRequest.refererInfo.referer;

bidRequests.push({
method: 'GET',
url: bidUrl,
data: payload,
options: {
withCredentials: true
},
bidId: bidRequest.bidId,
bids.push({
bid_id: bidRequest.bidId,
placement_id: getBidIdParameter('placementId', bidRequest.params),
transaction_id: bidRequest.transactionId,
bidder_request_id: bidRequest.bidderRequestId,
ad_unit_code: bidRequest.adUnitCode,
auction_id: bidRequest.auctionId,
player: bidRequest.params.player,
width: payload.width,
height: payload.height,
mediaType: payload.media_type
width: width,
height: height,
media_type: mediaType
});
}
return bidRequests;

const data = JSON.stringify({
version: ADAPTER_VERSION,
bids: bids,
timeout_ms: bidderRequest.timeout,
bidder: bidder,
bid_requests_count: count,
uuid: getUuid(),
pv: '$prebid.version$',
imuid: imuid,
ref: bidderRequest.refererInfo.referer
})

return {
method: 'POST',
url: `https://${bidDomain || BIDDER_DOMAIN}/bid/v1/sprebid`,
options: {
withCredentials: true
},
data: data
};
}

function interpretResponse(serverResponse, bidRequest) {
Expand All @@ -105,34 +122,38 @@ function interpretResponse(serverResponse, bidRequest) {
}

const playerUrl = bidRequest.player || body.playerUrl;
const mediaType = bidRequest.mediaType || VIDEO;

let bidResponse = {
requestId: bidRequest.bidId,
width: bidRequest.width,
height: bidRequest.height,
cpm: body.price,
currency: body.currency,
creativeId: body.creativeId,
dealId: body.dealId || '',
ttl: body.ttl || DEFAULT_TTL,
netRevenue: true,
mediaType: mediaType,
meta: {
advertiserDomains: body.adomain || [],
mediaType: VIDEO

for (const res of body.ads) {
let bidResponse = {
requestId: res.bidId,
width: res.width,
height: res.height,
cpm: res.price,
currency: res.currency,
creativeId: res.creativeId,
dealId: body.dealId || '',
ttl: body.ttl || DEFAULT_TTL,
netRevenue: true,
mediaType: res.mediaType || VIDEO,
meta: {
advertiserDomains: res.adomain || [],
mediaType: VIDEO
}
};

if (bidResponse.mediaType === VIDEO) {
bidResponse.vastXml = res.vast;
bidResponse.renderer = newRenderer(res.bidId, playerUrl);
} else {
const playerTag = createPlayerTag(playerUrl);
const renderTag = createRenderTag(res.width, res.height, res.vast);
bidResponse.ad = `<div id="rop-prebid">${playerTag}${renderTag}</div>`;
}
};
if (mediaType === VIDEO) {
bidResponse.vastXml = body.vast;
bidResponse.renderer = newRenderer(bidRequest.bidId, playerUrl);
} else {
const playerTag = createPlayerTag(playerUrl);
const renderTag = createRenderTag(bidRequest.width, bidRequest.height, body.vast);
bidResponse.ad = `<div id="rop-prebid">${playerTag}${renderTag}</div>`;
bidResponses.push(bidResponse);
}
bidResponses.push(bidResponse);

// eslint-disable-next-line no-console
console.log(JSON.stringify(bidResponses));
return bidResponses;
}

Expand Down
134 changes: 71 additions & 63 deletions test/spec/modules/relaidoBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,34 @@ describe('RelaidoAdapter', function () {
serverResponse = {
body: {
status: 'ok',
price: 500,
model: 'vcpm',
currency: 'JPY',
creativeId: 1000,
uuid: relaido_uuid,
vast: '<VAST version="3.0"><Ad><InLine></InLine></Ad></VAST>',
ads: [{
placementId: 100000,
width: 640,
height: 360,
bidId: '2ed93003f7bb99',
price: 500,
model: 'vcpm',
currency: 'JPY',
creativeId: 1000,
vast: '<VAST version="3.0"><Ad><InLine></InLine></Ad></VAST>',
syncUrl: 'https://relaido/sync.html',
adomain: ['relaido.co.jp', 'www.cmertv.co.jp'],
mediaType: 'video'
}],
playerUrl: 'https://relaido/player.js',
syncUrl: 'https://relaido/sync.html',
adomain: ['relaido.co.jp', 'www.cmertv.co.jp']
syncUrl: 'https://api-dev.ulizaex.com/tr/v1/prebid/sync.html',
uuid: relaido_uuid,
}
};
serverRequest = {
method: 'GET',
bidId: bidRequest.bidId,
width: bidRequest.mediaTypes.video.playerSize[0][0],
height: bidRequest.mediaTypes.video.playerSize[0][1],
mediaType: 'video',
method: 'POST',
data: {
bids: [{
bidId: bidRequest.bidId,
width: bidRequest.mediaTypes.video.playerSize[0][0],
height: bidRequest.mediaTypes.video.playerSize[0][1],
mediaType: 'video'}]
}
};
});

Expand Down Expand Up @@ -195,28 +206,23 @@ describe('RelaidoAdapter', function () {
describe('spec.buildRequests', function () {
it('should build bid requests by video', function () {
const bidRequests = spec.buildRequests([bidRequest], bidderRequest);
expect(bidRequests).to.have.lengthOf(1);
const request = bidRequests[0];
expect(request.method).to.equal('GET');
expect(request.url).to.equal('https://api.relaido.jp/bid/v1/prebid/100000');
expect(request.bidId).to.equal(bidRequest.bidId);
expect(request.width).to.equal(bidRequest.mediaTypes.video.playerSize[0][0]);
expect(request.height).to.equal(bidRequest.mediaTypes.video.playerSize[0][1]);
expect(request.mediaType).to.equal('video');
expect(request.data.ref).to.equal(bidderRequest.refererInfo.referer);
expect(request.data.timeout_ms).to.equal(bidderRequest.timeout);
expect(request.data.ad_unit_code).to.equal(bidRequest.adUnitCode);
expect(request.data.auction_id).to.equal(bidRequest.auctionId);
expect(request.data.bidder).to.equal(bidRequest.bidder);
expect(request.data.bidder_request_id).to.equal(bidRequest.bidderRequestId);
expect(request.data.bid_requests_count).to.equal(bidRequest.bidRequestsCount);
expect(request.data.bid_id).to.equal(bidRequest.bidId);
expect(request.data.transaction_id).to.equal(bidRequest.transactionId);
expect(request.data.media_type).to.equal('video');
expect(request.data.uuid).to.equal(relaido_uuid);
expect(request.data.width).to.equal(bidRequest.mediaTypes.video.playerSize[0][0]);
expect(request.data.height).to.equal(bidRequest.mediaTypes.video.playerSize[0][1]);
expect(request.data.pv).to.equal('$prebid.version$');
const data = JSON.parse(bidRequests.data);
expect(data.bids).to.have.lengthOf(1);
const request = data.bids[0];
expect(bidRequests.method).to.equal('POST');
expect(bidRequests.url).to.equal('https://api.relaido.jp/bid/v1/sprebid');
expect(data.ref).to.equal(bidderRequest.refererInfo.referer);
expect(data.timeout_ms).to.equal(bidderRequest.timeout);
expect(request.ad_unit_code).to.equal(bidRequest.adUnitCode);
expect(request.auction_id).to.equal(bidRequest.auctionId);
expect(data.bidder).to.equal(bidRequest.bidder);
expect(request.bidder_request_id).to.equal(bidRequest.bidderRequestId);
expect(data.bid_requests_count).to.equal(bidRequest.bidRequestsCount);
expect(request.bid_id).to.equal(bidRequest.bidId);
expect(request.transaction_id).to.equal(bidRequest.transactionId);
expect(request.media_type).to.equal('video');
expect(data.uuid).to.equal(relaido_uuid);
expect(data.pv).to.equal('$prebid.version$');
});

it('should build bid requests by banner', function () {
Expand All @@ -236,9 +242,10 @@ describe('RelaidoAdapter', function () {
}
};
const bidRequests = spec.buildRequests([bidRequest], bidderRequest);
expect(bidRequests).to.have.lengthOf(1);
const request = bidRequests[0];
expect(request.mediaType).to.equal('banner');
const data = JSON.parse(bidRequests.data);
expect(data.bids).to.have.lengthOf(1);
const request = data.bids[0];
expect(request.media_type).to.equal('banner');
});

it('should take 1x1 size', function () {
Expand All @@ -258,17 +265,18 @@ describe('RelaidoAdapter', function () {
}
};
const bidRequests = spec.buildRequests([bidRequest], bidderRequest);
expect(bidRequests).to.have.lengthOf(1);
const request = bidRequests[0];
const data = JSON.parse(bidRequests.data);
expect(data.bids).to.have.lengthOf(1);
const request = data.bids[0];

expect(request.width).to.equal(1);
});

it('The referrer should be the last', function () {
const bidRequests = spec.buildRequests([bidRequest], bidderRequest);
expect(bidRequests).to.have.lengthOf(1);
const request = bidRequests[0];
const keys = Object.keys(request.data);
const data = JSON.parse(bidRequests.data);
expect(data.bids).to.have.lengthOf(1);
const keys = Object.keys(data);
expect(keys[0]).to.equal('version');
expect(keys[keys.length - 1]).to.equal('ref');
});
Expand All @@ -277,9 +285,9 @@ describe('RelaidoAdapter', function () {
bidRequest.userId = {}
bidRequest.userId.imuid = 'i.tjHcK_7fTcqnbrS_YA2vaw';
const bidRequests = spec.buildRequests([bidRequest], bidderRequest);
expect(bidRequests).to.have.lengthOf(1);
const request = bidRequests[0];
expect(request.data.imuid).to.equal('i.tjHcK_7fTcqnbrS_YA2vaw');
const data = JSON.parse(bidRequests.data);
expect(data.bids).to.have.lengthOf(1);
expect(data.imuid).to.equal('i.tjHcK_7fTcqnbrS_YA2vaw');
});
});

Expand All @@ -288,29 +296,29 @@ describe('RelaidoAdapter', function () {
const bidResponses = spec.interpretResponse(serverResponse, serverRequest);
expect(bidResponses).to.have.lengthOf(1);
const response = bidResponses[0];
expect(response.requestId).to.equal(serverRequest.bidId);
expect(response.width).to.equal(serverRequest.width);
expect(response.height).to.equal(serverRequest.height);
expect(response.cpm).to.equal(serverResponse.body.price);
expect(response.currency).to.equal(serverResponse.body.currency);
expect(response.creativeId).to.equal(serverResponse.body.creativeId);
expect(response.vastXml).to.equal(serverResponse.body.vast);
expect(response.meta.advertiserDomains).to.equal(serverResponse.body.adomain);
expect(response.requestId).to.equal(serverRequest.data.bids[0].bidId);
expect(response.width).to.equal(serverRequest.data.bids[0].width);
expect(response.height).to.equal(serverRequest.data.bids[0].height);
expect(response.cpm).to.equal(serverResponse.body.ads[0].price);
expect(response.currency).to.equal(serverResponse.body.ads[0].currency);
expect(response.creativeId).to.equal(serverResponse.body.ads[0].creativeId);
expect(response.vastXml).to.equal(serverResponse.body.ads[0].vast);
expect(response.meta.advertiserDomains).to.equal(serverResponse.body.ads[0].adomain);
expect(response.meta.mediaType).to.equal(VIDEO);
expect(response.ad).to.be.undefined;
});

it('should build bid response by banner', function () {
serverRequest.mediaType = 'banner';
serverResponse.body.ads[0].mediaType = 'banner';
const bidResponses = spec.interpretResponse(serverResponse, serverRequest);
expect(bidResponses).to.have.lengthOf(1);
const response = bidResponses[0];
expect(response.requestId).to.equal(serverRequest.bidId);
expect(response.width).to.equal(serverRequest.width);
expect(response.height).to.equal(serverRequest.height);
expect(response.cpm).to.equal(serverResponse.body.price);
expect(response.currency).to.equal(serverResponse.body.currency);
expect(response.creativeId).to.equal(serverResponse.body.creativeId);
expect(response.requestId).to.equal(serverRequest.data.bids[0].bidId);
expect(response.width).to.equal(serverRequest.data.bids[0].width);
expect(response.height).to.equal(serverRequest.data.bids[0].height);
expect(response.cpm).to.equal(serverResponse.body.ads[0].price);
expect(response.currency).to.equal(serverResponse.body.ads[0].currency);
expect(response.creativeId).to.equal(serverResponse.body.ads[0].creativeId);
expect(response.vastXml).to.be.undefined;
expect(response.ad).to.include(`<div id="rop-prebid">`);
expect(response.ad).to.include(`<script src="https://relaido/player.js"></script>`);
Expand Down Expand Up @@ -370,8 +378,8 @@ describe('RelaidoAdapter', function () {
it('Should create nurl pixel if bid nurl', function () {
let bid = {
bidder: bidRequest.bidder,
creativeId: serverResponse.body.creativeId,
cpm: serverResponse.body.price,
creativeId: serverResponse.body.ads[0].creativeId,
cpm: serverResponse.body.ads[0].price,
params: [bidRequest.params],
auctionId: bidRequest.auctionId,
requestId: bidRequest.bidId,
Expand Down

0 comments on commit ab2a932

Please sign in to comment.