diff --git a/adapters/pubmatic/pubmatic.go b/adapters/pubmatic/pubmatic.go index 9876041ec4e..6d5241aad18 100644 --- a/adapters/pubmatic/pubmatic.go +++ b/adapters/pubmatic/pubmatic.go @@ -450,6 +450,12 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externa BidVideo: &openrtb_ext.ExtBidPrebidVideo{}, } + if dchain, _, _, err := jsonparser.Get(bid.Ext, "dchain"); err == nil && len(dchain) > 0 { + typedBid.BidMeta = &openrtb_ext.ExtBidPrebidMeta{ + DChain: dchain, + } + } + var bidExt *pubmaticBidExt err := json.Unmarshal(bid.Ext, &bidExt) if err != nil { diff --git a/dchain/dchain.go b/dchain/dchain.go new file mode 100644 index 00000000000..03b608e203e --- /dev/null +++ b/dchain/dchain.go @@ -0,0 +1,59 @@ +package dchain + +import ( + "encoding/json" + "strconv" + + "github.com/prebid/prebid-server/v2/openrtb_ext" +) + +type Dchain struct { + Ver string `json:"ver,omitempty"` + Complete int `json:"complete,omitempty"` + Nodes []DchainNodes `json:"nodes,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` +} + +type DchainNodes struct { + Asi string `json:"asi,omitempty"` + Bsid string `json:"bsid,omitempty"` + Rid string `json:"rid,omitempty"` + Name string `json:"name,omitempty"` + Domain string `json:"domain,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` +} + +func IsValidDchain(dchain Dchain) bool { + if dchain.Complete != 0 && dchain.Complete != 1 { + return false + } + if dchain.Nodes == nil || len(dchain.Nodes) == 0 { + return false + } + return true +} + +func AddDchainNode(prebidMeta *openrtb_ext.ExtBidPrebidMeta) { + var dchain Dchain + if err := json.Unmarshal(prebidMeta.DChain, &dchain); err == nil && IsValidDchain(dchain) { + dchain.Nodes = append(dchain.Nodes, + DchainNodes{Asi: prebidMeta.AdapterCode}, + ) + prebidMeta.DChain, _ = json.Marshal(dchain) + return + } + basicDchain := Dchain{ + Ver: "1.0", + Complete: 0, + Nodes: []DchainNodes{}, + } + + if prebidMeta.NetworkID != 0 && prebidMeta.NetworkName != "" { + basicDchain.Nodes = append(basicDchain.Nodes, + DchainNodes{Name: prebidMeta.NetworkName, Bsid: strconv.Itoa(prebidMeta.NetworkID)}, + ) + } + + basicDchain.Nodes = append(basicDchain.Nodes, DchainNodes{Name: prebidMeta.AdapterCode}) + prebidMeta.DChain, _ = json.Marshal(basicDchain) +} diff --git a/exchange/exchange.go b/exchange/exchange.go index 41134104f37..402a47546c7 100644 --- a/exchange/exchange.go +++ b/exchange/exchange.go @@ -14,6 +14,7 @@ import ( "strings" "time" + "github.com/prebid/prebid-server/v2/dchain" "github.com/prebid/prebid-server/v2/privacy" "github.com/prebid/prebid-server/v2/adapters" @@ -1347,6 +1348,10 @@ func makeBidExtJSON(ext json.RawMessage, prebid *openrtb_ext.ExtBidPrebid, impEx prebid.Meta.AdapterCode = adapter.String() + if prebid.Meta.DChain != nil { + dchain.AddDchainNode(prebid.Meta) + } + // ext.prebid.storedrequestattributes and ext.prebid.passthrough if impExtInfo, ok := impExtInfoMap[impId]; ok { prebid.Passthrough = impExtInfoMap[impId].Passthrough