Skip to content

Commit

Permalink
response changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AvinashKapre committed Jul 25, 2024
1 parent bb63822 commit 5e94b16
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
27 changes: 19 additions & 8 deletions adapters/pubmatic/pubmatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,13 @@ func (a *PubmaticAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ad
extractWrapperExtFromImp = false
}

var floors []float64
floorsImpMap := map[string][]float64{}

for i := 0; i < len(request.Imp); i++ {
var wrapperExtFromImp *pubmaticWrapperExt
var pubIDFromImp string
var err error
wrapperExtFromImp, pubIDFromImp, floors, err = parseImpressionObject(&request.Imp[i], extractWrapperExtFromImp, extractPubIDFromImp)

wrapperExtFromImp, pubIDFromImp, floors, 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)
Expand All @@ -131,6 +130,10 @@ func (a *PubmaticAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ad
continue
}

if len(floors) > 0 {
floorsImpMap[request.Imp[i].ID] = floors
}

if extractWrapperExtFromImp {
if wrapperExtFromImp != nil {
if wrapperExt == nil {
Expand Down Expand Up @@ -235,17 +238,25 @@ func (a *PubmaticAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ad
request.Device.DNT = request.Device.Lmt
}

if len(floors) > 0 {
requestData := make([]*adapters.RequestData, 0, len(floors))
for i, floor := range floors {
if len(floorsImpMap) > 0 {
requestData := make([]*adapters.RequestData, 0, 3)
for i := 0; i < 3; i++ {
for j := range request.Imp {
floors, ok := floorsImpMap[request.Imp[j].ID]
if !ok || len(floors) <= i {
continue
}
impcopy := &request.Imp[j]
impcopy.BidFloor = floor
impcopy.ID = fmt.Sprintf("%s_%d", impcopy.ID, i+1)
impcopy.BidFloor = floors[i]
impcopy.ID = fmt.Sprintf("%s_mf%d", impcopy.ID, i+1)
}
newRequestData, newErrData := a.buildAdapterRequest(request, cookies)
errs = append(errs, newErrData...)
requestData = append(requestData, newRequestData...)
for j := range request.Imp {
impcopy := &request.Imp[j]
impcopy.ID = strings.TrimSuffix(impcopy.ID, fmt.Sprintf("_mf%d", i+1))
}
}
return requestData, errs
}
Expand Down
4 changes: 2 additions & 2 deletions adapters/pubmatic/pubmatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,15 @@ func TestPubmaticAdapter_MakeRequests(t *testing.T) {
W: ptrutil.ToPtr[int64](300),
H: ptrutil.ToPtr[int64](250),
},
Ext: json.RawMessage(`{"bidder":{"applovin_floors":[1.1,2.2,3.3]}}`),
Ext: json.RawMessage(`{}`),
},
{
ID: "test-imp-id_new",
Banner: &openrtb2.Banner{
W: ptrutil.ToPtr[int64](300),
H: ptrutil.ToPtr[int64](250),
},
Ext: json.RawMessage(`{"bidder":{"applovin_floors":[1.1,2.2,3.3]}}`),
Ext: json.RawMessage(`{"bidder":{}}`),
},
},
},
Expand Down
44 changes: 44 additions & 0 deletions modules/pubmatic/openwrap/auctionresponsehook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openwrap
import (
"context"
"encoding/json"
"regexp"
"strconv"
"time"

Expand Down Expand Up @@ -81,6 +82,9 @@ func (m OpenWrap) handleAuctionResponseHook(

anyDealTierSatisfyingBid := false
winningBids := make(models.WinningBids)

payload.BidResponse = updateMultiFloorResponse(payload.BidResponse)

for _, seatBid := range payload.BidResponse.SeatBid {
for _, bid := range seatBid.Bid {
m.metricEngine.RecordPlatformPublisherPartnerResponseStats(rctx.Platform, rctx.PubIDStr, seatBid.Seat)
Expand Down Expand Up @@ -405,6 +409,46 @@ func (m OpenWrap) handleAuctionResponseHook(
return result, nil
}

func updateMultiFloorResponse(bidResponse *openrtb2.BidResponse) *openrtb2.BidResponse {
pubMaticResponseMap := map[string][]openrtb2.Bid{}
for _, seatBid := range bidResponse.SeatBid {
if seatBid.Seat == "pubmatic" {
for _, bid := range seatBid.Bid {
if bid.Price == 0 {
continue
}
bid.ImpID = trimSuffixWithPattern(bid.ImpID)
pubMaticResponseMap[bid.ImpID] = append(pubMaticResponseMap[bid.ImpID], bid)
}
}
}
winningBids := []openrtb2.Bid{}
for _, bid := range pubMaticResponseMap {
if len(bid) == 0 {
continue
}
winningBid := bid[0]
for i := 1; i < len(bid); i++ {
if bid[i].Price > winningBid.Price {
winningBid = bid[i]
}
}
winningBids = append(winningBids, winningBid)
}

for i, seatBid := range bidResponse.SeatBid {
if seatBid.Seat == "pubmatic" {
bidResponse.SeatBid[i].Bid = winningBids
}
}
return bidResponse
}

func trimSuffixWithPattern(input string) string {
re := regexp.MustCompile(models.AppLovinMaxImpressionPattern)
return re.ReplaceAllString(input, "")
}

func (m *OpenWrap) updateORTBV25Response(rctx models.RequestCtx, bidResponse *openrtb2.BidResponse) (*openrtb2.BidResponse, error) {
if len(bidResponse.SeatBid) == 0 {
return bidResponse, nil
Expand Down
2 changes: 2 additions & 0 deletions modules/pubmatic/openwrap/models/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ const (
AppPlatformKey = "appPlatform"
IntegrationPathKey = "integrationPath"
SubIntegrationPathKey = "subIntegrationPath"

AppLovinMaxImpressionPattern = `_mf.*`
)

const (
Expand Down

0 comments on commit 5e94b16

Please sign in to comment.