Skip to content
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
12 changes: 12 additions & 0 deletions adapters/thetradedesk/thetradedesk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"text/template"

"github.com/prebid/prebid-server/v3/adapters"
Expand Down Expand Up @@ -176,6 +178,7 @@ func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest
for _, bid := range seatBid.Bid {
bid := bid

resolveAuctionPriceMacros(&bid)
bidType, err := getBidType(bid.MType)

if err != nil {
Expand Down Expand Up @@ -232,3 +235,12 @@ func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server co
templateEndpoint: template,
}, nil
}

func resolveAuctionPriceMacros(bid *openrtb2.Bid) {
if bid == nil {
return
}
price := strconv.FormatFloat(bid.Price, 'f', -1, 64)
bid.NURL = strings.Replace(bid.NURL, "${AUCTION_PRICE}", price, -1)
bid.AdM = strings.Replace(bid.AdM, "${AUCTION_PRICE}", price, -1)
}
Comment thread
andre-gielow-ttd marked this conversation as resolved.
144 changes: 144 additions & 0 deletions adapters/thetradedesk/thetradedesk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,147 @@ func TestTheTradeDeskAdapter_BuildEndpoint(t *testing.T) {
})
}
}

func TestResolveAuctionPriceMacros(t *testing.T) {
tests := []struct {
name string
bid *openrtb2.Bid
expectedNURL string
expectedAdM string
}{
{
name: "nil_bid",
bid: nil,
},
{
name: "no_macros",
bid: &openrtb2.Bid{
Price: 1.23,
NURL: "http://example.com/nurl",
AdM: "<div>Ad content</div>",
},
expectedNURL: "http://example.com/nurl",
expectedAdM: "<div>Ad content</div>",
},
{
name: "macros_in_both_nurl_and_adm",
bid: &openrtb2.Bid{
Price: 1.50,
NURL: "http://example.com/nurl?price=${AUCTION_PRICE}",
AdM: "<div>Ad content with price ${AUCTION_PRICE}</div>",
},
expectedNURL: "http://example.com/nurl?price=1.5",
expectedAdM: "<div>Ad content with price 1.5</div>",
},
{
name: "macro_only_in_nurl",
bid: &openrtb2.Bid{
Price: 2.75,
NURL: "http://example.com/nurl?price=${AUCTION_PRICE}",
AdM: "<div>Ad content</div>",
},
expectedNURL: "http://example.com/nurl?price=2.75",
expectedAdM: "<div>Ad content</div>",
},
{
name: "macro_only_in_adm",
bid: &openrtb2.Bid{
Price: 0.99,
NURL: "http://example.com/nurl",
AdM: "<div>Price: ${AUCTION_PRICE}</div>",
},
expectedNURL: "http://example.com/nurl",
expectedAdM: "<div>Price: 0.99</div>",
},
{
name: "multiple_macros_in_same_field",
bid: &openrtb2.Bid{
Price: 3.14,
NURL: "http://example.com/nurl?price=${AUCTION_PRICE}&backup_price=${AUCTION_PRICE}",
AdM: "<div>Price: ${AUCTION_PRICE}, Backup: ${AUCTION_PRICE}</div>",
},
expectedNURL: "http://example.com/nurl?price=3.14&backup_price=3.14",
expectedAdM: "<div>Price: 3.14, Backup: 3.14</div>",
},
{
name: "zero_price",
bid: &openrtb2.Bid{
Price: 0.0,
NURL: "http://example.com/nurl?price=${AUCTION_PRICE}",
AdM: "<div>Price: ${AUCTION_PRICE}</div>",
},
expectedNURL: "http://example.com/nurl?price=0",
expectedAdM: "<div>Price: 0</div>",
},
{
name: "very_small_price",
bid: &openrtb2.Bid{
Price: 0.001,
NURL: "http://example.com/nurl?price=${AUCTION_PRICE}",
AdM: "<div>Price: ${AUCTION_PRICE}</div>",
},
expectedNURL: "http://example.com/nurl?price=0.001",
expectedAdM: "<div>Price: 0.001</div>",
},
{
name: "large_price",
bid: &openrtb2.Bid{
Price: 999.999,
NURL: "http://example.com/nurl?price=${AUCTION_PRICE}",
AdM: "<div>Price: ${AUCTION_PRICE}</div>",
},
expectedNURL: "http://example.com/nurl?price=999.999",
expectedAdM: "<div>Price: 999.999</div>",
},
{
name: "integer_price",
bid: &openrtb2.Bid{
Price: 5.0,
NURL: "http://example.com/nurl?price=${AUCTION_PRICE}",
AdM: "<div>Price: ${AUCTION_PRICE}</div>",
},
expectedNURL: "http://example.com/nurl?price=5",
expectedAdM: "<div>Price: 5</div>",
},
{
name: "empty_nurl_and_adm",
bid: &openrtb2.Bid{
Price: 1.23,
NURL: "",
AdM: "",
},
expectedNURL: "",
expectedAdM: "",
},
{
name: "case_sensitive_macro_not_replaced",
bid: &openrtb2.Bid{
Price: 1.50,
NURL: "http://example.com/nurl?price=${auction_price}",
AdM: "<div>Price: ${Auction_Price}</div>",
},
expectedNURL: "http://example.com/nurl?price=${auction_price}",
expectedAdM: "<div>Price: ${Auction_Price}</div>",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var testBid *openrtb2.Bid
if tt.bid != nil {
bidCopy := *tt.bid
testBid = &bidCopy
}

resolveAuctionPriceMacros(testBid)

if tt.bid == nil {
assert.Nil(t, testBid)
return
}

assert.Equal(t, tt.expectedNURL, testBid.NURL, "NURL should match expected value")
assert.Equal(t, tt.expectedAdM, testBid.AdM, "AdM should match expected value")
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"mockBidRequest": {
"id": "test-request-id",
"site": {
"id": "site-id",
"page": "ttd.com",
"publisher": {
"id": "123456"
}
},
"device": {
"os": "android",
"ip": "91.199.242.236",
"ua": "random user agent"
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
],
"w": 100,
"h": 150
},
"ext": {
"bidder": {
"publisherId": "123456"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://direct.adsrvr.org/bid/bidder/ttd",
"headers": {
"Content-Type": [
"application/json;charset=utf-8"
],
"Accept": [
"application/json"
]
},
"body": {
"id": "test-request-id",
"site": {
"id": "site-id",
"page": "ttd.com",
"publisher": {
"id": "123456"
}
},
"device": {
"os": "android",
"ip": "91.199.242.236",
"ua": "random user agent"
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
],
"w": 300,
"h": 250
},
"ext": {
"bidder": {
"publisherId": "123456"
}
}
}
]
},
"impIDs":["test-imp-id"]
},
"mockResponse": {
"status": 200,
"body": {
"currency": "USD",
"seatbid": [
{
"bid": [
{
"id": "test-slot-id",
"impid": "test-imp-id",
"price": 1.50,
"crid": "creative-123",
"adm": "<iframe src='http://creative-url.ttd.com?price=${AUCTION_PRICE}'></iframe>",
"nurl": "http://win-notification.ttd.com?wp=${AUCTION_PRICE}&id=test-slot-id",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
},
"mtype": 1
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-slot-id",
"impid": "test-imp-id",
"price": 1.50,
"crid": "creative-123",
"adm": "<iframe src='http://creative-url.ttd.com?price=1.5'></iframe>",
"nurl": "http://win-notification.ttd.com?wp=1.5&id=test-slot-id",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
},
"mtype": 1
},
"type": "banner"
}
]
}
]
}
Loading