Skip to content

Commit

Permalink
Updated adf adapter to support native with type; use ortb request for…
Browse files Browse the repository at this point in the history
… natives (#9616)

Co-authored-by: Justas Pupelis <justas.pupelis@adform.com>
  • Loading branch information
Pupis and Pupis authored Mar 5, 2023
1 parent 7ff49b1 commit f67f762
Show file tree
Hide file tree
Showing 2 changed files with 319 additions and 207 deletions.
113 changes: 22 additions & 91 deletions modules/adfBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import {_map, deepAccess, deepSetValue, mergeDeep, parseSizesInput} from '../src/utils.js';
import {deepAccess, deepSetValue, mergeDeep, parseSizesInput, deepClone} from '../src/utils.js';
import {config} from '../src/config.js';
import {Renderer} from '../src/Renderer.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';

const { getConfig } = config;

Expand All @@ -16,38 +15,7 @@ const BIDDER_ALIAS = [
{ code: 'adformOpenRTB', gvlid: GVLID },
{ code: 'adform', gvlid: GVLID }
];
const NATIVE_ASSET_IDS = { 0: 'title', 2: 'icon', 3: 'image', 5: 'sponsoredBy', 4: 'body', 1: 'cta' };
const NATIVE_PARAMS = {
title: {
id: 0,
name: 'title'
},
icon: {
id: 2,
type: 1,
name: 'img'
},
image: {
id: 3,
type: 3,
name: 'img'
},
sponsoredBy: {
id: 5,
name: 'data',
type: 1
},
body: {
id: 4,
name: 'data',
type: 2
},
cta: {
id: 1,
type: 12,
name: 'data'
}
};

const OUTSTREAM_RENDERER_URL = 'https://s2.adform.net/banners/scripts/video/outstream/render.js';

export const spec = {
Expand All @@ -61,9 +29,6 @@ export const spec = {
return !!(mid || (inv && mname));
},
buildRequests: (validBidRequests, bidderRequest) => {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);

let app, site;

const commonFpd = bidderRequest.ortb2 || {};
Expand Down Expand Up @@ -123,45 +88,28 @@ export const spec = {
}
};

const assets = _map(bid.nativeParams, (bidParams, key) => {
const props = NATIVE_PARAMS[key];
const asset = {
required: bidParams.required & 1,
};
if (props) {
asset.id = props.id;
let wmin, hmin, w, h;
let aRatios = bidParams.aspect_ratios;

if (aRatios && aRatios[0]) {
aRatios = aRatios[0];
wmin = aRatios.min_width || 0;
hmin = aRatios.ratio_height * wmin / aRatios.ratio_width | 0;
if (bid.nativeOrtbRequest && bid.nativeOrtbRequest.assets) {
let assets = bid.nativeOrtbRequest.assets;
let requestAssets = [];
for (let i = 0; i < assets.length; i++) {
let asset = deepClone(assets[i]);
let img = asset.img;
if (img) {
let aspectratios = img.ext && img.ext.aspectratios;

if (aspectratios) {
let ratioWidth = parseInt(aspectratios[0].split(':')[0], 10);
let ratioHeight = parseInt(aspectratios[0].split(':')[1], 10);
img.wmin = img.wmin || 0;
img.hmin = ratioHeight * img.wmin / ratioWidth | 0;
}
}

if (bidParams.sizes) {
const sizes = flatten(bidParams.sizes);
w = sizes[0];
h = sizes[1];
}

asset[props.name] = {
len: bidParams.len,
type: props.type,
wmin,
hmin,
w,
h
};

return asset;
requestAssets.push(asset);
}
}).filter(Boolean);

if (assets.length) {
imp.native = {
request: {
assets
assets: requestAssets
}
};
}
Expand Down Expand Up @@ -268,7 +216,9 @@ export const spec = {
};

if (bidResponse.native) {
result.native = parseNative(bidResponse);
result.native = {
ortb: bidResponse.native
};
} else {
result[ mediaType === VIDEO ? 'vastXml' : 'ad' ] = bidResponse.adm;
}
Expand All @@ -286,25 +236,6 @@ export const spec = {

registerBidder(spec);

function parseNative(bid) {
const { assets, link, imptrackers, jstracker } = bid.native;
const result = {
clickUrl: link.url,
clickTrackers: link.clicktrackers || undefined,
impressionTrackers: imptrackers || undefined,
javascriptTrackers: jstracker ? [ jstracker ] : undefined
};
assets.forEach(asset => {
const kind = NATIVE_ASSET_IDS[asset.id];
const content = kind && asset[NATIVE_PARAMS[kind].name];
if (content) {
result[kind] = content.text || content.value || { url: content.url, width: content.w, height: content.h };
}
});

return result;
}

function setOnAny(collection, key) {
for (let i = 0, result; i < collection.length; i++) {
result = deepAccess(collection[i], key);
Expand Down
Loading

0 comments on commit f67f762

Please sign in to comment.