Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubicon: Update segtax logic #1909

Merged
merged 5 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 53 additions & 28 deletions adapters/rubicon/rubicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ type rubiconExtUserTpID struct {
UID string `json:"uid"`
}

type rubiconUserDataExt struct {
TaxonomyName string `json:"taxonomyname"`
type rubiconDataExt struct {
SegTax int `json:"segtax"`
}

type rubiconUserExt struct {
Expand All @@ -105,7 +105,8 @@ type rubiconUserExt struct {
}

type rubiconSiteExtRP struct {
SiteID int `json:"site_id"`
SiteID int `json:"site_id"`
Target json.RawMessage `json:"target,omitempty"`
}

type rubiconSiteExt struct {
Expand Down Expand Up @@ -756,12 +757,13 @@ func (a *RubiconAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ada

if request.User != nil {
userCopy := *request.User
userExtRP := rubiconUserExt{RP: rubiconUserExtRP{Target: rubiconExt.Visitor}}

if err := updateUserExtWithIabAttribute(&userExtRP, userCopy.Data); err != nil {
target, err := updateExtWithIabAttribute(rubiconExt.Visitor, userCopy.Data, []int{4})
if err != nil {
errs = append(errs, err)
continue
}
userExtRP := rubiconUserExt{RP: rubiconUserExtRP{Target: target}}

if request.User.Ext != nil {
var userExt *openrtb_ext.ExtUser
Expand Down Expand Up @@ -849,19 +851,30 @@ func (a *RubiconAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ada
thisImp.Video = nil
}

siteExt := rubiconSiteExt{RP: rubiconSiteExtRP{SiteID: rubiconExt.SiteId}}
pubExt := rubiconPubExt{RP: rubiconPubExtRP{AccountID: rubiconExt.AccountId}}

if request.Site != nil {
siteCopy := *request.Site
siteCopy.Ext, err = json.Marshal(&siteExt)
siteExtRP := rubiconSiteExt{RP: rubiconSiteExtRP{SiteID: rubiconExt.SiteId}}
target, err := updateExtWithIabAttribute(nil, siteCopy.Content.Data, []int{1, 2})
if err != nil {
errs = append(errs, err)
continue
}
siteExtRP.RP.Target = target

siteCopy.Ext, err = json.Marshal(&siteExtRP)
if err != nil {
errs = append(errs, err)
continue
}

siteCopy.Publisher = &openrtb2.Publisher{}
siteCopy.Publisher.Ext, err = json.Marshal(&pubExt)
rubiconRequest.Site = &siteCopy
}
if request.App != nil {
} else {
appCopy := *request.App
appCopy.Ext, err = json.Marshal(&siteExt)
appCopy.Ext, err = json.Marshal(rubiconSiteExt{RP: rubiconSiteExtRP{SiteID: rubiconExt.SiteId}})
appCopy.Publisher = &openrtb2.Publisher{}
appCopy.Publisher.Ext, err = json.Marshal(&pubExt)
rubiconRequest.App = &appCopy
Expand Down Expand Up @@ -908,41 +921,53 @@ func resolveBidFloorAttributes(bidFloor float64, bidFloorCur string) (float64, s
return bidFloor, bidFloorCur
}

func updateUserExtWithIabAttribute(userExtRP *rubiconUserExt, data []openrtb2.Data) error {
func updateExtWithIabAttribute(target json.RawMessage, data []openrtb2.Data, segTaxes []int) (json.RawMessage, error) {
var segmentIdsToCopy = getSegmentIdsToCopy(data, segTaxes)

extRPTarget := make(map[string]interface{})

if target != nil {
if err := json.Unmarshal(target, &extRPTarget); err != nil {
return nil, &errortypes.BadInput{Message: err.Error()}
}
}

extRPTarget["iab"] = segmentIdsToCopy

jsonTarget, err := json.Marshal(&extRPTarget)
if err != nil {
return nil, &errortypes.BadInput{Message: err.Error()}
}
return jsonTarget, nil
}

func getSegmentIdsToCopy(data []openrtb2.Data, segTaxValues []int) []string {
var segmentIdsToCopy = make([]string, 0)

for _, dataRecord := range data {
if dataRecord.Ext != nil {
var dataExtObject rubiconUserDataExt
var dataExtObject rubiconDataExt
err := json.Unmarshal(dataRecord.Ext, &dataExtObject)
if err != nil {
continue
}
if strings.EqualFold(dataExtObject.TaxonomyName, "iab") {
if contains(segTaxValues, dataExtObject.SegTax) {
for _, segment := range dataRecord.Segment {
segmentIdsToCopy = append(segmentIdsToCopy, segment.ID)
}
}
}
}
return segmentIdsToCopy
}

userExtRPTarget := make(map[string]interface{})

if userExtRP.RP.Target != nil {
if err := json.Unmarshal(userExtRP.RP.Target, &userExtRPTarget); err != nil {
return &errortypes.BadInput{Message: err.Error()}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}

userExtRPTarget["iab"] = segmentIdsToCopy

if target, err := json.Marshal(&userExtRPTarget); err != nil {
return &errortypes.BadInput{Message: err.Error()}
} else {
userExtRP.RP.Target = target
}

return nil
return false
}

func getTpIdsAndSegments(eids []openrtb_ext.ExtUserEid) (mappedRubiconUidsParam, []error) {
Expand Down
28 changes: 28 additions & 0 deletions adapters/rubicon/rubicon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,10 @@ func TestOpenRTBRequest(t *testing.T) {
}
}}`),
}},
App: &openrtb2.App{
ID: "com.test",
Name: "testApp",
},
Device: &openrtb2.Device{
PxRatio: rubidata.devicePxRatio,
},
Expand Down Expand Up @@ -1154,6 +1158,10 @@ func TestOpenRTBRequestWithBannerImpEvenIfImpHasVideo(t *testing.T) {
"visitor": {"key2" : "val2"}
}}`),
}},
App: &openrtb2.App{
ID: "com.test",
Name: "testApp",
},
}

reqs, errs := bidder.MakeRequests(request, &adapters.ExtraRequestInfo{})
Expand Down Expand Up @@ -1203,6 +1211,10 @@ func TestOpenRTBRequestWithImpAndAdSlotIncluded(t *testing.T) {
}
}`),
}},
App: &openrtb2.App{
ID: "com.test",
Name: "testApp",
},
}

reqs, _ := bidder.MakeRequests(request, &adapters.ExtraRequestInfo{})
Expand Down Expand Up @@ -1258,6 +1270,10 @@ func TestOpenRTBRequestWithBadvOverflowed(t *testing.T) {
}
}`),
}},
App: &openrtb2.App{
ID: "com.test",
Name: "testApp",
},
}

