forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgammaBidAdapter.js
144 lines (127 loc) · 3.93 KB
/
gammaBidAdapter.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
import { registerBidder } from '../src/adapters/bidderFactory.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
*/
const BIDDER_CODE = 'gamma';
const ENDPOINTS = {
SGP: 'https://hb.gammaplatform.com',
JPN: 'https://hb-jp.gammaplatform.com',
US_WEST: 'https://hb-us.gammaplatform.com',
EU: 'https://hb-eu.gammaplatform.com'
}
export const spec = {
code: BIDDER_CODE,
aliases: ['gamma'],
supportedMediaTypes: ['banner', 'video'],
/**
* Determines whether or not the given bid request is valid.
*
* @param {object} bid The bid to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function(bid) {
return !!(bid.params.siteId || bid.params.zoneId);
},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} bidRequests A non-empty list of bid requests which should be sent to the Server.
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(bidRequests, bidderRequest) {
const serverRequests = [];
const bidderRequestReferer = bidderRequest?.refererInfo?.page || '';
let ENDPOINT;
for (var i = 0, len = bidRequests.length; i < len; i++) {
const gaxObjParams = bidRequests[i];
ENDPOINT = getAdUrlByRegion(gaxObjParams);
serverRequests.push({
method: 'GET',
url: ENDPOINT + '/adx/request?wid=' + gaxObjParams.params.siteId + '&zid=' + gaxObjParams.params.zoneId + '&hb=pbjs&bidid=' + gaxObjParams.bidId + '&urf=' + encodeURIComponent(bidderRequestReferer)
});
}
return serverRequests;
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse) {
serverResponse = serverResponse.body;
const bids = [];
if (serverResponse.id) {
const bid = newBid(serverResponse);
bids.push(bid);
}
return bids;
}
}
/**
* Get endpoint url by region
* @param bid
* @return aUrl
*/
function getAdUrlByRegion(bid) {
let ENDPOINT;
if (bid.params.region && ENDPOINTS[bid.params.region]) {
ENDPOINT = ENDPOINTS[bid.params.region];
} else {
try {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const region = timezone.split('/')[0];
switch (region) {
case 'Europe':
ENDPOINT = ENDPOINTS['EU'];
break;
case 'Australia':
ENDPOINT = ENDPOINTS['JPN'];
break;
case 'Asia':
ENDPOINT = ENDPOINTS['SGP'];
break;
case 'America':
ENDPOINT = ENDPOINTS['US_WEST'];
break;
default: ENDPOINT = ENDPOINTS['SGP'];
}
} catch (err) {
ENDPOINT = ENDPOINTS['SGP'];
}
}
return ENDPOINT;
}
/**
* Unpack the Server's Bid into a Prebid-compatible one.
* @param serverBid
* @return Bid
*/
function newBid(serverBid) {
const bid = {
ad: serverBid.seatbid[0].bid[0].adm,
cpm: serverBid.seatbid[0].bid[0].price,
creativeId: serverBid.seatbid[0].bid[0].adid,
currency: serverBid.cur,
dealId: serverBid.seatbid[0].bid[0].dealid,
width: serverBid.seatbid[0].bid[0].w,
height: serverBid.seatbid[0].bid[0].h,
mediaType: serverBid.type,
netRevenue: true,
requestId: serverBid.id,
ttl: serverBid.seatbid[0].bid[0].ttl || 300,
meta: {
advertiserDomains: serverBid.seatbid[0].bid[0].adomain && serverBid.seatbid[0].bid[0].adomain.length ? serverBid.seatbid[0].bid[0].adomain : []
}
};
if (serverBid.type == 'video') {
Object.assign(bid, {
vastXml: serverBid.seatbid[0].bid[0].vastXml,
vastUrl: serverBid.seatbid[0].bid[0].vastUrl,
ttl: 3600
});
}
return bid;
}
registerBidder(spec);