forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Openrtb2blocking module part 3: RawBidderResponse Hook implementation (…
…prebid#2534) Co-authored-by: 4lexvav <[email protected]>
- Loading branch information
Showing
11 changed files
with
1,498 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hookstage | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/prebid/prebid-server/adapters" | ||
) | ||
|
||
func (c *ChangeSet[T]) RawBidderResponse() ChangeSetRawBidderResponse[T] { | ||
return ChangeSetRawBidderResponse[T]{changeSet: c} | ||
} | ||
|
||
type ChangeSetRawBidderResponse[T any] struct { | ||
changeSet *ChangeSet[T] | ||
} | ||
|
||
func (c ChangeSetRawBidderResponse[T]) Bids() ChangeSetBids[T] { | ||
return ChangeSetBids[T]{changeSetRawBidderResponse: c} | ||
} | ||
|
||
func (c ChangeSetRawBidderResponse[T]) castPayload(p T) (RawBidderResponsePayload, error) { | ||
if payload, ok := any(p).(RawBidderResponsePayload); ok { | ||
return payload, nil | ||
} | ||
return RawBidderResponsePayload{}, errors.New("failed to cast RawBidderResponsePayload") | ||
} | ||
|
||
type ChangeSetBids[T any] struct { | ||
changeSetRawBidderResponse ChangeSetRawBidderResponse[T] | ||
} | ||
|
||
func (c ChangeSetBids[T]) Update(bids []*adapters.TypedBid) { | ||
c.changeSetRawBidderResponse.changeSet.AddMutation(func(p T) (T, error) { | ||
bidderPayload, err := c.changeSetRawBidderResponse.castPayload(p) | ||
if err == nil { | ||
bidderPayload.Bids = bids | ||
} | ||
if payload, ok := any(bidderPayload).(T); ok { | ||
return payload, nil | ||
} | ||
return p, errors.New("failed to cast RawBidderResponsePayload") | ||
}, MutationUpdate, "bids") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ortb2blocking | ||
|
||
import ( | ||
"github.com/prebid/prebid-server/hooks/hookanalytics" | ||
"github.com/prebid/prebid-server/hooks/hookstage" | ||
) | ||
|
||
const enforceBlockingTag = "enforce_blocking" | ||
|
||
const ( | ||
attributesAnalyticKey = "attributes" | ||
badvAnalyticKey = "adomain" | ||
cattaxAnalyticKey = "bcat" | ||
bappAnalyticKey = "bundle" | ||
battrAnalyticKey = "attr" | ||
) | ||
|
||
// ortb2blocking module has only 1 activity: `enforce_blocking` which will be used in further result processing | ||
func newEnforceBlockingTags() hookanalytics.Analytics { | ||
return hookanalytics.Analytics{ | ||
Activities: []hookanalytics.Activity{ | ||
{ | ||
Name: enforceBlockingTag, | ||
Status: hookanalytics.ActivityStatusSuccess, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func addFailedStatusTag(result *hookstage.HookResult[hookstage.RawBidderResponsePayload]) { | ||
result.AnalyticsTags.Activities[0].Status = hookanalytics.ActivityStatusError | ||
} | ||
|
||
func addAllowedAnalyticTag(result *hookstage.HookResult[hookstage.RawBidderResponsePayload], bidder, ImpID string) { | ||
newAllowedResult := hookanalytics.Result{ | ||
Status: hookanalytics.ResultStatusAllow, | ||
AppliedTo: hookanalytics.AppliedTo{ | ||
Bidder: bidder, | ||
ImpIds: []string{ImpID}, | ||
}, | ||
} | ||
|
||
result.AnalyticsTags.Activities[0].Results = append(result.AnalyticsTags.Activities[0].Results, newAllowedResult) | ||
} | ||
|
||
func addBlockedAnalyticTag( | ||
result *hookstage.HookResult[hookstage.RawBidderResponsePayload], | ||
bidder, ImpID string, | ||
failedAttributes []string, | ||
data map[string]interface{}, | ||
) { | ||
values := make(map[string]interface{}) | ||
|
||
values[attributesAnalyticKey] = failedAttributes | ||
for _, attribute := range [5]string{ | ||
"badv", | ||
"bcat", | ||
"cattax", | ||
"bapp", | ||
"battr", | ||
} { | ||
if _, ok := data[attribute]; ok { | ||
analyticKey := getAnalyticKeyForAttribute(attribute) | ||
values[analyticKey] = data[attribute] | ||
} | ||
} | ||
|
||
newBlockedResult := hookanalytics.Result{ | ||
Status: hookanalytics.ResultStatusBlock, | ||
Values: values, | ||
AppliedTo: hookanalytics.AppliedTo{ | ||
Bidder: bidder, | ||
ImpIds: []string{ImpID}, | ||
}, | ||
} | ||
|
||
result.AnalyticsTags.Activities[0].Results = append(result.AnalyticsTags.Activities[0].Results, newBlockedResult) | ||
} | ||
|
||
// most of the attributes have their own representation for an analytic key | ||
func getAnalyticKeyForAttribute(attribute string) string { | ||
switch attribute { | ||
case "badv": | ||
return badvAnalyticKey | ||
case "cattax": | ||
return cattaxAnalyticKey | ||
case "bapp": | ||
return bappAnalyticKey | ||
case "battr": | ||
return battrAnalyticKey | ||
default: | ||
return attribute | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.