forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadtargetBidAdapter.js
142 lines (119 loc) · 3.96 KB
/
adtargetBidAdapter.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
import {_map, deepAccess, flatten, isArray, logError, parseSizesInput} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {config} from '../src/config.js';
import {find} from '../src/polyfill.js';
import {chunk} from '../libraries/chunk/chunk.js';
import {
createTag, getUserSyncsFn,
isBidRequestValid,
supportedMediaTypes
} from '../libraries/adtelligentUtils/adtelligentUtils.js';
const ENDPOINT = 'https://ghb.console.adtarget.com.tr/v2/auction/';
const BIDDER_CODE = 'adtarget';
const DISPLAY = 'display';
const syncsCache = {};
export const spec = {
code: BIDDER_CODE,
gvlid: 779,
supportedMediaTypes,
isBidRequestValid,
getUserSyncs: function (syncOptions, serverResponses) {
return getUserSyncsFn(syncOptions, serverResponses, syncsCache)
},
buildRequests: function (bidRequests, adapterRequest) {
const adapterSettings = config.getConfig(adapterRequest.bidderCode)
const chunkSize = deepAccess(adapterSettings, 'chunkSize', 10);
const { tag, bids } = bidToTag(bidRequests, adapterRequest);
const bidChunks = chunk(bids, chunkSize);
return _map(bidChunks, (bids) => {
return {
data: Object.assign({}, tag, { BidRequests: bids }),
adapterRequest,
method: 'POST',
url: ENDPOINT
};
})
},
interpretResponse: function (serverResponse, { adapterRequest }) {
serverResponse = serverResponse.body;
let bids = [];
if (!isArray(serverResponse)) {
return parseResponse(serverResponse, adapterRequest);
}
serverResponse.forEach(serverBidResponse => {
bids = flatten(bids, parseResponse(serverBidResponse, adapterRequest));
});
return bids;
}
};
function parseResponse(serverResponse, adapterRequest) {
const isInvalidValidResp = !serverResponse || !isArray(serverResponse.bids);
const bids = [];
if (isInvalidValidResp) {
const extMessage = serverResponse && serverResponse.ext && serverResponse.ext.message ? `: ${serverResponse.ext.message}` : '';
const errorMessage = `in response for ${adapterRequest.bidderCode} adapter ${extMessage}`;
logError(errorMessage);
return bids;
}
serverResponse.bids.forEach(serverBid => {
const request = find(adapterRequest.bids, (bidRequest) => {
return bidRequest.bidId === serverBid.requestId;
});
if (serverBid.cpm !== 0 && request !== undefined) {
const bid = createBid(serverBid, request);
bids.push(bid);
}
});
return bids;
}
function bidToTag(bidRequests, adapterRequest) {
const tag = createTag(bidRequests, adapterRequest);
const bids = [];
for (let i = 0, length = bidRequests.length; i < length; i++) {
const bid = prepareBidRequests(bidRequests[i]);
bids.push(bid);
}
return { tag, bids };
}
function prepareBidRequests(bidReq) {
const mediaType = deepAccess(bidReq, 'mediaTypes.video') ? VIDEO : DISPLAY;
const sizes = mediaType === VIDEO ? deepAccess(bidReq, 'mediaTypes.video.playerSize') : deepAccess(bidReq, 'mediaTypes.banner.sizes');
const bidReqParams = {
'CallbackId': bidReq.bidId,
'Aid': bidReq.params.aid,
'AdType': mediaType,
'Sizes': parseSizesInput(sizes).join(',')
};
return bidReqParams;
}
function getMediaType(bidderRequest) {
return deepAccess(bidderRequest, 'mediaTypes.video') ? VIDEO : BANNER;
}
function createBid(bidResponse, bidRequest) {
const mediaType = getMediaType(bidRequest)
const bid = {
requestId: bidResponse.requestId,
creativeId: bidResponse.cmpId,
height: bidResponse.height,
currency: bidResponse.cur,
width: bidResponse.width,
cpm: bidResponse.cpm,
netRevenue: true,
mediaType,
ttl: 300,
meta: {
advertiserDomains: bidResponse.adomain || []
}
};
if (mediaType === BANNER) {
return Object.assign(bid, {
ad: bidResponse.ad
});
}
Object.assign(bid, {
vastUrl: bidResponse.vastUrl
});
return bid;
}
registerBidder(spec);