forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriteoBidAdapter.js
194 lines (168 loc) · 6.17 KB
/
criteoBidAdapter.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var bidfactory = require('src/bidfactory.js');
var bidmanager = require('src/bidmanager.js');
var adloader = require('src/adloader');
var adaptermanager = require('src/adaptermanager');
var utils = require('src/utils');
var CriteoAdapter = function CriteoAdapter() {
var sProt = (window.location.protocol === 'http:') ? 'http:' : 'https:';
var _publisherTagUrl = sProt + '//static.criteo.net/js/ld/publishertag.js';
var _bidderCode = 'criteo';
var _profileId = 125;
var _adapterVersion = 1;
function _callBids(params) {
if (!window.criteo_pubtag || window.criteo_pubtag instanceof Array) {
// publisherTag not loaded yet
_pushBidRequestEvent(params);
adloader.loadScript(
_publisherTagUrl,
function () { },
true
);
} else {
// publisherTag already loaded
_pushBidRequestEvent(params);
}
}
// send bid request to criteo direct bidder handler
function _pushBidRequestEvent(params) {
// if we want to be fully asynchronous, we must first check window.criteo_pubtag in case publishertag.js is not loaded yet.
window.Criteo = window.Criteo || {};
window.Criteo.events = window.Criteo.events || [];
// generate the bidding event
var biddingEventFunc = function () {
var bids = params.bids || [];
var slots = [];
var isAudit = false;
var networkid;
var integrationMode;
// build slots before sending one multi-slots bid request
for (var i = 0; i < bids.length; i++) {
var bid = bids[i];
var sizes = utils.parseSizesInput(bid.sizes);
slots.push(
new Criteo.PubTag.DirectBidding.DirectBiddingSlot(
bid.placementCode,
bid.params.zoneId,
bid.params.nativeCallback ? bid.params.nativeCallback : undefined,
bid.transactionId,
sizes.map((sizeString) => {
var xIndex = sizeString.indexOf('x');
var w = parseInt(sizeString.substring(0, xIndex));
var h = parseInt(sizeString.substring(xIndex + 1, sizeString.length))
return new Criteo.PubTag.DirectBidding.Size(w, h);
}
)
)
);
networkid = bid.params.networkId || networkid;
if (bid.params.integrationMode !== undefined) {
integrationMode = bid.params.integrationMode.toLowerCase() == 'amp' ? 1 : 0;
}
isAudit |= bid.params.audit !== undefined;
}
var biddingEvent = new Criteo.PubTag.DirectBidding.DirectBiddingEvent(
_profileId,
new Criteo.PubTag.DirectBidding.DirectBiddingUrlBuilder(isAudit),
slots,
_callbackSuccess(slots),
_callbackError(slots),
_callbackError(slots), // timeout handled as error
undefined,
networkid,
integrationMode,
_adapterVersion
);
// process the event as soon as possible
window.criteo_pubtag.push(biddingEvent);
};
window.Criteo.events.push(biddingEventFunc);
}
function parseBidResponse(bidsResponse) {
try {
return JSON.parse(bidsResponse);
} catch (error) {
return {};
}
}
function isNoBidResponse(jsonbidsResponse) {
return jsonbidsResponse.slots === undefined;
}
function _callbackSuccess(slots) {
return function (bidsResponse) {
var jsonbidsResponse = parseBidResponse(bidsResponse);
if (isNoBidResponse(jsonbidsResponse)) { return _callbackError(slots)(); }
for (var i = 0; i < slots.length; i++) {
var bidResponse = null;
// look for the matching bid response
for (var j = 0; j < jsonbidsResponse.slots.length; j++) {
if (jsonbidsResponse.slots[j] && jsonbidsResponse.slots[j].impid === slots[i].impId) {
bidResponse = jsonbidsResponse.slots.splice(j, 1)[0];
break;
}
}
// register the bid response
let bidObject = _buildBidObject(bidResponse, slots[i]);
bidmanager.addBidResponse(slots[i].impId, bidObject);
}
};
}
function _callbackError(slots) {
return function () {
for (var i = 0; i < slots.length; i++) {
bidmanager.addBidResponse(slots[i].impId, _invalidBidResponse());
}
};
}
function _invalidBidResponse() {
var bidObject = bidfactory.createBid(2);
bidObject.bidderCode = _bidderCode;
return bidObject;
}
function _buildBidObject(bidResponse, slot) {
let bidObject;
if (bidResponse) {
// map the common fields
bidObject = bidfactory.createBid(1);
bidObject.bidderCode = _bidderCode;
bidObject.cpm = bidResponse.cpm;
// in case of native
if (slot.nativeCallback && bidResponse.native) {
if (typeof slot.nativeCallback !== 'function') {
utils.logError('Criteo bid: nativeCallback parameter is not a function');
} else {
// store the callbacks in a global object
window.criteo_pubtag.native_slots = window.criteo_pubtag.native_slots || {};
window.criteo_pubtag.native_slots['' + bidObject.adId] = { callback: slot.nativeCallback, nativeResponse: bidResponse.native };
// this code is executed in an iframe, we need to get a reference to the
// publishertag in the main window to retrieve native responses and callbacks.
// it doesn't work with safeframes
bidObject.ad = `<script type=\"text/javascript\">
let win = window;
for (const i=0; i<10; ++i) {
win = win.parent;
if (win.criteo_pubtag && win.criteo_pubtag.native_slots) {
let responseSlot = win.criteo_pubtag.native_slots["${bidObject.adId}"];
responseSlot.callback(responseSlot.nativeResponse);
break;
}
}
</script>`;
}
} else {
// width and height are only relevant with non-native requests.
// native requests will always return a 2x2 zone size.
bidObject.width = bidResponse.width;
bidObject.height = bidResponse.height;
bidObject.ad = bidResponse.creative;
}
} else {
bidObject = _invalidBidResponse();
}
return bidObject;
}
return {
callBids: _callBids
};
};
adaptermanager.registerBidAdapter(new CriteoAdapter(), 'criteo');
module.exports = CriteoAdapter;