Skip to content

Commit bb97526

Browse files
h12mediastsepelin
authored andcommitted
H12media Bid Adapter: added new optional params, fixes, and various integration support (prebid#6436)
* Change module H12 Media * Change module H12 Media * Change module H12 Media * Change module H12 Media * Update module H12 Media * Update module H12 Media * Update module H12 Media * Update module H12 Media * Update module H12 Media * Update module H12 Media
1 parent 3619f1e commit bb97526

File tree

2 files changed

+218
-125
lines changed

2 files changed

+218
-125
lines changed

modules/h12mediaBidAdapter.js

Lines changed: 126 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as utils from '../src/utils.js';
22
import { registerBidder } from '../src/adapters/bidderFactory.js';
3-
import find from 'core-js-pure/features/array/find.js';
43
const BIDDER_CODE = 'h12media';
54
const DEFAULT_URL = 'https://bidder.h12-media.com/prebid/';
65
const DEFAULT_CURRENCY = 'USD';
@@ -16,21 +15,31 @@ export const spec = {
1615
},
1716

1817
buildRequests: function(validBidRequests, bidderRequest) {
19-
const requestUrl = validBidRequests[0].params.endpointdom || DEFAULT_URL;
20-
const isiframe = !((window.self === window.top) || window.frameElement);
18+
const isiframe = utils.inIframe();
2119
const screenSize = getClientDimensions();
2220
const docSize = getDocumentDimensions();
2321

24-
const bidrequests = validBidRequests.map((bidRequest) => {
22+
return validBidRequests.map((bidRequest) => {
2523
const bidderParams = bidRequest.params;
26-
const adUnitElement = document.getElementById(bidRequest.adUnitCode);
24+
const requestUrl = bidderParams.endpointdom || DEFAULT_URL;
25+
let pubsubid = bidderParams.pubsubid || '';
26+
if (pubsubid && pubsubid.length > 32) {
27+
utils.logError('Bidder param \'pubsubid\' should be not more than 32 chars.');
28+
pubsubid = '';
29+
}
30+
const pubcontainerid = bidderParams.pubcontainerid;
31+
const adUnitElement = document.getElementById(pubcontainerid || bidRequest.adUnitCode);
2732
const ishidden = !isVisible(adUnitElement);
28-
const coords = {
33+
const framePos = getFramePos();
34+
const coords = isiframe ? {
35+
x: framePos[0],
36+
y: framePos[1],
37+
} : {
2938
x: adUnitElement && adUnitElement.getBoundingClientRect().x,
3039
y: adUnitElement && adUnitElement.getBoundingClientRect().y,
3140
};
3241

33-
return {
42+
const bidrequest = {
3443
bidId: bidRequest.bidId,
3544
transactionId: bidRequest.transactionId,
3645
adunitId: bidRequest.adUnitCode,
@@ -40,63 +49,75 @@ export const spec = {
4049
adunitSize: bidRequest.mediaTypes.banner.sizes || [],
4150
coords,
4251
ishidden,
52+
pubsubid,
53+
pubcontainerid,
4354
};
44-
});
4555

46-
return {
47-
method: 'POST',
48-
url: requestUrl,
49-
options: {withCredentials: false},
50-
data: {
51-
gdpr: utils.deepAccess(bidderRequest, 'gdprConsent.gdprApplies') ? Boolean(bidderRequest.gdprConsent.gdprApplies & 1) : false,
52-
gdpr_cs: utils.deepAccess(bidderRequest, 'gdprConsent.gdprApplies') ? bidderRequest.gdprConsent.consentString : '',
53-
topLevelUrl: window.top.location.href,
54-
refererUrl: bidderRequest.refererInfo ? bidderRequest.refererInfo.referer : '',
55-
isiframe,
56-
version: '$prebid.version$',
57-
visitorInfo: {
58-
localTime: getLocalDateFormatted(),
59-
dayOfWeek: new Date().getDay(),
60-
screenWidth: screenSize[0],
61-
screenHeight: screenSize[1],
62-
docWidth: docSize[0],
63-
docHeight: docSize[1],
64-
scrollbarx: window.scrollX,
65-
scrollbary: window.scrollY,
56+
let windowTop;
57+
try {
58+
windowTop = window.top;
59+
} catch (e) {
60+
utils.logMessage(e);
61+
windowTop = window;
62+
}
63+
64+
return {
65+
method: 'POST',
66+
url: requestUrl,
67+
options: {withCredentials: true},
68+
data: {
69+
gdpr: !!utils.deepAccess(bidderRequest, 'gdprConsent.gdprApplies', false),
70+
gdpr_cs: utils.deepAccess(bidderRequest, 'gdprConsent.consentString', ''),
71+
usp: !!utils.deepAccess(bidderRequest, 'uspConsent', false),
72+
usp_cs: utils.deepAccess(bidderRequest, 'uspConsent', ''),
73+
topLevelUrl: utils.deepAccess(bidderRequest, 'refererInfo.referer', ''),
74+
refererUrl: windowTop.document.referrer,
75+
isiframe,
76+
version: '$prebid.version$',
77+
ExtUserIDs: bidRequest.userId,
78+
visitorInfo: {
79+
localTime: getLocalDateFormatted(),
80+
dayOfWeek: new Date().getDay(),
81+
screenWidth: screenSize[0],
82+
screenHeight: screenSize[1],
83+
docWidth: docSize[0],
84+
docHeight: docSize[1],
85+
scrollbarx: windowTop.scrollX,
86+
scrollbary: windowTop.scrollY,
87+
},
88+
bidrequest,
6689
},
67-
bidrequests,
68-
},
69-
};
90+
};
91+
});
7092
},
7193

7294
interpretResponse: function(serverResponse, bidRequests) {
7395
let bidResponses = [];
7496
try {
7597
const serverBody = serverResponse.body;
7698
if (serverBody) {
77-
if (serverBody.bids) {
78-
serverBody.bids.forEach(bidBody => {
79-
const bidRequest = find(bidRequests.data.bidrequests, bid => bid.bidId === bidBody.bidId);
80-
const bidResponse = {
81-
currency: serverBody.currency || DEFAULT_CURRENCY,
82-
netRevenue: serverBody.netRevenue || DEFAULT_NET_REVENUE,
83-
ttl: serverBody.ttl || DEFAULT_TTL,
84-
requestId: bidBody.bidId,
85-
cpm: bidBody.cpm,
86-
width: bidBody.width,
87-
height: bidBody.height,
88-
creativeId: bidBody.creativeId,
89-
ad: bidBody.ad,
90-
meta: bidBody.meta,
91-
mediaType: 'banner',
92-
};
93-
if (bidRequest) {
94-
bidResponse.pubid = bidRequest.pubid;
95-
bidResponse.placementid = bidRequest.placementid;
96-
bidResponse.size = bidRequest.size;
97-
}
98-
bidResponses.push(bidResponse);
99-
});
99+
if (serverBody.bid) {
100+
const bidBody = serverBody.bid;
101+
const bidRequest = bidRequests.data.bidrequest;
102+
const bidResponse = {
103+
currency: serverBody.currency || DEFAULT_CURRENCY,
104+
netRevenue: serverBody.netRevenue || DEFAULT_NET_REVENUE,
105+
ttl: serverBody.ttl || DEFAULT_TTL,
106+
requestId: bidBody.bidId,
107+
cpm: bidBody.cpm,
108+
width: bidBody.width,
109+
height: bidBody.height,
110+
creativeId: bidBody.creativeId,
111+
ad: bidBody.ad,
112+
meta: bidBody.meta,
113+
mediaType: 'banner',
114+
};
115+
if (bidRequest) {
116+
bidResponse.pubid = bidRequest.pubid;
117+
bidResponse.placementid = bidRequest.placementid;
118+
bidResponse.size = bidRequest.size;
119+
}
120+
bidResponses.push(bidResponse);
100121
}
101122
}
102123
return bidResponses;
@@ -105,47 +126,50 @@ export const spec = {
105126
}
106127
},
107128

108-
getUserSyncs: function(syncOptions, serverResponses, gdprConsent) {
109-
const serverBody = serverResponses[0].body;
129+
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, usPrivacy) {
110130
const syncs = [];
131+
const uspApplies = !!utils.deepAccess(usPrivacy, 'uspConsent', false);
132+
const uspString = utils.deepAccess(usPrivacy, 'uspConsent', '');
111133
gdprConsent = gdprConsent || {
112134
gdprApplies: false, consentString: '',
113135
};
114136

115-
if (serverBody) {
116-
if (serverBody.bids) {
117-
serverBody.bids.forEach(bidBody => {
118-
const userSyncUrls = bidBody.usersync || [];
119-
const userSyncUrlProcess = url => {
120-
return url
121-
.replace('{gdpr}', gdprConsent.gdprApplies)
122-
.replace('{gdpr_cs}', gdprConsent.consentString);
123-
}
137+
const userSyncUrlProcess = url => {
138+
return url
139+
.replace('{gdpr}', gdprConsent.gdprApplies)
140+
.replace('{gdpr_cs}', gdprConsent.consentString)
141+
.replace('{usp}', uspApplies)
142+
.replace('{usp_cs}', uspString);
143+
}
124144

125-
userSyncUrls.forEach(sync => {
126-
if (syncOptions.iframeEnabled && sync.type === 'iframe' && sync.url) {
127-
syncs.push({
128-
type: 'iframe',
129-
url: userSyncUrlProcess(sync.url),
130-
});
131-
}
132-
if (syncOptions.pixelEnabled && sync.type === 'image' && sync.url) {
133-
syncs.push({
134-
type: 'image',
135-
url: userSyncUrlProcess(sync.url),
136-
});
137-
}
145+
serverResponses.forEach(serverResponse => {
146+
const userSyncUrls = serverResponse.body.usersync || [];
147+
userSyncUrls.forEach(sync => {
148+
if (syncOptions.iframeEnabled && sync.type === 'iframe' && sync.url) {
149+
syncs.push({
150+
type: 'iframe',
151+
url: userSyncUrlProcess(sync.url),
138152
});
139-
});
140-
}
141-
}
153+
}
154+
if (syncOptions.pixelEnabled && sync.type === 'image' && sync.url) {
155+
syncs.push({
156+
type: 'image',
157+
url: userSyncUrlProcess(sync.url),
158+
});
159+
}
160+
})
161+
});
142162

143163
return syncs;
144164
},
145165
}
146166

147167
function getContext(elem) {
148-
return elem && window.document.body.contains(elem) ? window : (window.top.document.body.contains(elem) ? top : undefined);
168+
try {
169+
return elem && window.document.body.contains(elem) ? window : (window.top.document.body.contains(elem) ? top : undefined);
170+
} catch (e) {
171+
return undefined;
172+
}
149173
}
150174

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

233+
function getFramePos() {
234+
let t = window;
235+
let m = 0;
236+
let frmLeft = 0;
237+
let frmTop = 0;
238+
do {
239+
m = m + 1;
240+
try {
241+
if (m > 1) {
242+
t = t.parent
243+
}
244+
frmLeft = frmLeft + t.frameElement.getBoundingClientRect().left;
245+
frmTop = frmTop + t.frameElement.getBoundingClientRect().top;
246+
} catch (o) { /* keep looping */
247+
}
248+
} while ((m < 100) && (t.parent !== t.self))
249+
250+
return [frmLeft, frmTop];
251+
}
252+
209253
registerBidder(spec);

0 commit comments

Comments
 (0)