Skip to content

Commit 42e55d0

Browse files
adish1997adish-rakshat-vijaywargiya
authored
Medianet Bid Adapter: passing uidsAsEids in adapter and log refactor (prebid#11924)
* passing uidsAsEids in adapter and log refactor * Changes: removed sendBeacon() and added keepalive = true option for ajax. --------- Co-authored-by: adish.r <[email protected]> Co-authored-by: akshat.v <[email protected]>
1 parent 61b46af commit 42e55d0

File tree

4 files changed

+425
-56
lines changed

4 files changed

+425
-56
lines changed

modules/medianetBidAdapter.js

Lines changed: 101 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import {
2-
buildUrl,
32
deepAccess,
3+
formatQS,
44
getWindowTop,
55
isArray,
66
isEmpty,
77
isEmptyStr,
88
isStr,
99
logError,
1010
logInfo,
11-
triggerPixel
11+
safeJSONEncode,
12+
deepClone,
13+
deepSetValue
1214
} from '../src/utils.js';
1315
import {registerBidder} from '../src/adapters/bidderFactory.js';
1416
import {config} from '../src/config.js';
@@ -18,6 +20,7 @@ import {Renderer} from '../src/Renderer.js';
1820
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
1921
import {getGlobal} from '../src/prebidGlobal.js';
2022
import {getGptSlotInfoForAdUnitCode} from '../libraries/gptUtils/gptUtils.js';
23+
import {ajax} from '../src/ajax.js';
2124

2225
/**
2326
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
@@ -35,27 +38,20 @@ const SLOT_VISIBILITY = {
3538
ABOVE_THE_FOLD: 1,
3639
BELOW_THE_FOLD: 2
3740
};
38-
const EVENTS = {
41+
export const EVENTS = {
3942
TIMEOUT_EVENT_NAME: 'client_timeout',
40-
BID_WON_EVENT_NAME: 'client_bid_won'
43+
BID_WON_EVENT_NAME: 'client_bid_won',
44+
SET_TARGETING: 'client_set_targeting',
45+
BIDDER_ERROR: 'client_bidder_error'
4146
};
42-
const EVENT_PIXEL_URL = 'qsearch-a.akamaihd.net/log';
47+
export const EVENT_PIXEL_URL = 'https://navvy.media.net/log';
4348
const OUTSTREAM = 'outstream';
4449

45-
// TODO: this should be picked from bidderRequest
46-
let refererInfo = getRefererInfo();
47-
48-
let mnData = {};
50+
let pageMeta;
4951

5052
window.mnet = window.mnet || {};
5153
window.mnet.queue = window.mnet.queue || [];
5254

53-
mnData.urlData = {
54-
domain: refererInfo.domain,
55-
page: refererInfo.page,
56-
isTop: refererInfo.reachedTop
57-
};
58-
5955
const aliases = [
6056
{ code: TRUSTEDSTACK_CODE, gvlid: 1288 },
6157
];
@@ -85,20 +81,20 @@ function siteDetails(site, bidderRequest) {
8581
}
8682

8783
function getPageMeta() {
88-
if (mnData.pageMeta) {
89-
return mnData.pageMeta;
84+
if (pageMeta) {
85+
return pageMeta;
9086
}
9187
let canonicalUrl = getUrlFromSelector('link[rel="canonical"]', 'href');
9288
let ogUrl = getUrlFromSelector('meta[property="og:url"]', 'content');
9389
let twitterUrl = getUrlFromSelector('meta[name="twitter:url"]', 'content');
9490

95-
mnData.pageMeta = Object.assign({},
91+
pageMeta = Object.assign({},
9692
canonicalUrl && { 'canonical_url': canonicalUrl },
9793
ogUrl && { 'og_url': ogUrl },
9894
twitterUrl && { 'twitter_url': twitterUrl }
9995
);
10096

101-
return mnData.pageMeta;
97+
return pageMeta;
10298
}
10399

104100
function getUrlFromSelector(selector, attribute) {
@@ -338,14 +334,23 @@ function getBidderURL(bidderCode, cid) {
338334
return url + '?cid=' + encodeURIComponent(cid);
339335
}
340336

337+
function ortb2Data(ortb2, bidRequests) {
338+
const ortb2Object = deepClone(ortb2);
339+
const eids = deepAccess(bidRequests, '0.userIdAsEids');
340+
if (eids) {
341+
deepSetValue(ortb2Object, 'user.ext.eids', eids)
342+
}
343+
return ortb2Object;
344+
}
345+
341346
function generatePayload(bidRequests, bidderRequests) {
342347
return {
343348
site: siteDetails(bidRequests[0].params.site, bidderRequests),
344349
ext: extParams(bidRequests[0], bidderRequests),
345350
// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
346351
id: bidRequests[0].auctionId,
347352
imp: bidRequests.map(request => slotParams(request, bidderRequests)),
348-
ortb2: bidderRequests.ortb2,
353+
ortb2: ortb2Data(bidderRequests.ortb2, bidRequests),
349354
tmax: bidderRequests.timeout
350355
}
351356
}
@@ -363,38 +368,71 @@ function fetchCookieSyncUrls(response) {
363368
return [];
364369
}
365370

366-
function getLoggingData(event, data) {
367-
data = (isArray(data) && data) || [];
368-
369-
let params = {};
371+
function getEventData(event) {
372+
const params = {};
373+
const referrerInfo = getRefererInfo();
370374
params.logid = 'kfk';
371375
params.evtid = 'projectevents';
372376
params.project = 'prebid';
373-
params.acid = deepAccess(data, '0.auctionId') || '';
377+
params.pbver = '$prebid.version$';
374378
params.cid = getGlobal().medianetGlobals.cid || '';
375-
params.crid = data.map((adunit) => deepAccess(adunit, 'params.0.crid') || adunit.adUnitCode).join('|');
376-
params.adunit_count = data.length || 0;
377-
params.dn = mnData.urlData.domain || '';
378-
params.requrl = mnData.urlData.page || '';
379-
params.istop = mnData.urlData.isTop || '';
379+
params.dn = encodeURIComponent(referrerInfo.domain || '');
380+
params.requrl = encodeURIComponent(referrerInfo.page || '');
380381
params.event = event.name || '';
381382
params.value = event.value || '';
382383
params.rd = event.related_data || '';
384+
return params;
385+
}
383386

387+
function getBidData(bid) {
388+
const params = {};
389+
params.acid = bid.auctionId || '';
390+
params.crid = deepAccess(bid, 'params.crid') || deepAccess(bid, 'params.0.crid') || bid.adUnitCode || '';
391+
params.ext = safeJSONEncode(bid.ext) || '';
392+
393+
const rawobj = deepClone(bid);
394+
delete rawobj.ad;
395+
delete rawobj.vastXml;
396+
params.rawobj = safeJSONEncode(rawobj);
384397
return params;
385398
}
386399

387-
function logEvent (event, data) {
388-
let getParams = {
389-
protocol: 'https',
390-
hostname: EVENT_PIXEL_URL,
391-
search: getLoggingData(event, data)
392-
};
393-
triggerPixel(buildUrl(getParams));
400+
function getLoggingData(event, bids) {
401+
const logData = {};
402+
if (!isArray(bids)) {
403+
bids = [];
404+
}
405+
bids.forEach((bid) => {
406+
let bidData = getBidData(bid);
407+
Object.keys(bidData).forEach((key) => {
408+
logData[key] = logData[key] || [];
409+
logData[key].push(encodeURIComponent(bidData[key]));
410+
});
411+
});
412+
return Object.assign({}, getEventData(event), logData)
413+
}
414+
415+
function fireAjaxLog(url, payload) {
416+
ajax(url,
417+
{
418+
success: () => undefined,
419+
error: () => undefined
420+
},
421+
payload,
422+
{
423+
method: 'POST',
424+
keepalive: true
425+
}
426+
);
394427
}
395428

396-
function clearMnData() {
397-
mnData = {};
429+
function logEvent(event, data) {
430+
const logData = getLoggingData(event, data);
431+
fireAjaxLog(EVENT_PIXEL_URL, formatQS(logData));
432+
}
433+
434+
function clearPageMeta() {
435+
pageMeta = undefined;
398436
}
399437

400438
function addRenderer(bid) {
@@ -550,7 +588,30 @@ export const spec = {
550588
} catch (e) {}
551589
},
552590

553-
clearMnData,
591+
onSetTargeting: (bid) => {
592+
try {
593+
let eventData = {
594+
name: EVENTS.SET_TARGETING,
595+
value: bid.cpm
596+
};
597+
const enableSendAllBids = config.getConfig('enableSendAllBids');
598+
if (!enableSendAllBids) {
599+
logEvent(eventData, [bid]);
600+
}
601+
} catch (e) {}
602+
},
603+
604+
onBidderError: ({error, bidderRequest}) => {
605+
try {
606+
let eventData = {
607+
name: EVENTS.BIDDER_ERROR,
608+
related_data: `timedOut:${error.timedOut}|status:${error.status}|message:${error.reason.message}`
609+
};
610+
logEvent(eventData, bidderRequest.bids);
611+
} catch (e) {}
612+
},
613+
614+
clearPageMeta,
554615

555616
getWindowSize,
556617
};

src/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,14 @@ export function safeJSONParse(data) {
11101110
} catch (e) {}
11111111
}
11121112

1113+
export function safeJSONEncode(data) {
1114+
try {
1115+
return JSON.stringify(data);
1116+
} catch (e) {
1117+
return '';
1118+
}
1119+
}
1120+
11131121
/**
11141122
* Returns a memoized version of `fn`.
11151123
*

0 commit comments

Comments
 (0)