forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafpBidAdapter.js
167 lines (153 loc) · 5.16 KB
/
afpBidAdapter.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
import {includes} from '../src/polyfill.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {Renderer} from '../src/Renderer.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
export const IS_DEV = location.hostname === 'localhost'
export const BIDDER_CODE = 'afp'
export const SSP_ENDPOINT = 'https://ssp.afp.ai/api/prebid'
export const REQUEST_METHOD = 'POST'
// TODO: test code should be kept in tests
export const TEST_PAGE_URL = 'https://rtbinsight.ru/smiert-bolshikh-dannykh-kto-na-novienkogo/'
const SDK_PATH = 'https://cdn.afp.ai/ssp/sdk.js?auto_initialization=false&deploy_to_parent_window=true'
const TTL = 60
export const IN_IMAGE_BANNER_TYPE = 'In-image'
export const IN_IMAGE_MAX_BANNER_TYPE = 'In-image Max'
export const IN_CONTENT_BANNER_TYPE = 'In-content Banner'
export const IN_CONTENT_VIDEO_TYPE = 'In-content Video'
export const OUT_CONTENT_VIDEO_TYPE = 'Out-content Video'
export const IN_CONTENT_STORY_TYPE = 'In-content Stories'
export const ACTION_SCROLLER_TYPE = 'Action Scroller'
export const ACTION_SCROLLER_LIGHT_TYPE = 'Action Scroller Light'
export const JUST_BANNER_TYPE = 'Just Banner'
export const mediaTypeByPlaceType = {
[IN_IMAGE_BANNER_TYPE]: BANNER,
[IN_IMAGE_MAX_BANNER_TYPE]: BANNER,
[IN_CONTENT_BANNER_TYPE]: BANNER,
[IN_CONTENT_STORY_TYPE]: BANNER,
[ACTION_SCROLLER_TYPE]: BANNER,
[ACTION_SCROLLER_LIGHT_TYPE]: BANNER,
[JUST_BANNER_TYPE]: BANNER,
[IN_CONTENT_VIDEO_TYPE]: VIDEO,
[OUT_CONTENT_VIDEO_TYPE]: VIDEO,
}
const wrapAd = (dataToCreatePlace) => {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>html, body {width: 100%; height: 100%; margin: 0;}</style>
<script src="${SDK_PATH}"></script>
</head>
<body>
<script>
window.afp.createPlaceByData(JSON.parse(decodeURIComponent("${encodeURIComponent(JSON.stringify(dataToCreatePlace))}")))
</script>
</body>
</html>`
}
const bidRequestMap = {}
const createRenderer = (bid, dataToCreatePlace) => {
const renderer = new Renderer({
targetId: bid.adUnitCode,
url: SDK_PATH,
callback() {
renderer.loaded = true
window.afp.createPlaceByData(dataToCreatePlace)
}
})
return renderer
}
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid({mediaTypes, params}) {
if (typeof params !== 'object' || typeof mediaTypes !== 'object') {
return false
}
const {placeId, placeType, imageUrl, imageWidth, imageHeight} = params
const media = mediaTypes[mediaTypeByPlaceType[placeType]]
if (placeId && media) {
if (mediaTypeByPlaceType[placeType] === VIDEO) {
if (!media.playerSize) {
return false
}
} else if (mediaTypeByPlaceType[placeType] === BANNER) {
if (!media.sizes) {
return false
}
}
if (includes([IN_IMAGE_BANNER_TYPE, IN_IMAGE_MAX_BANNER_TYPE], placeType)) {
if (imageUrl && imageWidth && imageHeight) {
return true
}
} else {
return true
}
}
return false
},
buildRequests(validBidRequests, {refererInfo, gdprConsent}) {
const payload = {
pageUrl: IS_DEV ? TEST_PAGE_URL : refererInfo.page,
gdprConsent: gdprConsent,
bidRequests: validBidRequests.map(validBidRequest => {
const {bidId, ortb2Imp, sizes, params: {
placeId, placeType, imageUrl, imageWidth, imageHeight
}} = validBidRequest
bidRequestMap[bidId] = validBidRequest
const bidRequest = {
bidId,
transactionId: ortb2Imp?.ext?.tid,
sizes,
placeId,
}
if (includes([IN_IMAGE_BANNER_TYPE, IN_IMAGE_MAX_BANNER_TYPE], placeType)) {
Object.assign(bidRequest, {
imageUrl,
imageWidth: Math.floor(imageWidth),
imageHeight: Math.floor(imageHeight),
})
}
return bidRequest
})
}
return {
method: REQUEST_METHOD,
url: SSP_ENDPOINT,
data: payload,
options: {
contentType: 'application/json'
}
}
},
interpretResponse(serverResponse) {
let bids = serverResponse.body && serverResponse.body.bids
bids = Array.isArray(bids) ? bids : []
return bids.map(({bidId, cpm, width, height, creativeId, currency, netRevenue, adSettings, placeSettings}, index) => {
const bid = {
requestId: bidId,
cpm,
width,
height,
creativeId,
currency,
netRevenue,
meta: {
mediaType: mediaTypeByPlaceType[placeSettings.placeType],
},
ttl: TTL
}
const bidRequest = bidRequestMap[bidId]
const placeContainer = bidRequest.params.placeContainer
const dataToCreatePlace = { adSettings, placeSettings, placeContainer, isPrebid: true }
if (mediaTypeByPlaceType[placeSettings.placeType] === BANNER) {
bid.ad = wrapAd(dataToCreatePlace)
} else if (mediaTypeByPlaceType[placeSettings.placeType] === VIDEO) {
bid.vastXml = adSettings.content
bid.renderer = createRenderer(bid, dataToCreatePlace)
}
return bid
})
}
}
registerBidder(spec);