-
Notifications
You must be signed in to change notification settings - Fork 758
/
pubmatic.go
651 lines (548 loc) · 17.8 KB
/
pubmatic.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package pubmatic
import (
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"strconv"
"strings"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/prebid/prebid-server/v2/util/ptrutil"
"github.com/buger/jsonparser"
"github.com/prebid/openrtb/v20/openrtb2"
)
const MAX_IMPRESSIONS_PUBMATIC = 30
const ae = "ae"
type PubmaticAdapter struct {
URI string
bidderName string
}
type pubmaticBidExt struct {
BidType *int `json:"BidType,omitempty"`
VideoCreativeInfo *pubmaticBidExtVideo `json:"video,omitempty"`
Marketplace string `json:"marketplace,omitempty"`
PrebidDealPriority int `json:"prebiddealpriority,omitempty"`
}
type pubmaticWrapperExt struct {
ProfileID int `json:"profile,omitempty"`
VersionID int `json:"version,omitempty"`
}
type pubmaticBidExtVideo struct {
Duration *int `json:"duration,omitempty"`
}
type ExtImpBidderPubmatic struct {
adapters.ExtImpBidder
Data json.RawMessage `json:"data,omitempty"`
AE int `json:"ae,omitempty"`
}
type ExtAdServer struct {
Name string `json:"name"`
AdSlot string `json:"adslot"`
}
type marketplaceReqExt struct {
AllowedBidders []string `json:"allowedbidders,omitempty"`
}
type extRequestAdServer struct {
Wrapper *pubmaticWrapperExt `json:"wrapper,omitempty"`
Acat []string `json:"acat,omitempty"`
Marketplace *marketplaceReqExt `json:"marketplace,omitempty"`
openrtb_ext.ExtRequest
}
type respExt struct {
FledgeAuctionConfigs map[string]json.RawMessage `json:"fledge_auction_configs,omitempty"`
}
const (
dctrKeyName = "key_val"
pmZoneIDKeyName = "pmZoneId"
pmZoneIDKeyNameOld = "pmZoneID"
ImpExtAdUnitKey = "dfp_ad_unit_code"
AdServerGAM = "gam"
AdServerKey = "adserver"
PBAdslotKey = "pbadslot"
)
func (a *PubmaticAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
errs := make([]error, 0, len(request.Imp))
pubID := ""
extractWrapperExtFromImp := true
extractPubIDFromImp := true
newReqExt, err := extractPubmaticExtFromRequest(request)
if err != nil {
return nil, []error{err}
}
wrapperExt := newReqExt.Wrapper
if wrapperExt != nil && wrapperExt.ProfileID != 0 && wrapperExt.VersionID != 0 {
extractWrapperExtFromImp = false
}
for i := 0; i < len(request.Imp); i++ {
wrapperExtFromImp, pubIDFromImp, err := parseImpressionObject(&request.Imp[i], extractWrapperExtFromImp, extractPubIDFromImp)
// If the parsing is failed, remove imp and add the error.
if err != nil {
errs = append(errs, err)
request.Imp = append(request.Imp[:i], request.Imp[i+1:]...)
i--
continue
}
if extractWrapperExtFromImp {
if wrapperExtFromImp != nil {
if wrapperExt == nil {
wrapperExt = &pubmaticWrapperExt{}
}
if wrapperExt.ProfileID == 0 {
wrapperExt.ProfileID = wrapperExtFromImp.ProfileID
}
if wrapperExt.VersionID == 0 {
wrapperExt.VersionID = wrapperExtFromImp.VersionID
}
if wrapperExt != nil && wrapperExt.ProfileID != 0 && wrapperExt.VersionID != 0 {
extractWrapperExtFromImp = false
}
}
}
if extractPubIDFromImp && pubIDFromImp != "" {
pubID = pubIDFromImp
extractPubIDFromImp = false
}
}
// If all the requests are invalid, Call to adaptor is skipped
if len(request.Imp) == 0 {
return nil, errs
}
newReqExt.Wrapper = wrapperExt
rawExt, err := json.Marshal(newReqExt)
if err != nil {
return nil, []error{err}
}
request.Ext = rawExt
if request.Site != nil {
siteCopy := *request.Site
if siteCopy.Publisher != nil {
publisherCopy := *siteCopy.Publisher
publisherCopy.ID = pubID
siteCopy.Publisher = &publisherCopy
} else {
siteCopy.Publisher = &openrtb2.Publisher{ID: pubID}
}
request.Site = &siteCopy
} else if request.App != nil {
appCopy := *request.App
if appCopy.Publisher != nil {
publisherCopy := *appCopy.Publisher
publisherCopy.ID = pubID
appCopy.Publisher = &publisherCopy
} else {
appCopy.Publisher = &openrtb2.Publisher{ID: pubID}
}
request.App = &appCopy
}
reqJSON, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
return []*adapters.RequestData{{
Method: "POST",
Uri: a.URI,
Body: reqJSON,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}}, errs
}
// validateAdslot validate the optional adslot string
// valid formats are 'adslot@WxH', 'adslot' and no adslot
func validateAdSlot(adslot string, imp *openrtb2.Imp) error {
adSlotStr := strings.TrimSpace(adslot)
if len(adSlotStr) == 0 {
return nil
}
if !strings.Contains(adSlotStr, "@") {
imp.TagID = adSlotStr
return nil
}
adSlot := strings.Split(adSlotStr, "@")
if len(adSlot) == 2 && adSlot[0] != "" && adSlot[1] != "" {
imp.TagID = strings.TrimSpace(adSlot[0])
adSize := strings.Split(strings.ToLower(adSlot[1]), "x")
if len(adSize) != 2 {
return fmt.Errorf("Invalid size provided in adSlot %v", adSlotStr)
}
width, err := strconv.Atoi(strings.TrimSpace(adSize[0]))
if err != nil {
return fmt.Errorf("Invalid width provided in adSlot %v", adSlotStr)
}
heightStr := strings.Split(adSize[1], ":")
height, err := strconv.Atoi(strings.TrimSpace(heightStr[0]))
if err != nil {
return fmt.Errorf("Invalid height provided in adSlot %v", adSlotStr)
}
//In case of video, size could be derived from the player size
if imp.Banner != nil {
imp.Banner = assignBannerWidthAndHeight(imp.Banner, int64(width), int64(height))
}
} else {
return fmt.Errorf("Invalid adSlot %v", adSlotStr)
}
return nil
}
func assignBannerSize(banner *openrtb2.Banner) (*openrtb2.Banner, error) {
if banner.W != nil && banner.H != nil {
return banner, nil
}
return assignBannerWidthAndHeight(banner, banner.Format[0].W, banner.Format[0].H), nil
}
func assignBannerWidthAndHeight(banner *openrtb2.Banner, w, h int64) *openrtb2.Banner {
bannerCopy := *banner
bannerCopy.W = ptrutil.ToPtr(w)
bannerCopy.H = ptrutil.ToPtr(h)
return &bannerCopy
}
// parseImpressionObject parse the imp to get it ready to send to pubmatic
func parseImpressionObject(imp *openrtb2.Imp, extractWrapperExtFromImp, extractPubIDFromImp bool) (*pubmaticWrapperExt, string, error) {
var wrapExt *pubmaticWrapperExt
var pubID string
// PubMatic supports banner and video impressions.
if imp.Banner == nil && imp.Video == nil && imp.Native == nil {
return wrapExt, pubID, fmt.Errorf("invalid MediaType. PubMatic only supports Banner, Video and Native. Ignoring ImpID=%s", imp.ID)
}
if imp.Audio != nil {
imp.Audio = nil
}
var bidderExt ExtImpBidderPubmatic
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return wrapExt, pubID, err
}
var pubmaticExt openrtb_ext.ExtImpPubmatic
if err := json.Unmarshal(bidderExt.Bidder, &pubmaticExt); err != nil {
return wrapExt, pubID, err
}
if extractPubIDFromImp {
pubID = strings.TrimSpace(pubmaticExt.PublisherId)
}
// Parse Wrapper Extension only once per request
if extractWrapperExtFromImp && len(pubmaticExt.WrapExt) != 0 {
err := json.Unmarshal([]byte(pubmaticExt.WrapExt), &wrapExt)
if err != nil {
return wrapExt, pubID, fmt.Errorf("Error in Wrapper Parameters = %v for ImpID = %v WrapperExt = %v", err.Error(), imp.ID, string(pubmaticExt.WrapExt))
}
}
if err := validateAdSlot(strings.TrimSpace(pubmaticExt.AdSlot), imp); err != nil {
return wrapExt, pubID, err
}
if imp.Banner != nil {
bannerCopy, err := assignBannerSize(imp.Banner)
if err != nil {
return wrapExt, pubID, err
}
imp.Banner = bannerCopy
}
if pubmaticExt.Kadfloor != "" {
bidfloor, err := strconv.ParseFloat(strings.TrimSpace(pubmaticExt.Kadfloor), 64)
if err == nil {
// In case of valid kadfloor, select maximum of original imp.bidfloor and kadfloor
imp.BidFloor = math.Max(bidfloor, imp.BidFloor)
}
}
extMap := make(map[string]interface{}, 0)
if pubmaticExt.Keywords != nil && len(pubmaticExt.Keywords) != 0 {
addKeywordsToExt(pubmaticExt.Keywords, extMap)
}
//Give preference to direct values of 'dctr' & 'pmZoneId' params in extension
if pubmaticExt.Dctr != "" {
extMap[dctrKeyName] = pubmaticExt.Dctr
}
if pubmaticExt.PmZoneID != "" {
extMap[pmZoneIDKeyName] = pubmaticExt.PmZoneID
}
if len(bidderExt.Data) > 0 {
populateFirstPartyDataImpAttributes(bidderExt.Data, extMap)
}
if bidderExt.AE != 0 {
extMap[ae] = bidderExt.AE
}
imp.Ext = nil
if len(extMap) > 0 {
ext, err := json.Marshal(extMap)
if err == nil {
imp.Ext = ext
}
}
return wrapExt, pubID, nil
}
// extractPubmaticExtFromRequest parse the req.ext to fetch wrapper and acat params
func extractPubmaticExtFromRequest(request *openrtb2.BidRequest) (extRequestAdServer, error) {
// req.ext.prebid would always be there and Less nil cases to handle, more safe!
var pmReqExt extRequestAdServer
if request == nil || len(request.Ext) == 0 {
return pmReqExt, nil
}
reqExt := &openrtb_ext.ExtRequest{}
err := json.Unmarshal(request.Ext, &reqExt)
if err != nil {
return pmReqExt, fmt.Errorf("error decoding Request.ext : %s", err.Error())
}
pmReqExt.ExtRequest = *reqExt
reqExtBidderParams := make(map[string]json.RawMessage)
if reqExt.Prebid.BidderParams != nil {
err = json.Unmarshal(reqExt.Prebid.BidderParams, &reqExtBidderParams)
if err != nil {
return pmReqExt, err
}
}
//get request ext bidder params
if wrapperObj, present := reqExtBidderParams["wrapper"]; present && len(wrapperObj) != 0 {
wrpExt := &pubmaticWrapperExt{}
err = json.Unmarshal(wrapperObj, wrpExt)
if err != nil {
return pmReqExt, err
}
pmReqExt.Wrapper = wrpExt
}
if acatBytes, ok := reqExtBidderParams["acat"]; ok {
var acat []string
err = json.Unmarshal(acatBytes, &acat)
if err != nil {
return pmReqExt, err
}
for i := 0; i < len(acat); i++ {
acat[i] = strings.TrimSpace(acat[i])
}
pmReqExt.Acat = acat
}
if allowedBidders := getAlternateBidderCodesFromRequestExt(reqExt); allowedBidders != nil {
pmReqExt.Marketplace = &marketplaceReqExt{AllowedBidders: allowedBidders}
}
return pmReqExt, nil
}
func getAlternateBidderCodesFromRequestExt(reqExt *openrtb_ext.ExtRequest) []string {
if reqExt == nil || reqExt.Prebid.AlternateBidderCodes == nil {
return nil
}
allowedBidders := []string{"pubmatic"}
if reqExt.Prebid.AlternateBidderCodes.Enabled {
if pmABC, ok := reqExt.Prebid.AlternateBidderCodes.Bidders["pubmatic"]; ok && pmABC.Enabled {
if pmABC.AllowedBidderCodes == nil || (len(pmABC.AllowedBidderCodes) == 1 && pmABC.AllowedBidderCodes[0] == "*") {
return []string{"all"}
}
return append(allowedBidders, pmABC.AllowedBidderCodes...)
}
}
return allowedBidders
}
func addKeywordsToExt(keywords []*openrtb_ext.ExtImpPubmaticKeyVal, extMap map[string]interface{}) {
for _, keyVal := range keywords {
if len(keyVal.Values) == 0 {
continue
} else {
key := keyVal.Key
if keyVal.Key == pmZoneIDKeyNameOld {
key = pmZoneIDKeyName
}
extMap[key] = strings.Join(keyVal.Values[:], ",")
}
}
}
func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if response.StatusCode == http.StatusNoContent {
return nil, nil
}
if response.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}
if response.StatusCode != http.StatusOK {
return nil, []error{fmt.Errorf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode)}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
var errs []error
for _, sb := range bidResp.SeatBid {
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]
if len(bid.Cat) > 1 {
bid.Cat = bid.Cat[0:1]
}
typedBid := &adapters.TypedBid{
Bid: &bid,
BidType: openrtb_ext.BidTypeBanner,
BidVideo: &openrtb_ext.ExtBidPrebidVideo{},
}
var bidExt *pubmaticBidExt
err := json.Unmarshal(bid.Ext, &bidExt)
if err != nil {
errs = append(errs, err)
} else if bidExt != nil {
typedBid.Seat = openrtb_ext.BidderName(bidExt.Marketplace)
typedBid.BidType = getBidType(bidExt)
if bidExt.PrebidDealPriority > 0 {
typedBid.DealPriority = bidExt.PrebidDealPriority
}
if bidExt.VideoCreativeInfo != nil && bidExt.VideoCreativeInfo.Duration != nil {
typedBid.BidVideo.Duration = *bidExt.VideoCreativeInfo.Duration
}
}
if typedBid.BidType == openrtb_ext.BidTypeNative {
bid.AdM, err = getNativeAdm(bid.AdM)
if err != nil {
errs = append(errs, err)
}
}
bidResponse.Bids = append(bidResponse.Bids, typedBid)
}
}
if bidResp.Cur != "" {
bidResponse.Currency = bidResp.Cur
}
if bidResp.Ext != nil {
var bidRespExt respExt
if err := json.Unmarshal(bidResp.Ext, &bidRespExt); err == nil && bidRespExt.FledgeAuctionConfigs != nil {
bidResponse.FledgeAuctionConfigs = make([]*openrtb_ext.FledgeAuctionConfig, 0, len(bidRespExt.FledgeAuctionConfigs))
for impId, config := range bidRespExt.FledgeAuctionConfigs {
fledgeAuctionConfig := &openrtb_ext.FledgeAuctionConfig{
ImpId: impId,
Config: config,
}
bidResponse.FledgeAuctionConfigs = append(bidResponse.FledgeAuctionConfigs, fledgeAuctionConfig)
}
}
}
return bidResponse, errs
}
func getNativeAdm(adm string) (string, error) {
var err error
nativeAdm := make(map[string]interface{})
err = json.Unmarshal([]byte(adm), &nativeAdm)
if err != nil {
return adm, errors.New("unable to unmarshal native adm")
}
// move bid.adm.native to bid.adm
if _, ok := nativeAdm["native"]; ok {
//using jsonparser to avoid marshaling, encode escape, etc.
value, _, _, err := jsonparser.Get([]byte(adm), string(openrtb_ext.BidTypeNative))
if err != nil {
return adm, errors.New("unable to get native adm")
}
adm = string(value)
}
return adm, nil
}
// getMapFromJSON converts JSON to map
func getMapFromJSON(source json.RawMessage) map[string]interface{} {
if source != nil {
dataMap := make(map[string]interface{})
err := json.Unmarshal(source, &dataMap)
if err == nil {
return dataMap
}
}
return nil
}
// populateFirstPartyDataImpAttributes will parse imp.ext.data and populate imp extMap
func populateFirstPartyDataImpAttributes(data json.RawMessage, extMap map[string]interface{}) {
dataMap := getMapFromJSON(data)
if dataMap == nil {
return
}
populateAdUnitKey(data, dataMap, extMap)
populateDctrKey(dataMap, extMap)
}
// populateAdUnitKey parses data object to read and populate DFP adunit key
func populateAdUnitKey(data json.RawMessage, dataMap, extMap map[string]interface{}) {
if name, err := jsonparser.GetString(data, "adserver", "name"); err == nil && name == AdServerGAM {
if adslot, err := jsonparser.GetString(data, "adserver", "adslot"); err == nil && adslot != "" {
extMap[ImpExtAdUnitKey] = adslot
}
}
//imp.ext.dfp_ad_unit_code is not set, then check pbadslot in imp.ext.data
if extMap[ImpExtAdUnitKey] == nil && dataMap[PBAdslotKey] != nil {
extMap[ImpExtAdUnitKey] = dataMap[PBAdslotKey].(string)
}
}
// populateDctrKey reads key-val pairs from imp.ext.data and add it in imp.ext.key_val
func populateDctrKey(dataMap, extMap map[string]interface{}) {
var dctr strings.Builder
//append dctr key if already present in extMap
if extMap[dctrKeyName] != nil {
dctr.WriteString(extMap[dctrKeyName].(string))
}
for key, val := range dataMap {
//ignore 'pbaslot' and 'adserver' key as they are not targeting keys
if key == PBAdslotKey || key == AdServerKey {
continue
}
//separate key-val pairs in dctr string by pipe(|)
if dctr.Len() > 0 {
dctr.WriteString("|")
}
//trimming spaces from key
key = strings.TrimSpace(key)
switch typedValue := val.(type) {
case string:
if _, err := fmt.Fprintf(&dctr, "%s=%s", key, strings.TrimSpace(typedValue)); err != nil {
continue
}
case float64, bool:
if _, err := fmt.Fprintf(&dctr, "%s=%v", key, typedValue); err != nil {
continue
}
case []interface{}:
if valStrArr := getStringArray(typedValue); len(valStrArr) > 0 {
valStr := strings.Join(valStrArr[:], ",")
if _, err := fmt.Fprintf(&dctr, "%s=%s", key, valStr); err != nil {
continue
}
}
}
}
if dctrStr := dctr.String(); dctrStr != "" {
extMap[dctrKeyName] = strings.TrimSuffix(dctrStr, "|")
}
}
// getStringArray converts interface of type string array to string array
func getStringArray(array []interface{}) []string {
aString := make([]string, len(array))
for i, v := range array {
if str, ok := v.(string); ok {
aString[i] = strings.TrimSpace(str)
} else {
return nil
}
}
return aString
}
// getBidType returns the bid type specified in the response bid.ext
func getBidType(bidExt *pubmaticBidExt) openrtb_ext.BidType {
// setting "banner" as the default bid type
bidType := openrtb_ext.BidTypeBanner
if bidExt != nil && bidExt.BidType != nil {
switch *bidExt.BidType {
case 0:
bidType = openrtb_ext.BidTypeBanner
case 1:
bidType = openrtb_ext.BidTypeVideo
case 2:
bidType = openrtb_ext.BidTypeNative
default:
// default value is banner
bidType = openrtb_ext.BidTypeBanner
}
}
return bidType
}
// Builder builds a new instance of the Pubmatic adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
bidder := &PubmaticAdapter{
URI: config.Endpoint,
bidderName: string(bidderName),
}
return bidder, nil
}