Skip to content

Commit

Permalink
UOE-10971: Send correct MultiBidMultiFLoor records to analytics (#962)
Browse files Browse the repository at this point in the history
* Send correct multiBidMultiFLoor records to analytics

* test1

* test 2

* test3

* test4

* test5

* test6

* test cases

* comment fix

* review comment

* review comment2

* variable rename

* refactor

* refactor2
  • Loading branch information
AvinashKapre authored Nov 25, 2024
1 parent 20ff91b commit ee1ada2
Show file tree
Hide file tree
Showing 14 changed files with 1,271 additions and 583 deletions.
76 changes: 17 additions & 59 deletions adapters/pubmatic/pubmatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"

Expand All @@ -24,22 +23,18 @@ import (
const MAX_IMPRESSIONS_PUBMATIC = 30
const MAX_MULTIFLOORS_PUBMATIC = 3

var re = regexp.MustCompile(appLovinMaxImpressionPattern)

const (
ae = "ae"
PUBMATIC = "[PUBMATIC]"
buyId = "buyid"
buyIdTargetingKey = "hb_buyid_"
skAdnetworkKey = "skadn"
rewardKey = "reward"
dctrKeywordName = "dctr"
urlEncodedEqualChar = "%3D"
AdServerKey = "adserver"
PBAdslotKey = "pbadslot"
bidViewability = "bidViewability"
multiFloors = "_mf"
appLovinMaxImpressionPattern = "_mf.*"
ae = "ae"
PUBMATIC = "[PUBMATIC]"
buyId = "buyid"
buyIdTargetingKey = "hb_buyid_"
skAdnetworkKey = "skadn"
rewardKey = "reward"
dctrKeywordName = "dctr"
urlEncodedEqualChar = "%3D"
AdServerKey = "adserver"
PBAdslotKey = "pbadslot"
bidViewability = "bidViewability"
)

type PubmaticAdapter struct {
Expand Down Expand Up @@ -264,44 +259,6 @@ func (a *PubmaticAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ad
return requestData, errs
}

// buildMultiFloorRequests builds multiple requests for each floor value
func (a *PubmaticAdapter) buildMultiFloorRequests(request *openrtb2.BidRequest, impFloorsMap map[string][]float64, cookies []string) ([]*adapters.RequestData, []error) {
requestData := []*adapters.RequestData{}
errs := make([]error, 0, MAX_MULTIFLOORS_PUBMATIC*len(request.Imp))

for i := 0; i < MAX_MULTIFLOORS_PUBMATIC; i++ {
isFloorsUpdated := false
newImps := make([]openrtb2.Imp, len(request.Imp))
copy(newImps, request.Imp)
//TODO-AK: Remove the imp from the request if the floor is not present except for the first floor
for j := range newImps {
floors, ok := impFloorsMap[request.Imp[j].ID]
if !ok || len(floors) <= i {
continue
}
isFloorsUpdated = true
newImps[j].BidFloor = floors[i]
newImps[j].ID = fmt.Sprintf("%s"+multiFloors+"%d", newImps[j].ID, i+1)
}

if !isFloorsUpdated {
continue
}

newRequest := *request
newRequest.Imp = newImps

newRequestData, errData := a.buildAdapterRequest(&newRequest, cookies)
if errData != nil {
errs = append(errs, errData)
}
if len(newRequestData) > 0 {
requestData = append(requestData, newRequestData...)
}
}
return requestData, errs
}

// buildAdapterRequest builds the request for Pubmatic
func (a *PubmaticAdapter) buildAdapterRequest(request *openrtb2.BidRequest, cookies []string) ([]*adapters.RequestData, error) {
reqJSON, err := json.Marshal(request)
Expand Down Expand Up @@ -645,7 +602,12 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externa
targets := getTargetingKeys(sb.Ext, string(externalRequest.BidderName))
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]
bid.ImpID = trimSuffixWithPattern(bid.ImpID)

//If imp is multi-floored then update the bid.ext.mbmfv with the floor value
if appLovinMaxImpressionRegex.MatchString(bid.ImpID) {
bid.Ext = updateBidExtWithMultiFloor(bid.ImpID, bid.Ext, externalRequest.Body)
bid.ImpID = trimSuffixWithPattern(bid.ImpID)
}

bid.Ext = renameTransparencyParamsKey(bid.Ext)
// Copy SeatBid Ext to Bid.Ext
Expand Down Expand Up @@ -709,10 +671,6 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externa
return bidResponse, errs
}

func trimSuffixWithPattern(input string) string {
return re.ReplaceAllString(input, "")
}

func getNativeAdm(adm string) (string, error) {
var err error
nativeAdm := make(map[string]interface{})
Expand Down
85 changes: 83 additions & 2 deletions adapters/pubmatic/pubmatic_ow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@ import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strconv"

"github.com/buger/jsonparser"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)

const (
dsaKey = "dsa"
transparencyKey = "transparency"
dsaKey = "dsa"
transparencyKey = "transparency"
multiFloors = "_mf"
appLovinMaxImpressionPattern = `_mf[0-9]+$`
multiBidMultiFloorValueKey = "mbmfv"
)

var (
paramKey = []byte(`"params"`)
dsaParamKey = []byte(`"dsaparams"`)
)

var appLovinMaxImpressionRegex = regexp.MustCompile(appLovinMaxImpressionPattern)

func getTargetingKeys(bidExt json.RawMessage, bidderName string) map[string]string {
targets := map[string]string{}
if bidExt != nil {
Expand Down Expand Up @@ -102,3 +109,77 @@ func renameTransparencyParamsKey(bidExt []byte) []byte {

return bidExt
}

// buildMultiFloorRequests builds multiple requests for each floor value
func (a *PubmaticAdapter) buildMultiFloorRequests(request *openrtb2.BidRequest, impFloorsMap map[string][]float64, cookies []string) ([]*adapters.RequestData, []error) {
requestData := make([]*adapters.RequestData, 0, MAX_MULTIFLOORS_PUBMATIC*len(request.Imp))
errs := make([]error, 0, MAX_MULTIFLOORS_PUBMATIC*len(request.Imp))

for i := 0; i < MAX_MULTIFLOORS_PUBMATIC; i++ {
isFloorsUpdated := false
newImps := make([]openrtb2.Imp, len(request.Imp))
copy(newImps, request.Imp)
//TODO-AK: Remove the imp from the request if the floor is not present except for the first floor
for j := range newImps {
floors, ok := impFloorsMap[request.Imp[j].ID]
if !ok || len(floors) <= i {
continue
}
isFloorsUpdated = true
newImps[j].BidFloor = floors[i]
newImps[j].ID = fmt.Sprintf("%s"+multiFloors+"%d", newImps[j].ID, i+1)
}

if !isFloorsUpdated {
continue
}

newRequest := *request
newRequest.Imp = newImps

newRequestData, errData := a.buildAdapterRequest(&newRequest, cookies)
if errData != nil {
errs = append(errs, errData)
}
if len(newRequestData) > 0 {
requestData = append(requestData, newRequestData...)
}
}
return requestData, errs
}

func trimSuffixWithPattern(input string) string {
return appLovinMaxImpressionRegex.ReplaceAllString(input, "")
}

func updateBidExtWithMultiFloor(bidImpID string, bidExt, reqBody []byte) []byte {
var externalRequest openrtb2.BidRequest

if err := json.Unmarshal(reqBody, &externalRequest); err != nil {
return bidExt
}

updatedBidExt := bidExt
if bidExt == nil {
updatedBidExt = json.RawMessage(`{}`)
}

for _, imp := range externalRequest.Imp {
if imp.ID != bidImpID {
continue
}
if imp.BidFloor <= 0 {
continue
}
var err error
updatedBidExt, err = jsonparser.Set(updatedBidExt, []byte(fmt.Sprintf("%f", imp.BidFloor)), multiBidMultiFloorValueKey)
if err != nil {
return bidExt
}
}

if string(updatedBidExt) != "{}" {
return updatedBidExt
}
return bidExt
}
Loading

0 comments on commit ee1ada2

Please sign in to comment.