github.com/prebid/prebid-server/v2@v2.18.0/macros/provider.go (about)

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