Skip to content

Commit

Permalink
added type ad to the imp[].ext.storedrequest.id when is HB Group ID (p…
Browse files Browse the repository at this point in the history
…rebid#237)

* added type ad to the imp[].ext.storedrequest.id when is HB Group ID

* Optimization function AddTypeAdToHbGroupId

* Optimization function AddTypeAdToHbGroupId - 2

---------

Co-authored-by: mifanich <[email protected]>
  • Loading branch information
mhlm and mifanich authored Mar 27, 2023
1 parent 6689ad3 commit f7785f0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 28 deletions.
36 changes: 8 additions & 28 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -1688,41 +1688,21 @@ func (deps *endpointDeps) processStoredRequests(ctx context.Context, requestJson
// Fetch the Stored Request data
var storedReqIds []string
if hasStoredBidRequest {
if isHbGroupID, err := regexp.MatchString(`^g[1-9]\d*;.+`, storedBidRequestId); err == nil && isHbGroupID {
typeAd := "banner"
var valueBid []byte
valueBid, _, _, _ = jsonparser.Get(requestJson, "imp", "[0]", "banner")
if len(valueBid) == 0 {
valueBid, _, _, _ = jsonparser.Get(requestJson, "imp", "[0]", "video")
if len(valueBid) > 0 {
typeAd = "video"
} else {
valueBid, _, _, _ = jsonparser.Get(requestJson, "imp", "[0]", "native")
if len(valueBid) > 0 {
typeAd = "native"
} else {
valueBid, _, _, _ = jsonparser.Get(requestJson, "imp", "[0]", "audio")
if len(valueBid) > 0 {
typeAd = "audio"
}
}
}
}

storedBidRequestId = fmt.Sprintf(
"%s;%s",
storedBidRequestId,
typeAd,
)
}

storedBidRequestId = nextmillennium.AddTypeAdToHbGroupId(storedBidRequestId, &requestJson, "imp", "[0]")
storedReqIds = []string{storedBidRequestId}
}

impStoredReqIds := make([]string, 0, len(impInfo))
impStoredReqIdsUniqueTracker := make(map[string]struct{}, len(impInfo))
for _, impData := range impInfo {
if impData.ImpExtPrebid.StoredRequest != nil && len(impData.ImpExtPrebid.StoredRequest.ID) > 0 {
impByte := []byte(impData.Imp)
impData.ImpExtPrebid.StoredRequest.ID = nextmillennium.AddTypeAdToHbGroupId(
impData.ImpExtPrebid.StoredRequest.ID,
&impByte,
[]string{}...,
)

storedImpId := impData.ImpExtPrebid.StoredRequest.ID
if _, present := impStoredReqIdsUniqueTracker[storedImpId]; !present {
impStoredReqIds = append(impStoredReqIds, storedImpId)
Expand Down
41 changes: 41 additions & 0 deletions nextmillennium/hb_group_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nextmillennium

import (
"fmt"
"regexp"

"github.com/buger/jsonparser"
)

func AddTypeAdToHbGroupId(storedRequestID string, data *[]byte, keys ...string) string {
if isHbGroupID, err := regexp.MatchString(`^g[1-9]\d*;.+`, storedRequestID); err == nil && isHbGroupID {
impData, _, _, _ := jsonparser.Get(*data, keys...)
typeAd := "banner"
var valueBid []byte
valueBid, _, _, _ = jsonparser.Get(impData, "banner")
if len(valueBid) == 0 {
valueBid, _, _, _ = jsonparser.Get(impData, "video")
if len(valueBid) > 0 {
typeAd = "video"
} else {
valueBid, _, _, _ = jsonparser.Get(impData, "native")
if len(valueBid) > 0 {
typeAd = "native"
} else {
valueBid, _, _, _ = jsonparser.Get(impData, "audio")
if len(valueBid) > 0 {
typeAd = "audio"
}
}
}
}

storedRequestID = fmt.Sprintf(
"%s;%s",
storedRequestID,
typeAd,
)
}

return storedRequestID
}
68 changes: 68 additions & 0 deletions nextmillennium/hb_group_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package nextmillennium

import (
"encoding/json"
"testing"
)

type TestData struct {
StoredRequestID string
Data []byte
Keys []string
Expected string
}

func TestAddTypeAdToHbGroupId(t *testing.T) {
testData := []TestData{
{
StoredRequestID: "test1",
Expected: "test1",
},

{
StoredRequestID: "g1234;test2",
Data: json.RawMessage(`{"id": "e7cba931-80fb-4366-abd1-7eeaeffd2f39","imp": [{"banner": {"w": 728,"h": 409}}],"device": {"w": 1,"h": 1}}`),
Keys: []string{"imp", "[0]"},
Expected: "g1234;test2;banner",
},

{
StoredRequestID: "g1234;test3",
Data: json.RawMessage(`{"video": {"w": 728,"h": 409}}`),
Keys: []string{},
Expected: "g1234;test3;video",
},

{
StoredRequestID: "g1234;test4",
Data: json.RawMessage(`{"native": {"w": 728,"h": 409}}`),
Keys: []string{},
Expected: "g1234;test4;native",
},

{
StoredRequestID: "g1234;test5",
Data: json.RawMessage(`{"audio": {"duration": 123}}`),
Keys: []string{},
Expected: "g1234;test5;audio",
},

{
StoredRequestID: "g1234;test6",
Data: json.RawMessage(`{"id": "e7cba931-80fb-4366-abd1-7eeaeffd2f39","imp": [],"device": {"w": 1,"h": 1}}`),
Keys: []string{"imp", "[0]"},
Expected: "g1234;test6;banner",
},
}

for _, data := range testData {
result := AddTypeAdToHbGroupId(data.StoredRequestID, &data.Data, data.Keys...)
if data.Expected != result {
t.Error(
"For: ", data.StoredRequestID,
"Expected: ", data.Expected,
"Got: ", result,
)
}
}
}

0 comments on commit f7785f0

Please sign in to comment.