github.com/prebid/prebid-server/v2@v2.18.0/ortb/default.go (about) 1 package ortb 2 3 import ( 4 "github.com/prebid/prebid-server/v2/openrtb_ext" 5 "github.com/prebid/prebid-server/v2/util/ptrutil" 6 ) 7 8 const ( 9 DefaultPriceGranularityPrecision = 2 10 DefaultTargetingIncludeWinners = true 11 DefaultTargetingIncludeBidderKeys = true 12 DefaultSecure = int8(1) 13 ) 14 15 func SetDefaults(r *openrtb_ext.RequestWrapper) error { 16 requestExt, err := r.GetRequestExt() 17 if err != nil { 18 return err 19 } 20 21 requestExtPrebid := requestExt.GetPrebid() 22 if requestExtPrebid != nil { 23 modified := setDefaultsTargeting(requestExtPrebid.Targeting) 24 25 if modified { 26 requestExt.SetPrebid(requestExtPrebid) 27 } 28 } 29 30 imps := r.GetImp() 31 if len(imps) > 0 { 32 modified := setDefaultsImp(imps) 33 34 if modified { 35 r.SetImp(imps) 36 } 37 } 38 39 return nil 40 } 41 42 func setDefaultsTargeting(targeting *openrtb_ext.ExtRequestTargeting) bool { 43 if targeting == nil { 44 return false 45 } 46 47 modified := false 48 49 if newPG, updated := setDefaultsPriceGranularity(targeting.PriceGranularity); updated { 50 modified = true 51 targeting.PriceGranularity = newPG 52 } 53 54 // If price granularity is not specified in request then default one should be set. 55 // Default price granularity can be overwritten for video, banner or native bid type 56 // only in case targeting.MediaTypePriceGranularity.Video|Banner|Native != nil. 57 58 if targeting.MediaTypePriceGranularity.Video != nil { 59 if newVideoPG, updated := setDefaultsPriceGranularity(targeting.MediaTypePriceGranularity.Video); updated { 60 modified = true 61 targeting.MediaTypePriceGranularity.Video = newVideoPG 62 } 63 } 64 if targeting.MediaTypePriceGranularity.Banner != nil { 65 if newBannerPG, updated := setDefaultsPriceGranularity(targeting.MediaTypePriceGranularity.Banner); updated { 66 modified = true 67 targeting.MediaTypePriceGranularity.Banner = newBannerPG 68 } 69 } 70 if targeting.MediaTypePriceGranularity.Native != nil { 71 if newNativePG, updated := setDefaultsPriceGranularity(targeting.MediaTypePriceGranularity.Native); updated { 72 modified = true 73 targeting.MediaTypePriceGranularity.Native = newNativePG 74 } 75 } 76 77 if targeting.IncludeWinners == nil { 78 targeting.IncludeWinners = ptrutil.ToPtr(DefaultTargetingIncludeWinners) 79 modified = true 80 } 81 82 if targeting.IncludeBidderKeys == nil { 83 targeting.IncludeBidderKeys = ptrutil.ToPtr(DefaultTargetingIncludeBidderKeys) 84 modified = true 85 } 86 87 return modified 88 } 89 90 func setDefaultsPriceGranularity(pg *openrtb_ext.PriceGranularity) (*openrtb_ext.PriceGranularity, bool) { 91 if pg == nil || len(pg.Ranges) == 0 { 92 pg = ptrutil.ToPtr(openrtb_ext.NewPriceGranularityDefault()) 93 return pg, true 94 } 95 96 modified := false 97 98 if pg.Precision == nil { 99 pg.Precision = ptrutil.ToPtr(DefaultPriceGranularityPrecision) 100 modified = true 101 } 102 103 if setDefaultsPriceGranularityRange(pg.Ranges) { 104 modified = true 105 } 106 107 return pg, modified 108 } 109 110 func setDefaultsPriceGranularityRange(ranges []openrtb_ext.GranularityRange) bool { 111 modified := false 112 113 var prevMax float64 = 0 114 for i, r := range ranges { 115 if ranges[i].Min != prevMax { 116 ranges[i].Min = prevMax 117 modified = true 118 } 119 prevMax = r.Max 120 } 121 122 return modified 123 } 124 125 func setDefaultsImp(imps []*openrtb_ext.ImpWrapper) bool { 126 modified := false 127 128 for _, i := range imps { 129 if i != nil && i.Imp != nil && i.Secure == nil { 130 i.Secure = ptrutil.ToPtr(DefaultSecure) 131 modified = true 132 } 133 } 134 135 return modified 136 }