github.com/prebid/prebid-server@v0.275.0/openrtb_ext/bid.go (about)

     1  package openrtb_ext
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  )
     7  
     8  // ExtBid defines the contract for bidresponse.seatbid.bid[i].ext
     9  type ExtBid struct {
    10  	Prebid *ExtBidPrebid `json:"prebid,omitempty"`
    11  }
    12  
    13  // ExtBidPrebid defines the contract for bidresponse.seatbid.bid[i].ext.prebid
    14  // DealPriority represents priority of deal bid. If its non deal bid then value will be 0
    15  // DealTierSatisfied true represents corresponding bid has satisfied the deal tier
    16  type ExtBidPrebid struct {
    17  	Cache             *ExtBidPrebidCache  `json:"cache,omitempty"`
    18  	DealPriority      int                 `json:"dealpriority,omitempty"`
    19  	DealTierSatisfied bool                `json:"dealtiersatisfied,omitempty"`
    20  	Meta              *ExtBidPrebidMeta   `json:"meta,omitempty"`
    21  	Targeting         map[string]string   `json:"targeting,omitempty"`
    22  	TargetBidderCode  string              `json:"targetbiddercode,omitempty"`
    23  	Type              BidType             `json:"type,omitempty"`
    24  	Video             *ExtBidPrebidVideo  `json:"video,omitempty"`
    25  	Events            *ExtBidPrebidEvents `json:"events,omitempty"`
    26  	BidId             string              `json:"bidid,omitempty"`
    27  	Passthrough       json.RawMessage     `json:"passthrough,omitempty"`
    28  	Floors            *ExtBidPrebidFloors `json:"floors,omitempty"`
    29  }
    30  
    31  // ExtBidPrebidFloors defines the contract for bidresponse.seatbid.bid[i].ext.prebid.floors
    32  type ExtBidPrebidFloors struct {
    33  	FloorRule      string  `json:"floorRule,omitempty"`
    34  	FloorRuleValue float64 `json:"floorRuleValue,omitempty"`
    35  	FloorValue     float64 `json:"floorValue,omitempty"`
    36  	FloorCurrency  string  `json:"floorCurrency,omitempty"`
    37  }
    38  
    39  // ExtBidPrebidCache defines the contract for  bidresponse.seatbid.bid[i].ext.prebid.cache
    40  type ExtBidPrebidCache struct {
    41  	Key  string                 `json:"key"`
    42  	Url  string                 `json:"url"`
    43  	Bids *ExtBidPrebidCacheBids `json:"bids,omitempty"`
    44  }
    45  
    46  type ExtBidPrebidCacheBids struct {
    47  	Url     string `json:"url"`
    48  	CacheId string `json:"cacheId"`
    49  }
    50  
    51  // ExtBidPrebidMeta defines the contract for bidresponse.seatbid.bid[i].ext.prebid.meta
    52  type ExtBidPrebidMeta struct {
    53  	AdapterCode          string          `json:"adaptercode,omitempty"`
    54  	AdvertiserDomains    []string        `json:"advertiserDomains,omitempty"`
    55  	AdvertiserID         int             `json:"advertiserId,omitempty"`
    56  	AdvertiserName       string          `json:"advertiserName,omitempty"`
    57  	AgencyID             int             `json:"agencyId,omitempty"`
    58  	AgencyName           string          `json:"agencyName,omitempty"`
    59  	BrandID              int             `json:"brandId,omitempty"`
    60  	BrandName            string          `json:"brandName,omitempty"`
    61  	DChain               json.RawMessage `json:"dchain,omitempty"`
    62  	DemandSource         string          `json:"demandSource,omitempty"`
    63  	MediaType            string          `json:"mediaType,omitempty"`
    64  	NetworkID            int             `json:"networkId,omitempty"`
    65  	NetworkName          string          `json:"networkName,omitempty"`
    66  	PrimaryCategoryID    string          `json:"primaryCatId,omitempty"`
    67  	RendererName         string          `json:"rendererName,omitempty"`
    68  	RendererVersion      string          `json:"rendererVersion,omitempty"`
    69  	RendererData         json.RawMessage `json:"rendererData,omitempty"`
    70  	RendererUrl          string          `json:"rendererUrl,omitempty"`
    71  	SecondaryCategoryIDs []string        `json:"secondaryCatIds,omitempty"`
    72  }
    73  
    74  // ExtBidPrebidVideo defines the contract for bidresponse.seatbid.bid[i].ext.prebid.video
    75  type ExtBidPrebidVideo struct {
    76  	Duration        int    `json:"duration"`
    77  	PrimaryCategory string `json:"primary_category"`
    78  }
    79  
    80  // ExtBidPrebidEvents defines the contract for bidresponse.seatbid.bid[i].ext.prebid.events
    81  type ExtBidPrebidEvents struct {
    82  	Win string `json:"win,omitempty"`
    83  	Imp string `json:"imp,omitempty"`
    84  }
    85  
    86  // BidType describes the allowed values for bidresponse.seatbid.bid[i].ext.prebid.type
    87  type BidType string
    88  
    89  const (
    90  	BidTypeBanner BidType = "banner"
    91  	BidTypeVideo  BidType = "video"
    92  	BidTypeAudio  BidType = "audio"
    93  	BidTypeNative BidType = "native"
    94  )
    95  
    96  func BidTypes() []BidType {
    97  	return []BidType{
    98  		BidTypeBanner,
    99  		BidTypeVideo,
   100  		BidTypeAudio,
   101  		BidTypeNative,
   102  	}
   103  }
   104  
   105  func ParseBidType(bidType string) (BidType, error) {
   106  	switch bidType {
   107  	case "banner":
   108  		return BidTypeBanner, nil
   109  	case "video":
   110  		return BidTypeVideo, nil
   111  	case "audio":
   112  		return BidTypeAudio, nil
   113  	case "native":
   114  		return BidTypeNative, nil
   115  	default:
   116  		return "", fmt.Errorf("invalid BidType: %s", bidType)
   117  	}
   118  }
   119  
   120  // TargetingKeys are used throughout Prebid as keys which can be used in an ad server like DFP.
   121  // Clients set the values we assign on the request to the ad server, where they can be substituted like macros into
   122  // Creatives.
   123  //
   124  // Removing one of these, or changing the semantics of what we store there, will probably break the
   125  // line item setups for many publishers.
   126  //
   127  // These are especially important to Prebid Mobile. It's much more cumbersome for a Mobile App to update code
   128  // than it is for a website. As a result, they rely heavily on these targeting keys so that any changes can
   129  // be made on Prebid Server and the Ad Server's line items.
   130  type TargetingKey string
   131  
   132  const (
   133  	HbpbConstantKey TargetingKey = "hb_pb"
   134  
   135  	// HbEnvKey exists to support the Prebid Universal Creative. If it exists, the only legal value is mobile-app.
   136  	// It will exist only if the incoming bidRequest defined request.app instead of request.site.
   137  	HbEnvKey TargetingKey = "hb_env"
   138  
   139  	// HbCacheHost and HbCachePath exist to supply cache host and path as targeting parameters
   140  	HbConstantCacheHostKey TargetingKey = "hb_cache_host"
   141  	HbConstantCachePathKey TargetingKey = "hb_cache_path"
   142  
   143  	// HbBidderConstantKey is the name of the Bidder. For example, "appnexus" or "rubicon".
   144  	HbBidderConstantKey TargetingKey = "hb_bidder"
   145  	HbSizeConstantKey   TargetingKey = "hb_size"
   146  	HbDealIDConstantKey TargetingKey = "hb_deal"
   147  
   148  	// HbFormatKey is the format of the bid. For example, "video", "banner"
   149  	HbFormatKey TargetingKey = "hb_format"
   150  
   151  	// HbCacheKey and HbVastCacheKey store UUIDs which can be used to fetch things from prebid cache.
   152  	// Callers should *never* assume that either of these exist, since the call to the cache may always fail.
   153  	//
   154  	// HbVastCacheKey's UUID will fetch the entire bid JSON, while HbVastCacheKey will fetch just the VAST XML.
   155  	// HbVastCacheKey will only ever exist for Video bids.
   156  	HbCacheKey     TargetingKey = "hb_cache_id"
   157  	HbVastCacheKey TargetingKey = "hb_uuid"
   158  
   159  	// This is not a key, but values used by the HbEnvKey
   160  	HbEnvKeyApp string = "mobile-app"
   161  
   162  	HbCategoryDurationKey TargetingKey = "hb_pb_cat_dur"
   163  )
   164  
   165  func (key TargetingKey) BidderKey(bidder BidderName, maxLength int) string {
   166  	s := string(key) + "_" + string(bidder)
   167  	if maxLength != 0 {
   168  		return s[:min(len(s), maxLength)]
   169  	}
   170  	return s
   171  }
   172  
   173  func min(x, y int) int {
   174  	if x < y {
   175  		return x
   176  	}
   177  	return y
   178  }
   179  
   180  func (key TargetingKey) TruncateKey(maxLength int) string {
   181  	if maxLength > 0 {
   182  		return string(key)[:min(len(string(key)), maxLength)]
   183  	}
   184  	return string(key)
   185  }
   186  
   187  const (
   188  	StoredRequestAttributes = "storedrequestattributes"
   189  	OriginalBidCpmKey       = "origbidcpm"
   190  	OriginalBidCurKey       = "origbidcur"
   191  	Passthrough             = "passthrough"
   192  )