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.
Initial implementation - cache wurl, cache vast rewrite, bid.ext.preb…
…id.events Missing timestamp (waiting for prebid#1584)
- Loading branch information
Showing
13 changed files
with
693 additions
and
48 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,89 @@ | ||
package exchange | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
jsonpatch "github.com/evanphx/json-patch" | ||
"github.com/mxmCherry/openrtb" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/analytics" | ||
"github.com/prebid/prebid-server/config" | ||
"github.com/prebid/prebid-server/endpoints/events" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
"github.com/prebid/prebid-server/pbsmetrics" | ||
) | ||
|
||
// eventsData has configuration fields needed for adding event tracking to an auction response | ||
type eventsData struct { | ||
accountID string | ||
enabledForAccount bool | ||
enabledForRequest bool | ||
auctionTimestampMs int64 | ||
integration pbsmetrics.DemandSource // web app amp | ||
bidderInfos adapters.BidderInfos | ||
externalURL string | ||
} | ||
|
||
// getExtEventsData creates an eventsData object from the different configuration sources | ||
func getExtEventsData(requestExtPrebid *openrtb_ext.ExtRequestPrebid, ts time.Time, account *config.Account, bidderInfos adapters.BidderInfos, externalURL string) *eventsData { | ||
return &eventsData{ | ||
accountID: account.ID, | ||
enabledForAccount: account.EventsEnabled, | ||
enabledForRequest: requestExtPrebid != nil && requestExtPrebid.Events != nil, // || account.analytics.auction-events.<web|app|amp> | ||
auctionTimestampMs: ts.UnixNano() / 1e+6, | ||
integration: "", // FIXME | ||
bidderInfos: bidderInfos, | ||
externalURL: externalURL, | ||
} | ||
} | ||
|
||
// isModifyingVASTXMLAllowed returns true if this bidder config allows modifying VAST XML for event tracking | ||
func (ev *eventsData) isModifyingVASTXMLAllowed(bidderName string) bool { | ||
return ev.bidderInfos[bidderName].ModifyingVastXmlAllowed | ||
} | ||
|
||
// modifyVAST injects event Impression url if needed, otherwise returns original VAST string | ||
func (ev *eventsData) modifyVAST(bid *openrtb.Bid, bidderName openrtb_ext.BidderName, vastXML string) string { | ||
if ev.isModifyingVASTXMLAllowed(bidderName.String()) { | ||
vastXML = string(events.ModifyVastXml(ev.externalURL, json.RawMessage(vastXML), bid.ID, bidderName.String(), ev.accountID, ev.auctionTimestampMs)) | ||
} | ||
return vastXML | ||
} | ||
|
||
// modifyBidJSON injects "wurl" (win) event url if needed, otherwise returns original json | ||
func (ev *eventsData) modifyBidJSON(bid *openrtb.Bid, bidderName openrtb_ext.BidderName, jsonBytes []byte) []byte { | ||
if !ev.enabledForAccount && !ev.enabledForRequest { | ||
return jsonBytes | ||
} | ||
// wurl attribute is not in the schema, so we have to patch | ||
if patch, err := json.Marshal(map[string]string{"wurl": ev.makeEventURL(analytics.Win, bid, bidderName)}); err == nil { | ||
if modifiedJSON, err := jsonpatch.MergePatch(jsonBytes, patch); err == nil { | ||
jsonBytes = modifiedJSON | ||
} | ||
} | ||
return jsonBytes | ||
} | ||
|
||
// makeBidExtEvents make the data for bid.ext.prebid.events if needed, otherwise returns nil | ||
func (ev *eventsData) makeBidExtEvents(bid *openrtb.Bid, bidderName openrtb_ext.BidderName) *openrtb_ext.ExtBidPrebidEvents { | ||
if !ev.enabledForAccount && !ev.enabledForRequest { | ||
return nil | ||
} | ||
return &openrtb_ext.ExtBidPrebidEvents{ | ||
Win: ev.makeEventURL(analytics.Win, bid, bidderName), | ||
Imp: ev.makeEventURL(analytics.Imp, bid, bidderName), | ||
} | ||
} | ||
|
||
// makeEventURL returns an analytics event url for the requested type (win or imp) | ||
func (ev *eventsData) makeEventURL(evType analytics.EventType, bid *openrtb.Bid, bidderName openrtb_ext.BidderName) string { | ||
return events.EventRequestToUrl(ev.externalURL, | ||
&analytics.EventRequest{ | ||
Type: evType, | ||
BidID: bid.ID, | ||
Bidder: string(bidderName), | ||
AccountID: ev.accountID, | ||
Timestamp: ev.auctionTimestampMs, | ||
}) | ||
} |
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,133 @@ | ||
package exchange | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_eventsData_makeBidExtEvents(t *testing.T) { | ||
type args struct { | ||
enabledForAccount bool | ||
enabledForRequest bool | ||
bid *openrtb.Bid | ||
bidderName openrtb_ext.BidderName | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *openrtb_ext.ExtBidPrebidEvents | ||
}{ | ||
{ | ||
name: "Events enabled for request, disabled for account", | ||
args: args{ | ||
enabledForAccount: false, | ||
enabledForRequest: true, | ||
bid: &openrtb.Bid{ID: "BID-1"}, | ||
bidderName: openrtb_ext.BidderOpenx, | ||
}, | ||
want: &openrtb_ext.ExtBidPrebidEvents{ | ||
Win: "http://localhost/event?t=win&b=BID-1&a=123456&bidder=openx&ts=1234567890", | ||
Imp: "http://localhost/event?t=imp&b=BID-1&a=123456&bidder=openx&ts=1234567890", | ||
}, | ||
}, | ||
{ | ||
name: "Events enabled for account, disabled for request", | ||
args: args{ | ||
enabledForAccount: false, | ||
enabledForRequest: true, | ||
bid: &openrtb.Bid{ID: "BID-1"}, | ||
bidderName: openrtb_ext.BidderOpenx, | ||
}, | ||
want: &openrtb_ext.ExtBidPrebidEvents{ | ||
Win: "http://localhost/event?t=win&b=BID-1&a=123456&bidder=openx&ts=1234567890", | ||
Imp: "http://localhost/event?t=imp&b=BID-1&a=123456&bidder=openx&ts=1234567890", | ||
}, | ||
}, | ||
{ | ||
name: "Events disabled for account and request", | ||
args: args{ | ||
enabledForAccount: false, | ||
enabledForRequest: false, | ||
bid: &openrtb.Bid{ID: "BID-1"}, | ||
bidderName: openrtb_ext.BidderOpenx, | ||
}, | ||
want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
evData := &eventsData{ | ||
enabledForAccount: tt.args.enabledForAccount, | ||
enabledForRequest: tt.args.enabledForRequest, | ||
accountID: "123456", | ||
auctionTimestampMs: 1234567890, | ||
externalURL: "http://localhost", | ||
} | ||
assert.Equal(t, tt.want, evData.makeBidExtEvents(tt.args.bid, tt.args.bidderName)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_eventsData_modifyBidJSON(t *testing.T) { | ||
type args struct { | ||
enabledForAccount bool | ||
enabledForRequest bool | ||
bid *openrtb.Bid | ||
bidderName openrtb_ext.BidderName | ||
jsonBytes []byte | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []byte | ||
}{ | ||
{ | ||
name: "Events enabled for request, disabled for account", | ||
args: args{ | ||
enabledForAccount: false, | ||
enabledForRequest: true, | ||
bid: &openrtb.Bid{ID: "BID-1"}, | ||
bidderName: openrtb_ext.BidderOpenx, | ||
jsonBytes: []byte(`{"ID": "something"}`), | ||
}, | ||
want: []byte(`{"ID": "something", "wurl": "http://localhost/event?t=win&b=BID-1&a=123456&bidder=openx&ts=1234567890"}`), | ||
}, | ||
{ | ||
name: "Events enabled for account, disabled for request", | ||
args: args{ | ||
enabledForAccount: false, | ||
enabledForRequest: true, | ||
bid: &openrtb.Bid{ID: "BID-1"}, | ||
bidderName: openrtb_ext.BidderOpenx, | ||
jsonBytes: []byte(`{"ID": "something"}`), | ||
}, | ||
want: []byte(`{"ID": "something", "wurl": "http://localhost/event?t=win&b=BID-1&a=123456&bidder=openx&ts=1234567890"}`), | ||
}, | ||
{ | ||
name: "Events disabled for account and request", | ||
args: args{ | ||
enabledForAccount: false, | ||
enabledForRequest: false, | ||
bid: &openrtb.Bid{ID: "BID-1"}, | ||
bidderName: openrtb_ext.BidderOpenx, | ||
jsonBytes: []byte(`{"ID": "something"}`), | ||
}, | ||
want: []byte(`{"ID": "something"}`), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
evData := &eventsData{ | ||
enabledForAccount: tt.args.enabledForAccount, | ||
enabledForRequest: tt.args.enabledForRequest, | ||
accountID: "123456", | ||
auctionTimestampMs: 1234567890, | ||
externalURL: "http://localhost", | ||
} | ||
assert.JSONEq(t, string(tt.want), string(evData.modifyBidJSON(tt.args.bid, tt.args.bidderName, tt.args.jsonBytes))) | ||
}) | ||
} | ||
} |
Oops, something went wrong.