Skip to content

Commit

Permalink
ImproveDigital: updates (#3077)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishihanvcs authored Sep 29, 2023
1 parent f879f63 commit 0d488ee
Show file tree
Hide file tree
Showing 7 changed files with 685 additions and 29 deletions.
98 changes: 72 additions & 26 deletions adapters/improvedigital/improvedigital.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ package improvedigital
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/prebid/openrtb/v19/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
"net/http"
"regexp"
"strconv"
"strings"
)

const (
buyingTypeRTB = "rtb"
isRewardedInventory = "is_rewarded_inventory"
stateRewardedInventoryEnable = "1"
consentProvidersSettingsInputKey = "ConsentedProvidersSettings"
Expand Down Expand Up @@ -43,6 +42,8 @@ type ImpExtBidder struct {
}
}

var dealDetectionRegEx = regexp.MustCompile("(classic|deal)")

// MakeRequests makes the HTTP requests which should be made to fetch bids.
func (a *ImprovedigitalAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
numRequests := len(request.Imp)
Expand Down Expand Up @@ -119,6 +120,7 @@ func (a *ImprovedigitalAdapter) MakeBids(internalRequest *openrtb2.BidRequest, e
}

var bidResp openrtb2.BidResponse
var impMap = make(map[string]openrtb2.Imp)
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
Expand All @@ -134,18 +136,21 @@ func (a *ImprovedigitalAdapter) MakeBids(internalRequest *openrtb2.BidRequest, e
}

seatBid := bidResp.SeatBid[0]

if len(seatBid.Bid) == 0 {
return nil, nil
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(seatBid.Bid))
bidResponse.Currency = bidResp.Cur

for i := range internalRequest.Imp {
impMap[internalRequest.Imp[i].ID] = internalRequest.Imp[i]
}

for i := range seatBid.Bid {
bid := seatBid.Bid[i]

bidType, err := getMediaTypeForImp(bid.ImpID, internalRequest.Imp)
bidType, err := getBidType(bid, impMap)
if err != nil {
return nil, []error{err}
}
Expand All @@ -158,7 +163,7 @@ func (a *ImprovedigitalAdapter) MakeBids(internalRequest *openrtb2.BidRequest, e
}

bidExtImprovedigital := bidExt.Improvedigital
if bidExtImprovedigital.LineItemID != 0 && bidExtImprovedigital.BuyingType != "" && bidExtImprovedigital.BuyingType != buyingTypeRTB {
if bidExtImprovedigital.LineItemID != 0 && dealDetectionRegEx.MatchString(bidExtImprovedigital.BuyingType) {
bid.DealID = strconv.Itoa(bidExtImprovedigital.LineItemID)
}
}
Expand All @@ -169,7 +174,6 @@ func (a *ImprovedigitalAdapter) MakeBids(internalRequest *openrtb2.BidRequest, e
})
}
return bidResponse, nil

}

// Builder builds a new instance of the Improvedigital adapter for the given bidder with the given config.
Expand All @@ -180,31 +184,73 @@ func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server co
return bidder, nil
}

func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
}

if imp.Video != nil {
return openrtb_ext.BidTypeVideo, nil
}
func getBidType(bid openrtb2.Bid, impMap map[string]openrtb2.Imp) (openrtb_ext.BidType, error) {
// there must be a matching imp against bid.ImpID
imp, found := impMap[bid.ImpID]
if !found {
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to find impression for ID: \"%s\"", bid.ImpID),
}
}

if imp.Native != nil {
return openrtb_ext.BidTypeNative, nil
// if MType is not set in server response, try to determine it
if bid.MType == 0 {
if !isMultiFormatImp(imp) {
// Not a bid for multi format impression. So, determine MType from impression
if imp.Banner != nil {
bid.MType = openrtb2.MarkupBanner
} else if imp.Video != nil {
bid.MType = openrtb2.MarkupVideo
} else if imp.Audio != nil {
bid.MType = openrtb2.MarkupAudio
} else if imp.Native != nil {
bid.MType = openrtb2.MarkupNative
} else { // This should not happen.
// Let's handle it just in case by returning an error.
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Could not determine MType from impression with ID: \"%s\"", bid.ImpID),
}
}

} else {
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unknown impression type for ID: \"%s\"", impID),
Message: fmt.Sprintf("Bid must have non-zero MType for multi format impression with ID: \"%s\"", bid.ImpID),
}
}
}

// This shouldnt happen. Lets handle it just incase by returning an error.
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to find impression for ID: \"%s\"", impID),
// map MType to BidType
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
case openrtb2.MarkupAudio:
return openrtb_ext.BidTypeAudio, nil
case openrtb2.MarkupNative:
return openrtb_ext.BidTypeNative, nil
default:
// This shouldn't happen. Let's handle it just in case by returning an error.
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unsupported MType %d for impression with ID: \"%s\"", bid.MType, bid.ImpID),
}
}
}

func isMultiFormatImp(imp openrtb2.Imp) bool {
formatCount := 0
if imp.Banner != nil {
formatCount++
}
if imp.Video != nil {
formatCount++
}
if imp.Audio != nil {
formatCount++
}
if imp.Native != nil {
formatCount++
}
return formatCount > 1
}

// This method responsible to clone request and convert additional consent providers string to array when additional consent provider found
Expand Down
105 changes: 105 additions & 0 deletions adapters/improvedigital/improvedigitaltest/exemplary/app-multi.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@
"placementId": 13244
}
}
},
{
"id": "test-multi-format-id-with-mtype",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1920,
"h": 1080
},
"ext": {
"bidder": {
"placementId": 13244
}
}
}
]
},
Expand Down Expand Up @@ -159,6 +181,71 @@
"cur": "USD"
}
}
},
{
"expectedRequest": {
"uri": "http://localhost/pbs",
"body": {
"id": "test-request-id",
"app": {
"id": "appID",
"publisher": {
"id": "uniq_pub_id"
}
},
"device": {
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
},
"imp": [
{
"id": "test-multi-format-id-with-mtype",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1920,
"h": 1080
},
"ext": {
"bidder": {
"placementId": 13244
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "improvedigital",
"bid": [
{
"id": "randomid2",
"impid": "test-multi-format-id-with-mtype",
"price": 0.5,
"adm": "some-test-ad-vast",
"crid": "1234567",
"w": 1920,
"h": 1080,
"mtype": 1
}
]
}
],
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
Expand Down Expand Up @@ -197,6 +284,24 @@
"type": "video"
}
]
},
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "randomid2",
"impid": "test-multi-format-id-with-mtype",
"price": 0.5,
"adm": "some-test-ad-vast",
"crid": "1234567",
"w": 1920,
"h": 1080,
"mtype": 1
},
"type": "banner"
}
]
}
]
}
Loading

0 comments on commit 0d488ee

Please sign in to comment.