forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreenbidsRtdProvider.js
157 lines (142 loc) · 5.04 KB
/
greenbidsRtdProvider.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { logError, logInfo, logWarn, logMessage, deepClone, generateUUID, deepSetValue, deepAccess, getParameterByName } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js';
import * as events from '../src/events.js';
import { EVENTS } from '../src/constants.js';
const MODULE_NAME = 'greenbidsRtdProvider';
const MODULE_VERSION = '2.0.1';
const ENDPOINT = 'https://t.greenbids.ai';
const rtdOptions = {};
function init(moduleConfig) {
let params = moduleConfig?.params;
if (!params?.pbuid) {
logError('Greenbids pbuid is not set!');
return false;
} else {
rtdOptions.pbuid = params?.pbuid;
rtdOptions.timeout = params?.timeout || 200;
return true;
}
}
function onAuctionInitEvent(auctionDetails) {
/* Emitting one billing event per auction */
let defaultId = 'default_id';
let greenbidsId = deepAccess(auctionDetails.adUnits[0], 'ortb2Imp.ext.greenbids.greenbidsId', defaultId);
/* greenbids was successfully called so we emit the event */
if (greenbidsId !== defaultId) {
events.emit(EVENTS.BILLABLE_EVENT, {
type: 'auction',
billingId: generateUUID(),
auctionId: auctionDetails.auctionId,
vendor: MODULE_NAME
});
}
}
function getBidRequestData(reqBidsConfigObj, callback, config, userConsent) {
let greenbidsId = generateUUID();
let promise = createPromise(reqBidsConfigObj, greenbidsId);
promise.then(callback);
}
function createPromise(reqBidsConfigObj, greenbidsId) {
return new Promise((resolve) => {
const timeoutId = setTimeout(() => {
logWarn('GreenbidsRtdProvider: Greenbids API timeout, skipping shaping');
resolve(reqBidsConfigObj);
}, rtdOptions.timeout);
ajax(
ENDPOINT,
{
success: (response) => {
processSuccessResponse(response, timeoutId, reqBidsConfigObj, greenbidsId);
resolve(reqBidsConfigObj);
},
error: () => {
clearTimeout(timeoutId);
logWarn('GreenbidsRtdProvider: Greenbids API response error, skipping shaping');
resolve(reqBidsConfigObj);
},
},
createPayload(reqBidsConfigObj, greenbidsId),
{
contentType: 'application/json',
customHeaders: {
'Greenbids-Pbuid': rtdOptions.pbuid
}
}
);
});
}
function processSuccessResponse(response, timeoutId, reqBidsConfigObj, greenbidsId) {
clearTimeout(timeoutId);
try {
const responseAdUnits = JSON.parse(response);
updateAdUnitsBasedOnResponse(reqBidsConfigObj.adUnits, responseAdUnits, greenbidsId);
} catch (e) {
logWarn('GreenbidsRtdProvider: Greenbids API response parsing error, skipping shaping');
}
}
function updateAdUnitsBasedOnResponse(adUnits, responseAdUnits, greenbidsId) {
const isFilteringForced = getParameterByName('greenbids_force_filtering');
const isFilteringDisabled = getParameterByName('greenbids_disable_filtering');
adUnits.forEach((adUnit) => {
const matchingAdUnit = findMatchingAdUnit(responseAdUnits, adUnit.code);
if (matchingAdUnit) {
deepSetValue(adUnit, 'ortb2Imp.ext.greenbids', {
greenbidsId: greenbidsId,
keptInAuction: matchingAdUnit.bidders,
isExploration: matchingAdUnit.isExploration
});
if (matchingAdUnit.isExploration || isFilteringDisabled) {
logMessage('Greenbids Rtd: either exploration traffic, or disabled filtering flag detected');
} else if (isFilteringForced) {
adUnit.bids = [];
logInfo('Greenbids Rtd: filtering flag detected, forcing filtering of Rtd module.');
} else {
removeFalseBidders(adUnit, matchingAdUnit);
}
}
});
}
function findMatchingAdUnit(responseAdUnits, adUnitCode) {
return responseAdUnits.find((responseAdUnit) => responseAdUnit.code === adUnitCode);
}
function removeFalseBidders(adUnit, matchingAdUnit) {
const falseBidders = getFalseBidders(matchingAdUnit.bidders);
adUnit.bids = adUnit.bids.filter((bidRequest) => !falseBidders.includes(bidRequest.bidder));
}
function getFalseBidders(bidders) {
return Object.entries(bidders)
.filter(([bidder, shouldKeep]) => !shouldKeep)
.map(([bidder]) => bidder);
}
function stripAdUnits(adUnits) {
const stripedAdUnits = deepClone(adUnits);
return stripedAdUnits.map(adUnit => {
adUnit.bids = adUnit.bids.map(bid => {
return { bidder: bid.bidder };
});
return adUnit;
});
}
function createPayload(reqBidsConfigObj, greenbidsId) {
return JSON.stringify({
version: MODULE_VERSION,
...rtdOptions,
referrer: window.location.href,
prebid: '$prebid.version$',
greenbidsId: greenbidsId,
adUnits: stripAdUnits(reqBidsConfigObj.adUnits),
});
}
export const greenbidsSubmodule = {
name: MODULE_NAME,
init: init,
onAuctionInitEvent: onAuctionInitEvent,
getBidRequestData: getBidRequestData,
updateAdUnitsBasedOnResponse: updateAdUnitsBasedOnResponse,
findMatchingAdUnit: findMatchingAdUnit,
removeFalseBidders: removeFalseBidders,
getFalseBidders: getFalseBidders,
stripAdUnits: stripAdUnits,
};
submodule('realTimeData', greenbidsSubmodule);