Skip to content

Commit

Permalink
Improve Digital adapter: Google additional consent support (#2202)
Browse files Browse the repository at this point in the history
* Improve Digital: Google additional consent support

Transform Google additional consent into format understood by the Improve Digital ad server

* Google additional GDPR consent PR fixes

* Google additional GDPR consent PR fixes

* - return nil instead of []byte and error
- handle error for getAdditionalConsentProvidersUserExt method

* rename error variable

Co-authored-by: Samiul Amin Shanto <93644987+samiul-shanto@users.noreply.github.com>
  • Loading branch information
jbartek25 and samiul-shanto authored Apr 13, 2022
1 parent f78284f commit 74d2199
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
78 changes: 78 additions & 0 deletions adapters/improvedigital/improvedigital.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/mxmCherry/openrtb/v15/openrtb2"
"github.com/prebid/prebid-server/adapters"
Expand Down Expand Up @@ -36,6 +37,18 @@ func (a *ImprovedigitalAdapter) MakeRequests(request *openrtb2.BidRequest, reqIn

func (a *ImprovedigitalAdapter) makeRequest(request openrtb2.BidRequest, imp openrtb2.Imp) (*adapters.RequestData, error) {
request.Imp = []openrtb2.Imp{imp}

userExtAddtlConsent, err := a.getAdditionalConsentProvidersUserExt(request)
if err != nil {
return nil, err
}

if len(userExtAddtlConsent) > 0 {
userCopy := *request.User
userCopy.Ext = userExtAddtlConsent
request.User = &userCopy
}

reqJSON, err := json.Marshal(request)
if err != nil {
return nil, err
Expand Down Expand Up @@ -144,3 +157,68 @@ func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType,
Message: fmt.Sprintf("Failed to find impression for ID: \"%s\"", impID),
}
}

// This method responsible to clone request and convert additional consent providers string to array when additional consent provider found
func (a *ImprovedigitalAdapter) getAdditionalConsentProvidersUserExt(request openrtb2.BidRequest) ([]byte, error) {
const (
consentProvidersSettingsInputKey = "ConsentedProvidersSettings"
consentProvidersSettingsOutKey = "consented_providers_settings"
consentedProvidersKey = "consented_providers"
)

var cpStr string

// If user/user.ext not defined, no need to parse additional consent
if request.User == nil || request.User.Ext == nil {
return nil, nil
}

// Start validating additional consent
// Check key exist user.ext.ConsentedProvidersSettings
var userExtMap = make(map[string]json.RawMessage)
if err := json.Unmarshal(request.User.Ext, &userExtMap); err != nil {
return nil, err
}

cpsMapValue, cpsJSONFound := userExtMap[consentProvidersSettingsInputKey]
if !cpsJSONFound {
return nil, nil
}

// Check key exist user.ext.ConsentedProvidersSettings.consented_providers
var cpMap = make(map[string]json.RawMessage)
if err := json.Unmarshal(cpsMapValue, &cpMap); err != nil {
return nil, err
}

cpMapValue, cpJSONFound := cpMap[consentedProvidersKey]
if !cpJSONFound {
return nil, nil
}
// End validating additional consent

// Check if string contain ~, then substring after ~ to end of string
consentStr := string(cpMapValue)
var tildaPosition int
if tildaPosition = strings.Index(consentStr, "~"); tildaPosition == -1 {
return nil, nil
}
cpStr = consentStr[tildaPosition+1 : len(consentStr)-1]

// Prepare consent providers string
cpStr = fmt.Sprintf("[%s]", strings.Replace(cpStr, ".", ",", -1))
cpMap[consentedProvidersKey] = json.RawMessage(cpStr)

cpJSON, err := json.Marshal(cpMap)
if err != nil {
return nil, err
}
userExtMap[consentProvidersSettingsOutKey] = cpJSON

extJson, err := json.Marshal(userExtMap)
if err != nil {
return nil, err
}

return extJson, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"mockBidRequest": {
"id": "addtl-consent-request-id",
"site": {
"page": "https://good.site/url"
},
"imp": [{
"id": "test-imp-id",
"banner": {
"format": [{
"w": 300,
"h": 250
}]
},
"ext": {
"bidder": {
"placementId": 13245
}
}
}],
"user": {
"ext":{"consent":"ABC","ConsentedProvidersSettings":{"consented_providers":"1~10.20.90"}}
}
},

"httpCalls": [{
"expectedRequest": {
"uri": "http://localhost/pbs",
"body": {
"id": "addtl-consent-request-id",
"site": {
"page": "https://good.site/url"
},
"imp": [{
"id": "test-imp-id",
"banner": {
"format": [{
"w": 300,
"h": 250
}]
},
"ext": {
"bidder": {
"placementId": 13245
}
}
}],
"user": {
"ext": {"consent": "ABC","ConsentedProvidersSettings":{"consented_providers":"1~10.20.90"},"consented_providers_settings": {"consented_providers": [10,20,90]}
}
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "addtl-consent-request-id",
"seatbid": [{
"seat": "improvedigital",
"bid": [{
"id": "randomid",
"impid": "test-imp-id",
"price": 0.500000,
"adid": "12345678",
"adm": "some-test-ad",
"cid": "987",
"crid": "12345678",
"h": 250,
"w": 300
}]
}],
"cur": "USD"
}
}
}],

"expectedBidResponses": [{
"currency": "USD",
"bids": [{
"bid": {
"id": "randomid",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-test-ad",
"adid": "12345678",
"cid": "987",
"crid": "12345678",
"w": 300,
"h": 250
},
"type": "banner"
}]
}]
}

0 comments on commit 74d2199

Please sign in to comment.