Skip to content

Adgrid Bid Adapter : use common code #13123

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

Merged
merged 6 commits into from
May 22, 2025
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
271 changes: 99 additions & 172 deletions modules/adgridBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,206 +1,133 @@
import { _each, isEmpty, deepAccess } from '../src/utils.js';
import { config } from '../src/config.js';
import { deepSetValue, generateUUID, logInfo } from '../src/utils.js';
import { getStorageManager } from '../src/storageManager.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';

const BIDDER = Object.freeze({
CODE: 'adgrid',
HOST: 'https://api-prebid.adgrid.io',
REQUEST_METHOD: 'POST',
REQUEST_ENDPOINT: '/api/v1/auction',
SUPPORTED_MEDIA_TYPES: [BANNER, VIDEO],
});

const CURRENCY = Object.freeze({
KEY: 'currency',
US_DOLLAR: 'USD',
});

function isBidRequestValid(bid) {
if (!bid || !bid.params) {
return false;
}

return !!bid.params.domainId && !!bid.params.placement;
}
import { ortbConverter } from '../libraries/ortbConverter/converter.js'
import { createResponse, enrichImp, enrichRequest, getAmxId, getUserSyncs } from '../libraries/nexx360Utils/index.js';

/**
* Return some extra params
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').SyncOptions} SyncOptions
* @typedef {import('../src/adapters/bidderFactory.js').UserSync} UserSync
* @typedef {import('../src/adapters/bidderFactory.js').validBidRequests} validBidRequests
*/
function getAudience(validBidRequests, bidderRequest) {
const params = {
domain: deepAccess(bidderRequest, 'refererInfo.page')
};

if (deepAccess(bidderRequest, 'gdprConsent.gdprApplies')) {
params.gdpr = 1;
params.gdprConsent = deepAccess(bidderRequest, 'gdprConsent.consentString');
}
const BIDDER_CODE = 'adgrid';
const REQUEST_URL = 'https://fast.nexx360.io/adgrid';
const PAGE_VIEW_ID = generateUUID();
const BIDDER_VERSION = '2.0';
const ADGRID_KEY = 'adgrid';

if (deepAccess(bidderRequest, 'uspConsent')) {
params.usp = deepAccess(bidderRequest, 'uspConsent');
}
const ALIASES = [];

if (deepAccess(validBidRequests[0], 'schain')) {
params.schain = deepAccess(validBidRequests[0], 'schain');
}
// Define the storage manager for the Adgrid bidder
export const STORAGE = getStorageManager({
bidderCode: BIDDER_CODE,
});

if (deepAccess(validBidRequests[0], 'userId')) {
params.userIds = deepAccess(validBidRequests[0], 'userId');
/**
* Get the agdridId from local storage
* @return {object | false } false if localstorageNotEnabled
*/
export function getLocalStorage() {
if (!STORAGE.localStorageIsEnabled()) {
logInfo(`localstorage not enabled for Adgrid`);
return false;
}

if (deepAccess(validBidRequests[0], 'userIdAsEids')) {
params.userEids = deepAccess(validBidRequests[0], 'userIdAsEids');
const output = STORAGE.getDataFromLocalStorage(ADGRID_KEY);
if (output === null) {
const adgridStorage = { adgridId: generateUUID() };
STORAGE.setDataInLocalStorage(ADGRID_KEY, JSON.stringify(adgridStorage));
return adgridStorage;
}

if (bidderRequest.gppConsent) {
params.gpp = bidderRequest.gppConsent.gppString;
params.gppSid = bidderRequest.gppConsent.applicableSections?.toString();
} else if (bidderRequest.ortb2?.regs?.gpp) {
params.gpp = bidderRequest.ortb2.regs.gpp;
params.gppSid = bidderRequest.ortb2.regs.gpp_sid;
try {
return JSON.parse(output);
} catch (e) {
return false;
}

return params;
}

function buildRequests(validBidRequests, bidderRequest) {
const currencyObj = config.getConfig(CURRENCY.KEY);
const currency = (currencyObj && currencyObj.adServerCurrency) ? currencyObj.adServerCurrency : 'USD';
const bids = [];

_each(validBidRequests, bid => {
bids.push(getBidData(bid))
});

const bidsParams = Object.assign({}, {
url: window.location.href,
timeout: bidderRequest.timeout,
ts: new Date().getTime(),
device: {
size: [
window.screen.width,
window.screen.height
]
},
bids
});

// Add currency if not USD
if (currency != null && currency != CURRENCY.US_DOLLAR) {
bidsParams.cur = currency;
}

bidsParams.audience = getAudience(validBidRequests, bidderRequest);

// Passing geo location data if found in prebid config
bidsParams.geodata = config.getConfig('adgGeodata') || {};

return Object.assign({}, bidderRequest, {
method: BIDDER.REQUEST_METHOD,
url: `${BIDDER.HOST}${BIDDER.REQUEST_ENDPOINT}`,
data: bidsParams,
currency: currency,
options: {
withCredentials: true,
contentType: 'application/json'
}
});
}

function interpretResponse(response, bidRequest) {
let bids = response.body;
const bidResponses = [];

if (isEmpty(bids)) {
return bidResponses;
}

if (typeof bids !== 'object') {
return bidResponses;
}

bids = bids.bids;

bids.forEach((adUnit) => {
const bidResponse = {
requestId: adUnit.bidId,
cpm: Number(adUnit.cpm),
width: adUnit.width,
height: adUnit.height,
ttl: 300,
creativeId: adUnit.creativeId,
netRevenue: true,
currency: adUnit.currency || bidRequest.currency,
mediaType: adUnit.mediaType
};

if (adUnit.mediaType == 'video') {
if (adUnit.admUrl) {
bidResponse.vastUrl = adUnit.admUrl;
} else {
bidResponse.vastXml = adUnit.adm;
}
} else {
bidResponse.ad = adUnit.ad;
}

bidResponses.push(bidResponse);
});
const converter = ortbConverter({
context: {
netRevenue: true, // or false if your adapter should set bidResponse.netRevenue = false
ttl: 90, // default bidResponse.ttl (when not specified in ORTB response.seatbid[].bid[].exp)
},
imp(buildImp, bidRequest, context) {
let imp = buildImp(bidRequest, context);
imp = enrichImp(imp, bidRequest);
if (bidRequest.params.domainId) deepSetValue(imp, 'ext.adgrid.domainId', bidRequest.params.domainId);
if (bidRequest.params.placement) deepSetValue(imp, 'ext.adgrid.placement', bidRequest.params.placement);
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
let request = buildRequest(imps, bidderRequest, context);
const amxId = getAmxId(STORAGE, BIDDER_CODE);
request = enrichRequest(request, amxId, bidderRequest, PAGE_VIEW_ID, BIDDER_VERSION);
return request;
},
});

return bidResponses;
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
function isBidRequestValid(bid) {
if (!bid || !bid.params) return false;
if (typeof bid.params.domainId !== 'number') return false;
if (typeof bid.params.placement !== 'string') return false;
return true;
}

function getBidData(bid) {
const bidData = {
requestId: bid.bidId,
tid: bid.ortb2Imp?.ext?.tid,
deviceW: bid.ortb2?.device?.w,
deviceH: bid.ortb2?.device?.h,
deviceUa: bid.ortb2?.device?.ua,
domain: bid.ortb2?.site?.publisher?.domain,
domainId: bid.params.domainId,
placement: bid.params.placement,
code: bid.adUnitCode
};

if (bid.mediaTypes != null) {
if (bid.mediaTypes.banner != null) {
bidData.mediaType = 'banner';
bidData.sizes = bid.mediaTypes.banner.sizes;
} else if (bid.mediaTypes.video != null) {
bidData.mediaType = 'video';
bidData.sizes = bid.mediaTypes.video.playerSize;
bidData.videoData = bid.mediaTypes.video;
bidData.videoParams = bid.params.video;
}
/**
* Make a server request from the list of BidRequests.
*
* @return ServerRequest Info describing the request to the server.
*/
function buildRequests(bidRequests, bidderRequest) {
const data = converter.toORTB({ bidRequests, bidderRequest })
return {
method: 'POST',
url: REQUEST_URL,
data,
}

return bidData;
}

/**
* Register the user sync pixels/iframe which should be dropped after the auction.
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
function getUserSyncs(syncOptions, response, gdprConsent, uspConsent) {
if (typeof response !== 'object' || response === null || response.length === 0) {
function interpretResponse(serverResponse) {
const respBody = serverResponse.body;
if (!respBody || !Array.isArray(respBody.seatbid)) {
return [];
}

if (response[0]?.body?.ext?.cookies && typeof response[0].body.ext.cookies === 'object') {
return response[0].body.ext.cookies.slice(0, 5);
} else {
return [];
const responses = [];
for (let i = 0; i < respBody.seatbid.length; i++) {
const seatbid = respBody.seatbid[i];
for (let j = 0; j < seatbid.bid.length; j++) {
const bid = seatbid.bid[j];
const response = createResponse(bid, respBody);
responses.push(response);
}
}
};
return responses;
}

export const spec = {
code: BIDDER.CODE,
code: BIDDER_CODE,
aliases: ALIASES,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid,
buildRequests,
interpretResponse,
supportedMediaTypes: BIDDER.SUPPORTED_MEDIA_TYPES,
getUserSyncs
getUserSyncs,
};

registerBidder(spec);
Loading