Skip to content

Commit

Permalink
Admatic adapter: host param control && Add new Bidder
Browse files Browse the repository at this point in the history
  • Loading branch information
fatihkaya84 committed Nov 5, 2022
1 parent e7f6902 commit 3c797b1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
80 changes: 69 additions & 11 deletions modules/admaticBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { getValue, logError, deepAccess, getBidIdParameter, isArray } from '../src/utils.js';
import { loadExternalScript } from '../src/adloader.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';

const ENDPOINT_URL = 'https://layer.serve.admatic.com.tr/pb';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
const SYNC_URL = 'https://cdn.serve.admatic.com.tr/showad/sync.js';
const BIDDER_CODE = 'admatic';

export const spec = {
code: 'admatic',
aliases: [
{code: 'pixad'}
],
supportedMediaTypes: ['video', 'banner'],
/**
* Determines whether or not the given bid request is valid.
Expand All @@ -19,11 +20,12 @@ export const spec = {
let isValid = false;
if (typeof bid.params !== 'undefined') {
let isValidNetworkId = _validateId(getValue(bid.params, 'networkId'));
isValid = isValidNetworkId;// && isValidTypeId;
let isValidHost = _validateString(getValue(bid.params, 'host'));
isValid = isValidNetworkId && isValidHost;
}

if (!isValid) {
logError('AdMatic networkId parameters are required. Bid aborted.');
logError(`${bid.bidder} networkId and host parameters are required. Bid aborted.`);
}
return isValid;
},
Expand All @@ -35,11 +37,13 @@ export const spec = {
*/
buildRequests: function(validBidRequests, bidderRequest) {
const bids = validBidRequests.map(buildRequestObject);
const bidderName = validBidRequests[0].bidder;
const networkId = getValue(validBidRequests[0].params, 'networkId');
const host = getValue(validBidRequests[0].params, 'host');
const currency = getValue(validBidRequests[0].params, 'currency') || 'TRY';

setTimeout(() => {
loadExternalScript(SYNC_URL, BIDDER_CODE);
loadExternalScript(SYNC_URL, bidderName);
}, bidderRequest.timeout);

const payload = {
Expand All @@ -58,14 +62,14 @@ export const spec = {
imp: bids,
ext: {
'cur': currency,
'type': 'admatic'
'bidder': bidderName
}
};

const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: ENDPOINT_URL,
url: `https://${host}/pb?bidder=${bidderName}`,
data: payloadString,
options: {
contentType: 'application/json'
Expand Down Expand Up @@ -96,22 +100,72 @@ export const spec = {
advertiserDomains: bid && bid.adomain ? bid.adomain : []
},
ttl: 360,
bidder: 'admatic',
bidder: JSON.parse(request.data).ext.bidder,
timeToRespond: 1,
requestTimestamp: 1
};

bidResponses.push(resbid);
});
};
return bidResponses;
}
};

registerBidder(spec);

function enrichSlotWithFloors(slot, bidRequest) {
try {
const slotFloors = {};

if (bidRequest.getFloor) {
if (bidRequest.mediaTypes?.banner) {
slotFloors.banner = {};
const bannerSizes = parseSizes(deepAccess(bidRequest, 'mediaTypes.banner.sizes'))
bannerSizes.forEach(bannerSize => slotFloors.banner[parseSize(bannerSize).toString()] = bidRequest.getFloor({ size: bannerSize, mediaType: BANNER }));
}

if (bidRequest.mediaTypes?.video) {
slotFloors.video = {};
const videoSizes = parseSizes(deepAccess(bidRequest, 'mediaTypes.video.playerSize'))
videoSizes.forEach(videoSize => slotFloors.video[parseSize(videoSize).toString()] = bidRequest.getFloor({ size: videoSize, mediaType: VIDEO }));
}

if (Object.keys(slotFloors).length > 0) {
if (!slot) {
slot = {}
}
Object.assign(slot, {
floors: slotFloors
});
}
}
} catch (e) {
logError('Could not parse floors from Prebid: ' + e);
}
}

function parseSizes(sizes, parser = s => s) {
if (sizes == undefined) {
return [];
}
if (Array.isArray(sizes[0])) { // is there several sizes ? (ie. [[728,90],[200,300]])
return sizes.map(size => parser(size));
}
return [parser(sizes)]; // or a single one ? (ie. [728,90])
}

function parseSize(size) {
return size[0] + 'x' + size[1];
}

function buildRequestObject(bid) {
const reqObj = {};
reqObj.size = getSizes(bid);
reqObj.id = getBidIdParameter('bidId', bid);
reqObj.floor = getValue(bid.params, 'floor') || 0.01;

enrichSlotWithFloors(reqObj, bid);

return reqObj;
}

Expand Down Expand Up @@ -144,4 +198,8 @@ function _validateId(id) {
return (parseInt(id) > 0);
}

function _validateString(str) {
return (typeof str == 'string');
}

registerBidder(spec);
4 changes: 2 additions & 2 deletions modules/admaticBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Use `admatic` as bidder.
bidder: 'admatic',
params: {
networkId: 12345,
floor: 0.5
host: 'layer.serve.admatic.com.tr'
}
}]
},{
Expand All @@ -29,7 +29,7 @@ Use `admatic` as bidder.
bidder: 'admatic',
params: {
networkId: 12345,
floor: 0.5
host: 'layer.serve.admatic.com.tr'
}
}]
}];
Expand Down
1 change: 1 addition & 0 deletions src/adloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const _approvedLoadExternalJSList = [
'debugging',
'adloox',
'admatic',
'pixad',
'criteo',
'outstream',
'adagio',
Expand Down
6 changes: 4 additions & 2 deletions test/spec/modules/admaticBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe('admaticBidAdapter', () => {
let bid = {
'bidder': 'admatic',
'params': {
'networkId': 10433394
'networkId': 10433394,
'host': 'layer.serve.admatic.com.tr'
},
'adUnitCode': 'adunit-code',
'sizes': [[300, 250], [300, 600]],
Expand All @@ -37,7 +38,8 @@ describe('admaticBidAdapter', () => {
delete bid.params;

bid.params = {
'networkId': 0
'networkId': 0,
'host': 'layer.serve.admatic.com.tr'
};

expect(spec.isBidRequestValid(bid)).to.equal(false);
Expand Down

0 comments on commit 3c797b1

Please sign in to comment.