reqs, _ := bidder.MakeRequests(request, &adapters.ExtraRequestInfo{})
Expand Down Expand Up @@ -1291,6 +1307,10 @@ func TestOpenRTBRequestWithSpecificExtUserEids(t *testing.T) {
"accountId": 7891
}}`),
}},
App: &openrtb2.App{
ID: "com.test",
Name: "testApp",
},
User: &openrtb2.User{
Ext: json.RawMessage(`{"eids": [
{
Expand Down Expand Up @@ -1399,6 +1419,10 @@ func TestOpenRTBRequestWithVideoImpEvenIfImpHasBannerButAllRequiredVideoFields(t
"video": {"size_id": 1}
}}`),
}},
App: &openrtb2.App{
ID: "com.test",
Name: "testApp",
},
}

reqs, errs := bidder.MakeRequests(request, &adapters.ExtraRequestInfo{})
Expand Down Expand Up @@ -1444,6 +1468,10 @@ func TestOpenRTBRequestWithVideoImpAndEnabledRewardedInventoryFlag(t *testing.T)
"video": {"size_id": 1}
}}`),
}},
App: &openrtb2.App{
ID: "com.test",
Name: "testApp",
},
}

reqs, _ := bidder.MakeRequests(request, &adapters.ExtraRequestInfo{})
Expand Down
Loading