Skip to content

H12media Bid Adapter: added new optional params, fixes, and various integration support #6436

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 12 commits into from
Mar 25, 2021
208 changes: 126 additions & 82 deletions modules/h12mediaBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as utils from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import find from 'core-js-pure/features/array/find.js';
const BIDDER_CODE = 'h12media';
const DEFAULT_URL = 'https://bidder.h12-media.com/prebid/';
const DEFAULT_CURRENCY = 'USD';
Expand All @@ -16,21 +15,31 @@ export const spec = {
},

buildRequests: function(validBidRequests, bidderRequest) {
const requestUrl = validBidRequests[0].params.endpointdom || DEFAULT_URL;
const isiframe = !((window.self === window.top) || window.frameElement);
const isiframe = utils.inIframe();
const screenSize = getClientDimensions();
const docSize = getDocumentDimensions();

const bidrequests = validBidRequests.map((bidRequest) => {
return validBidRequests.map((bidRequest) => {
const bidderParams = bidRequest.params;
const adUnitElement = document.getElementById(bidRequest.adUnitCode);
const requestUrl = bidderParams.endpointdom || DEFAULT_URL;
let pubsubid = bidderParams.pubsubid || '';
if (pubsubid && pubsubid.length > 32) {
utils.logError('Bidder param \'pubsubid\' should be not more than 32 chars.');
pubsubid = '';
}
const pubcontainerid = bidderParams.pubcontainerid;
const adUnitElement = document.getElementById(pubcontainerid || bidRequest.adUnitCode);
const ishidden = !isVisible(adUnitElement);
const coords = {
const framePos = getFramePos();
const coords = isiframe ? {
x: framePos[0],
y: framePos[1],
} : {
x: adUnitElement && adUnitElement.getBoundingClientRect().x,
y: adUnitElement && adUnitElement.getBoundingClientRect().y,
};

return {
const bidrequest = {
bidId: bidRequest.bidId,
transactionId: bidRequest.transactionId,
adunitId: bidRequest.adUnitCode,
Expand All @@ -40,63 +49,75 @@ export const spec = {
adunitSize: bidRequest.mediaTypes.banner.sizes || [],
coords,
ishidden,
pubsubid,
pubcontainerid,
};
});

return {
method: 'POST',
url: requestUrl,
options: {withCredentials: false},
data: {
gdpr: utils.deepAccess(bidderRequest, 'gdprConsent.gdprApplies') ? Boolean(bidderRequest.gdprConsent.gdprApplies & 1) : false,
gdpr_cs: utils.deepAccess(bidderRequest, 'gdprConsent.gdprApplies') ? bidderRequest.gdprConsent.consentString : '',
topLevelUrl: window.top.location.href,
refererUrl: bidderRequest.refererInfo ? bidderRequest.refererInfo.referer : '',
isiframe,
version: '$prebid.version$',
visitorInfo: {
localTime: getLocalDateFormatted(),
dayOfWeek: new Date().getDay(),
screenWidth: screenSize[0],
screenHeight: screenSize[1],
docWidth: docSize[0],
docHeight: docSize[1],
scrollbarx: window.scrollX,
scrollbary: window.scrollY,
let windowTop;
try {
windowTop = window.top;
} catch (e) {
utils.logMessage(e);
windowTop = window;
}

return {
method: 'POST',
url: requestUrl,
options: {withCredentials: true},
data: {
gdpr: !!utils.deepAccess(bidderRequest, 'gdprConsent.gdprApplies', false),
gdpr_cs: utils.deepAccess(bidderRequest, 'gdprConsent.consentString', ''),
usp: !!utils.deepAccess(bidderRequest, 'uspConsent', false),
usp_cs: utils.deepAccess(bidderRequest, 'uspConsent', ''),
topLevelUrl: utils.deepAccess(bidderRequest, 'refererInfo.referer', ''),
refererUrl: windowTop.document.referrer,
isiframe,
version: '$prebid.version$',
ExtUserIDs: bidRequest.userId,
visitorInfo: {
localTime: getLocalDateFormatted(),
dayOfWeek: new Date().getDay(),
screenWidth: screenSize[0],
screenHeight: screenSize[1],
docWidth: docSize[0],
docHeight: docSize[1],
scrollbarx: windowTop.scrollX,
scrollbary: windowTop.scrollY,
},
bidrequest,
},
bidrequests,
},
};
};
});
},

interpretResponse: function(serverResponse, bidRequests) {
let bidResponses = [];
try {
const serverBody = serverResponse.body;
if (serverBody) {
if (serverBody.bids) {
serverBody.bids.forEach(bidBody => {
const bidRequest = find(bidRequests.data.bidrequests, bid => bid.bidId === bidBody.bidId);
const bidResponse = {
currency: serverBody.currency || DEFAULT_CURRENCY,
netRevenue: serverBody.netRevenue || DEFAULT_NET_REVENUE,
ttl: serverBody.ttl || DEFAULT_TTL,
requestId: bidBody.bidId,
cpm: bidBody.cpm,
width: bidBody.width,
height: bidBody.height,
creativeId: bidBody.creativeId,
ad: bidBody.ad,
meta: bidBody.meta,
mediaType: 'banner',
};
if (bidRequest) {
bidResponse.pubid = bidRequest.pubid;
bidResponse.placementid = bidRequest.placementid;
bidResponse.size = bidRequest.size;
}
bidResponses.push(bidResponse);
});
if (serverBody.bid) {
const bidBody = serverBody.bid;
const bidRequest = bidRequests.data.bidrequest;
const bidResponse = {
currency: serverBody.currency || DEFAULT_CURRENCY,
netRevenue: serverBody.netRevenue || DEFAULT_NET_REVENUE,
ttl: serverBody.ttl || DEFAULT_TTL,
requestId: bidBody.bidId,
cpm: bidBody.cpm,
width: bidBody.width,
height: bidBody.height,
creativeId: bidBody.creativeId,
ad: bidBody.ad,
meta: bidBody.meta,
mediaType: 'banner',
};
if (bidRequest) {
bidResponse.pubid = bidRequest.pubid;
bidResponse.placementid = bidRequest.placementid;
bidResponse.size = bidRequest.size;
}
bidResponses.push(bidResponse);
}
}
return bidResponses;
Expand All @@ -105,47 +126,50 @@ export const spec = {
}
},

getUserSyncs: function(syncOptions, serverResponses, gdprConsent) {
const serverBody = serverResponses[0].body;
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, usPrivacy) {
const syncs = [];
const uspApplies = !!utils.deepAccess(usPrivacy, 'uspConsent', false);
const uspString = utils.deepAccess(usPrivacy, 'uspConsent', '');
gdprConsent = gdprConsent || {
gdprApplies: false, consentString: '',
};

if (serverBody) {
if (serverBody.bids) {
serverBody.bids.forEach(bidBody => {
const userSyncUrls = bidBody.usersync || [];
const userSyncUrlProcess = url => {
return url
.replace('{gdpr}', gdprConsent.gdprApplies)
.replace('{gdpr_cs}', gdprConsent.consentString);
}
const userSyncUrlProcess = url => {
return url
.replace('{gdpr}', gdprConsent.gdprApplies)
.replace('{gdpr_cs}', gdprConsent.consentString)
.replace('{usp}', uspApplies)
.replace('{usp_cs}', uspString);
}

userSyncUrls.forEach(sync => {
if (syncOptions.iframeEnabled && sync.type === 'iframe' && sync.url) {
syncs.push({
type: 'iframe',
url: userSyncUrlProcess(sync.url),
});
}
if (syncOptions.pixelEnabled && sync.type === 'image' && sync.url) {
syncs.push({
type: 'image',
url: userSyncUrlProcess(sync.url),
});
}
serverResponses.forEach(serverResponse => {
const userSyncUrls = serverResponse.body.usersync || [];
userSyncUrls.forEach(sync => {
if (syncOptions.iframeEnabled && sync.type === 'iframe' && sync.url) {
syncs.push({
type: 'iframe',
url: userSyncUrlProcess(sync.url),
});
});
}
}
}
if (syncOptions.pixelEnabled && sync.type === 'image' && sync.url) {
syncs.push({
type: 'image',
url: userSyncUrlProcess(sync.url),
});
}
})
});

return syncs;
},
}

function getContext(elem) {
return elem && window.document.body.contains(elem) ? window : (window.top.document.body.contains(elem) ? top : undefined);
try {
return elem && window.document.body.contains(elem) ? window : (window.top.document.body.contains(elem) ? top : undefined);
} catch (e) {
return undefined;
}
}

function isDefined(val) {
Expand Down Expand Up @@ -206,4 +230,24 @@ function getLocalDateFormatted() {
return `${d.getFullYear()}-${two(d.getMonth() + 1)}-${two(d.getDate())} ${two(d.getHours())}:${two(d.getMinutes())}:${two(d.getSeconds())}`;
}

function getFramePos() {
let t = window;
let m = 0;
let frmLeft = 0;
let frmTop = 0;
do {
m = m + 1;
try {
if (m > 1) {
t = t.parent
}
frmLeft = frmLeft + t.frameElement.getBoundingClientRect().left;
frmTop = frmTop + t.frameElement.getBoundingClientRect().top;
} catch (o) { /* keep looping */
}
} while ((m < 100) && (t.parent !== t.self))

return [frmLeft, frmTop];
}

registerBidder(spec);
Loading