forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedianetRtdProvider.js
111 lines (97 loc) · 3.35 KB
/
medianetRtdProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {isEmptyStr, isFn, isStr, logError, mergeDeep} from '../src/utils.js';
import {loadExternalScript} from '../src/adloader.js';
import {submodule} from '../src/hook.js';
import {getGlobal} from '../src/prebidGlobal.js';
import {includes} from '../src/polyfill.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
const MODULE_NAME = 'medianet';
const SOURCE = MODULE_NAME + 'rtd';
const AD_UNIT_CODE_TARGETING_KEY = 'mnadc';
const OPEN_RTB_FIELD = 'ortb2Imp';
const getClientUrl = (customerId, domain) => `https://warp.media.net/js/tags/prebidrtdclient.js?cid=${customerId}&dn=${domain}`;
window.mnjs = window.mnjs || {};
window.mnjs.que = window.mnjs.que || [];
function init(config) {
const customerId = config.params && config.params.cid;
if (!customerId || !isStr(customerId) || isEmptyStr(customerId)) {
logError(`${SOURCE}: cid should be a string`);
return false;
}
loadRtdScript(customerId);
executeCommand(() => window.mnjs.setData({
module: 'iref',
name: 'initIRefresh',
data: {config, prebidGlobal: getGlobal()},
}, SOURCE));
return true;
}
function getBidRequestData(requestBidsProps, callback, config, userConsent) {
executeCommand(() => {
let adUnits = getAdUnits(requestBidsProps.adUnits, requestBidsProps.adUnitCodes);
const request = window.mnjs.onPrebidRequestBid({requestBidsProps, config, userConsent});
if (!request) {
callback();
return;
}
const success = (adUnitProps, openRtbProps) => {
adUnits.forEach(adUnit => {
adUnit[OPEN_RTB_FIELD] = adUnit[OPEN_RTB_FIELD] || {};
mergeDeep(adUnit[OPEN_RTB_FIELD], openRtbProps[adUnit.code]);
mergeDeep(adUnit, adUnitProps[adUnit.code]);
});
callback();
};
const error = () => callback();
request.onComplete(error, success);
});
}
function onAuctionInitEvent(auctionInit) {
executeCommand(() => window.mnjs.setData({
module: 'iref',
name: 'auctionInit',
data: {auction: auctionInit},
}, SOURCE));
}
function getTargetingData(adUnitCodes, config, consent, auction) {
const adUnits = getAdUnits(auction.adUnits, adUnitCodes);
let targetingData = {};
if (window.mnjs.loaded && isFn(window.mnjs.getTargetingData)) {
targetingData = window.mnjs.getTargetingData(adUnitCodes, adUnits, SOURCE) || {};
}
const targeting = {};
adUnitCodes.forEach(adUnitCode => {
targeting[adUnitCode] = targeting[adUnitCode] || {};
targetingData[adUnitCode] = targetingData[adUnitCode] || {};
targeting[adUnitCode] = {
// we use this to find gpt slot => prebid ad unit
[AD_UNIT_CODE_TARGETING_KEY]: adUnitCode,
...targetingData[adUnitCode]
};
});
return targeting;
}
function executeCommand(command) {
window.mnjs.que.push(command);
}
function loadRtdScript(customerId) {
const url = getClientUrl(customerId, window.location.hostname);
loadExternalScript(url, MODULE_TYPE_RTD, MODULE_NAME)
}
function getAdUnits(adUnits, adUnitCodes) {
adUnits = adUnits || getGlobal().adUnits || [];
if (adUnitCodes && adUnitCodes.length) {
adUnits = adUnits.filter(unit => includes(adUnitCodes, unit.code));
}
return adUnits;
}
export const medianetRtdModule = {
name: MODULE_NAME,
init,
getBidRequestData,
onAuctionInitEvent,
getTargetingData,
};
function registerSubModule() {
submodule('realTimeData', medianetRtdModule);
}
registerSubModule();