From 5e94b1662f826cd85599b0185d706c61cefc1d57 Mon Sep 17 00:00:00 2001 From: Avinash Kapre Date: Thu, 25 Jul 2024 19:23:49 +0530 Subject: [PATCH] response changes --- adapters/pubmatic/pubmatic.go | 27 ++++++++---- adapters/pubmatic/pubmatic_test.go | 4 +- .../pubmatic/openwrap/auctionresponsehook.go | 44 +++++++++++++++++++ modules/pubmatic/openwrap/models/constants.go | 2 + 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/adapters/pubmatic/pubmatic.go b/adapters/pubmatic/pubmatic.go index 414b28828f0..f7916038091 100644 --- a/adapters/pubmatic/pubmatic.go +++ b/adapters/pubmatic/pubmatic.go @@ -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) @@ -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 { @@ -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 } diff --git a/adapters/pubmatic/pubmatic_test.go b/adapters/pubmatic/pubmatic_test.go index f484cd0af8e..4dfc55683af 100644 --- a/adapters/pubmatic/pubmatic_test.go +++ b/adapters/pubmatic/pubmatic_test.go @@ -351,7 +351,7 @@ 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", @@ -359,7 +359,7 @@ 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(`{"bidder":{}}`), }, }, }, diff --git a/modules/pubmatic/openwrap/auctionresponsehook.go b/modules/pubmatic/openwrap/auctionresponsehook.go index e6cbe7912ab..a28ae080e1a 100644 --- a/modules/pubmatic/openwrap/auctionresponsehook.go +++ b/modules/pubmatic/openwrap/auctionresponsehook.go @@ -3,6 +3,7 @@ package openwrap import ( "context" "encoding/json" + "regexp" "strconv" "time" @@ -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) @@ -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 diff --git a/modules/pubmatic/openwrap/models/constants.go b/modules/pubmatic/openwrap/models/constants.go index 7025adf2520..f74175c520b 100755 --- a/modules/pubmatic/openwrap/models/constants.go +++ b/modules/pubmatic/openwrap/models/constants.go @@ -395,6 +395,8 @@ const ( AppPlatformKey = "appPlatform" IntegrationPathKey = "integrationPath" SubIntegrationPathKey = "subIntegrationPath" + + AppLovinMaxImpressionPattern = `_mf.*` ) const (