-
Notifications
You must be signed in to change notification settings - Fork 715
/
provider.go
145 lines (124 loc) · 4.01 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package macros
import (
"net/url"
"strconv"
"time"
"github.com/prebid/prebid-server/v2/exchange/entities"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)
const (
MacroKeyBidID = "PBS-BIDID"
MacroKeyAppBundle = "PBS-APPBUNDLE"
MacroKeyDomain = "PBS-DOMAIN"
MacroKeyPubDomain = "PBS-PUBDOMAIN"
MacroKeyPageURL = "PBS-PAGEURL"
MacroKeyAccountID = "PBS-ACCOUNTID"
MacroKeyLmtTracking = "PBS-LIMITADTRACKING"
MacroKeyConsent = "PBS-GDPRCONSENT"
MacroKeyBidder = "PBS-BIDDER"
MacroKeyIntegration = "PBS-INTEGRATION"
MacroKeyVastCRTID = "PBS-VASTCRTID"
MacroKeyTimestamp = "PBS-TIMESTAMP"
MacroKeyAuctionID = "PBS-AUCTIONID"
MacroKeyChannel = "PBS-CHANNEL"
MacroKeyEventType = "PBS-EVENTTYPE"
MacroKeyVastEvent = "PBS-VASTEVENT"
)
const (
customMacroLength = 100
CustomMacroPrefix = "PBS-MACRO-"
)
type macroProvider struct {
// macros map stores macros key values
macros map[string]string
}
// NewBuilder returns the instance of macro buidler
func NewProvider(reqWrapper *openrtb_ext.RequestWrapper) *macroProvider {
macroProvider := ¯oProvider{macros: map[string]string{}}
macroProvider.populateRequestMacros(reqWrapper)
return macroProvider
}
func (b *macroProvider) populateRequestMacros(reqWrapper *openrtb_ext.RequestWrapper) {
b.macros[MacroKeyTimestamp] = strconv.Itoa(int(time.Now().Unix()))
reqExt, err := reqWrapper.GetRequestExt()
if err == nil && reqExt != nil {
if reqPrebidExt := reqExt.GetPrebid(); reqPrebidExt != nil {
for key, value := range reqPrebidExt.Macros {
customMacroKey := CustomMacroPrefix + key // Adding prefix PBS-MACRO to custom macro keys
b.macros[customMacroKey] = truncate(value, customMacroLength) // limit the custom macro value to 100 chars only
}
if reqPrebidExt.Integration != "" {
b.macros[MacroKeyIntegration] = reqPrebidExt.Integration
}
if reqPrebidExt.Channel != nil {
b.macros[MacroKeyChannel] = reqPrebidExt.Channel.Name
}
}
}
b.macros[MacroKeyAuctionID] = reqWrapper.ID
if reqWrapper.App != nil {
if reqWrapper.App.Bundle != "" {
b.macros[MacroKeyAppBundle] = reqWrapper.App.Bundle
}
if reqWrapper.App.Domain != "" {
b.macros[MacroKeyDomain] = reqWrapper.App.Domain
}
if reqWrapper.App.Publisher != nil {
if reqWrapper.App.Publisher.Domain != "" {
b.macros[MacroKeyPubDomain] = reqWrapper.App.Publisher.Domain
}
if reqWrapper.App.Publisher.ID != "" {
b.macros[MacroKeyAccountID] = reqWrapper.App.Publisher.ID
}
}
}
if reqWrapper.Site != nil {
if reqWrapper.Site.Page != "" {
b.macros[MacroKeyPageURL] = reqWrapper.Site.Page
}
if reqWrapper.Site.Domain != "" {
b.macros[MacroKeyDomain] = reqWrapper.Site.Domain
}
if reqWrapper.Site.Publisher != nil {
if reqWrapper.Site.Publisher.Domain != "" {
b.macros[MacroKeyPubDomain] = reqWrapper.Site.Publisher.Domain
}
if reqWrapper.Site.Publisher.ID != "" {
b.macros[MacroKeyAccountID] = reqWrapper.Site.Publisher.ID
}
}
}
userExt, err := reqWrapper.GetUserExt()
if err == nil && userExt != nil && userExt.GetConsent() != nil {
b.macros[MacroKeyConsent] = *userExt.GetConsent()
}
if reqWrapper.Device != nil && reqWrapper.Device.Lmt != nil {
b.macros[MacroKeyLmtTracking] = strconv.Itoa(int(*reqWrapper.Device.Lmt))
}
}
func (b *macroProvider) GetMacro(key string) string {
return url.QueryEscape(b.macros[key])
}
func (b *macroProvider) PopulateBidMacros(bid *entities.PbsOrtbBid, seat string) {
if bid.Bid != nil {
if bid.GeneratedBidID != "" {
b.macros[MacroKeyBidID] = bid.GeneratedBidID
} else {
b.macros[MacroKeyBidID] = bid.Bid.ID
}
}
b.macros[MacroKeyBidder] = seat
}
func (b *macroProvider) PopulateEventMacros(vastCreativeID, eventType, vastEvent string) {
b.macros[MacroKeyVastCRTID] = vastCreativeID
b.macros[MacroKeyEventType] = eventType
b.macros[MacroKeyVastEvent] = vastEvent
}
func truncate(text string, width uint) string {
r := []rune(text)
if uint(len(r)) < (width) {
return text
}
trunc := r[:width]
return string(trunc)
}