-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
craftBidAdapter.js
189 lines (175 loc) · 5.26 KB
/
craftBidAdapter.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
import {getBidRequest} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import {find, includes} from '../src/polyfill.js';
import {getStorageManager} from '../src/storageManager.js';
import {ajax} from '../src/ajax.js';
import {hasPurpose1Consent} from '../src/utils/gdpr.js';
import {convertOrtbRequestToProprietaryNative} from '../src/native.js';
import {getANKeywordParam} from '../libraries/appnexusUtils/anKeywords.js';
import {interpretResponseUtil} from '../libraries/interpretResponseUtils/index.js';
const BIDDER_CODE = 'craft';
const URL_BASE = 'https://gacraft.jp/prebid-v3';
const TTL = 360;
const storage = getStorageManager({bidderCode: BIDDER_CODE});
export const spec = {
code: BIDDER_CODE,
aliases: ['craft'],
supportedMediaTypes: [BANNER],
isBidRequestValid: function(bid) {
return !!bid.params.sitekey && !!bid.params.placementId && !isAmp();
},
buildRequests: function(bidRequests, bidderRequest) {
// convert Native ORTB definition to old-style prebid native definition
bidRequests = convertOrtbRequestToProprietaryNative(bidRequests);
const bidRequest = bidRequests[0];
const tags = bidRequests.map(bidToTag);
const schain = bidRequest.schain;
const payload = {
tags: [...tags],
ua: navigator.userAgent,
sdk: {
version: '$prebid.version$'
},
schain: schain
};
if (bidderRequest) {
if (bidderRequest.gdprConsent) {
payload.gdpr_consent = {
consent_string: bidderRequest.gdprConsent.consentString,
consent_required: bidderRequest.gdprConsent.gdprApplies
};
}
if (bidderRequest.uspConsent) {
payload.us_privacy = bidderRequest.uspConsent;
}
if (bidderRequest.refererInfo) {
let refererinfo = {
// TODO: this collects everything it finds, except for the canonical URL
rd_ref: bidderRequest.refererInfo.topmostLocation,
rd_top: bidderRequest.refererInfo.reachedTop,
rd_ifs: bidderRequest.refererInfo.numIframes,
};
if (bidderRequest.refererInfo.stack) {
refererinfo.rd_stk = bidderRequest.refererInfo.stack.join(',');
}
payload.referrer_detection = refererinfo;
}
if (bidRequest.userId) {
payload.userId = bidRequest.userId
}
}
const request = formatRequest(payload, bidderRequest);
return request;
},
interpretResponse: function(serverResponse, {bidderRequest}) {
try {
const bids = interpretResponseUtil(serverResponse, {bidderRequest}, serverBid => {
const rtbBid = getRtbBid(serverBid);
if (rtbBid && rtbBid.cpm !== 0 && includes(this.supportedMediaTypes, rtbBid.ad_type)) {
const bid = newBid(serverBid, rtbBid, bidderRequest);
bid.mediaType = parseMediaType(rtbBid);
return bid;
}
});
return bids;
} catch (e) {
return [];
}
},
onBidWon: function(bid) {
ajax(bid._prebidWon, null, null, {
method: 'POST',
contentType: 'application/json'
});
}
};
function formatRequest(payload, bidderRequest) {
let options = {};
if (!hasPurpose1Consent(bidderRequest?.gdprConsent)) {
options = {
withCredentials: false
};
}
const baseUrl = payload.tags[0].url || URL_BASE;
const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: `${baseUrl}/${payload.tags[0].sitekey}`,
data: payloadString,
bidderRequest,
options
};
}
function newBid(serverBid, rtbBid, bidderRequest) {
const bidRequest = getBidRequest(serverBid.uuid, [bidderRequest]);
const bid = {
requestId: serverBid.uuid,
cpm: rtbBid.cpm,
currency: 'JPY',
width: rtbBid.rtb.banner.width,
height: rtbBid.rtb.banner.height,
ad: rtbBid.rtb.banner.content,
ttl: TTL,
creativeId: rtbBid.creative_id,
netRevenue: false, // ???
dealId: rtbBid.deal_id,
meta: null,
_adUnitCode: bidRequest.adUnitCode,
_bidKey: serverBid.bid_key,
_prebidWon: serverBid.won_url,
};
return bid;
}
function bidToTag(bid) {
const tag = {};
for (var k in bid.params) {
tag[k] = bid.params[k];
}
try {
if (storage.hasLocalStorage()) {
tag.uid = JSON.parse(storage.getDataFromLocalStorage(`${bid.params.sitekey}_uid`));
}
} catch (e) {
}
tag.sizes = bid.sizes;
tag.primary_size = tag.sizes[0];
tag.ad_types = [];
tag.uuid = bid.bidId;
const keywords = getANKeywordParam(bid.ortb2, bid.params.keywords);
if (keywords.length) {
tag.keywords = keywords;
}
if (bid.mediaTypes?.banner) {
tag.ad_types.push(BANNER);
}
if (tag.ad_types.length === 0) {
delete tag.ad_types;
}
return tag;
}
function getRtbBid(tag) {
return tag && tag.ads && tag.ads.length && find(tag.ads, ad => ad.rtb);
}
function parseMediaType(rtbBid) {
const adType = rtbBid.ad_type;
if (adType === VIDEO) {
return VIDEO;
} else if (adType === NATIVE) {
return NATIVE;
} else {
return BANNER;
}
}
function isAmp() {
try {
const ampContext = window.context || window.parent.context;
if (ampContext && ampContext.pageViewId) {
return ampContext;
}
return false;
} catch (e) {
return false;
}
}
registerBidder(spec);