Skip to content

Commit

Permalink
Pubmatic: Support for video duration and primary category (#1384)
Browse files Browse the repository at this point in the history
* Adding suport for video duration and primary category in pubmatic adapter

* Adding code review changes for PR-1384

* Adding changes for syntaxNode suggestion

Co-authored-by: Isha Bharti <isha.bharti@pubmatic.com>
  • Loading branch information
PubMatic-OpenWrap and pm-isha-bharti authored Jul 27, 2020
1 parent a5962de commit f1582a4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 43 deletions.
67 changes: 43 additions & 24 deletions adapters/pubmatic/pubmatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
)

const MAX_IMPRESSIONS_PUBMATIC = 30
const bidTypeExtKey = "BidType"

type PubmaticAdapter struct {
http *adapters.HTTPAdapter
Expand All @@ -48,6 +47,15 @@ type pubmaticParams struct {
Keywords map[string]string `json:"keywords,omitempty"`
}

type pubmaticBidExtVideo struct {
Duration *int `json:"duration,omitempty"`
}

type pubmaticBidExt struct {
BidType *int `json:"BidType,omitempty"`
VideoCreativeInfo *pubmaticBidExtVideo `json:"video,omitempty"`
}

const (
INVALID_PARAMS = "Invalid BidParam"
MISSING_PUBID = "Missing PubID"
Expand Down Expand Up @@ -289,7 +297,11 @@ func (a *PubmaticAdapter) Call(ctx context.Context, req *pbs.PBSRequest, bidder
DealId: bid.DealID,
}

mediaType := getBidType(bid.Ext)
var bidExt pubmaticBidExt
mediaType := openrtb_ext.BidTypeBanner
if err := json.Unmarshal(bid.Ext, &bidExt); err == nil {
mediaType = getBidType(&bidExt)
}
pbid.CreativeMediaType = string(mediaType)

bids = append(bids, &pbid)
Expand Down Expand Up @@ -549,9 +561,24 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb.BidRequest, external
for _, sb := range bidResp.SeatBid {
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]
impVideo := &openrtb_ext.ExtBidPrebidVideo{}

if len(bid.Cat) > 1 {
bid.Cat = bid.Cat[0:1]
}

var bidExt *pubmaticBidExt
bidType := openrtb_ext.BidTypeBanner
if err := json.Unmarshal(bid.Ext, &bidExt); err == nil && bidExt != nil {
if bidExt.VideoCreativeInfo != nil && bidExt.VideoCreativeInfo.Duration != nil {
impVideo.Duration = *bidExt.VideoCreativeInfo.Duration
}
bidType = getBidType(bidExt)
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getBidType(bid.Ext),
Bid: &bid,
BidType: bidType,
BidVideo: impVideo,
})

}
Expand All @@ -560,28 +587,20 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb.BidRequest, external
}

// getBidType returns the bid type specified in the response bid.ext
func getBidType(bidExt json.RawMessage) openrtb_ext.BidType {
func getBidType(bidExt *pubmaticBidExt) openrtb_ext.BidType {
// setting "banner" as the default bid type
bidType := openrtb_ext.BidTypeBanner
if bidExt != nil {
bidExtMap := make(map[string]interface{})
extbyte, err := json.Marshal(bidExt)
if err == nil {
err = json.Unmarshal(extbyte, &bidExtMap)
if err == nil && bidExtMap[bidTypeExtKey] != nil {
bidTypeVal := int(bidExtMap[bidTypeExtKey].(float64))
switch bidTypeVal {
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
}
}
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
Expand Down
33 changes: 18 additions & 15 deletions adapters/pubmatic/pubmatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,46 +674,49 @@ func TestPubmaticSampleRequest(t *testing.T) {
}

func TestGetBidTypeVideo(t *testing.T) {
extJSON := `{"BidType":1}`
extrm := json.RawMessage(extJSON)
actualBidTypeValue := getBidType(extrm)
pubmaticExt := new(pubmaticBidExt)
pubmaticExt.BidType = new(int)
*pubmaticExt.BidType = 1
actualBidTypeValue := getBidType(pubmaticExt)
if actualBidTypeValue != openrtb_ext.BidTypeVideo {
t.Errorf("Expected Bid Type value was: %v, actual value is: %v", openrtb_ext.BidTypeVideo, actualBidTypeValue)
}
}

func TestGetBidTypeForMissingBidTypeExt(t *testing.T) {
extJSON := `{}`
extrm := json.RawMessage(extJSON)
actualBidTypeValue := getBidType(extrm)
pubmaticExt := pubmaticBidExt{}
actualBidTypeValue := getBidType(&pubmaticExt)
// banner is the default bid type when no bidType key is present in the bid.ext
if actualBidTypeValue != "banner" {
t.Errorf("Expected Bid Type value was: banner, actual value is: %v", actualBidTypeValue)
}
}

func TestGetBidTypeBanner(t *testing.T) {
extJSON := `{"BidType":0}`
extrm := json.RawMessage(extJSON)
actualBidTypeValue := getBidType(extrm)
pubmaticExt := new(pubmaticBidExt)
pubmaticExt.BidType = new(int)
*pubmaticExt.BidType = 0
actualBidTypeValue := getBidType(pubmaticExt)
if actualBidTypeValue != openrtb_ext.BidTypeBanner {
t.Errorf("Expected Bid Type value was: %v, actual value is: %v", openrtb_ext.BidTypeBanner, actualBidTypeValue)
}
}

func TestGetBidTypeNative(t *testing.T) {
extJSON := `{"BidType":2}`
extrm := json.RawMessage(extJSON)
actualBidTypeValue := getBidType(extrm)
pubmaticExt := new(pubmaticBidExt)
pubmaticExt.BidType = new(int)
*pubmaticExt.BidType = 2
actualBidTypeValue := getBidType(pubmaticExt)
if actualBidTypeValue != openrtb_ext.BidTypeNative {
t.Errorf("Expected Bid Type value was: %v, actual value is: %v", openrtb_ext.BidTypeNative, actualBidTypeValue)
}
}

func TestGetBidTypeForUnsupportedCode(t *testing.T) {
extJSON := `{"BidType":99}`
extrm := json.RawMessage(extJSON)
actualBidTypeValue := getBidType(extrm)
pubmaticExt := new(pubmaticBidExt)
pubmaticExt.BidType = new(int)
*pubmaticExt.BidType = 99
actualBidTypeValue := getBidType(pubmaticExt)
if actualBidTypeValue != openrtb_ext.BidTypeBanner {
t.Errorf("Expected Bid Type value was: %v, actual value is: %v", openrtb_ext.BidTypeBanner, actualBidTypeValue)
}
Expand Down
21 changes: 17 additions & 4 deletions adapters/pubmatic/pubmatictest/exemplary/video.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@
"h": 250,
"w": 300,
"dealid":"test deal",
"cat" : ["IAB-1", "IAB-2"],
"ext": {
"dspid": 6,
"deal_channel": 1,
"BidType": 1
"BidType": 1,
"video" : {
"duration" : 5
}
}
}]
}
Expand All @@ -139,19 +143,28 @@
"adid": "29681110",
"adm": "some-test-ad",
"adomain": ["pubmatic.com"],
"cat": [
"IAB-1"
],
"crid": "29681110",
"w": 300,
"h": 250,
"dealid":"test deal",
"ext": {
"dspid": 6,
"deal_channel": 1,
"BidType": 1
"BidType": 1,
"video" : {
"duration" : 5
}
}
},
"type": "video"
"type": "video",
"video" :{
"duration" : 5
}
}
]
}
]
}
}

0 comments on commit f1582a4

Please sign in to comment.