From bfcda5a4fdeb67ee53c9f9ce75c9acb8b89b6ca8 Mon Sep 17 00:00:00 2001 From: "dhruv.sonone" Date: Thu, 4 Jul 2024 15:26:58 +0530 Subject: [PATCH] Integrate event tracker with auction logic --- exchange/events.go | 53 +++++++++++++++++++++++++++++++++++++++++++- exchange/exchange.go | 2 +- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/exchange/events.go b/exchange/events.go index b20eea328f6..5aecb60f4d3 100644 --- a/exchange/events.go +++ b/exchange/events.go @@ -4,6 +4,8 @@ import ( "time" "github.com/prebid/prebid-server/v2/exchange/entities" + "github.com/prebid/prebid-server/v2/injector" + "github.com/prebid/prebid-server/v2/macros" jsonpatch "gopkg.in/evanphx/json-patch.v4" "github.com/prebid/prebid-server/v2/analytics" @@ -22,10 +24,12 @@ type eventTracking struct { integrationType string bidderInfos config.BidderInfos externalURL string + events injector.VASTEvents + macroProvider *macros.MacroProvider } // getEventTracking creates an eventTracking object from the different configuration sources -func getEventTracking(requestExtPrebid *openrtb_ext.ExtRequestPrebid, ts time.Time, account *config.Account, bidderInfos config.BidderInfos, externalURL string) *eventTracking { +func getEventTracking(requestExtPrebid *openrtb_ext.ExtRequestPrebid, ts time.Time, account *config.Account, bidderInfos config.BidderInfos, externalURL string, mp *macros.MacroProvider) *eventTracking { return &eventTracking{ accountID: account.ID, enabledForAccount: account.Events.Enabled, @@ -34,6 +38,8 @@ func getEventTracking(requestExtPrebid *openrtb_ext.ExtRequestPrebid, ts time.Ti integrationType: getIntegrationType(requestExtPrebid), bidderInfos: bidderInfos, externalURL: externalURL, + macroProvider: mp, + events: convertToVastEvent(account.Events), } } @@ -77,6 +83,8 @@ func (ev *eventTracking) modifyBidVAST(pbsBid *entities.PbsOrtbBid, bidderName o if newVastXML, ok := events.ModifyVastXmlString(ev.externalURL, vastXML, bidID, bidderName.String(), ev.accountID, ev.auctionTimestampMs, ev.integrationType); ok { bid.AdM = newVastXML } + + ev.InjectTrackers(pbsBid, bidderName) } // modifyBidJSON injects "wurl" (win) event url if needed, otherwise returns original json @@ -134,3 +142,46 @@ func (ev *eventTracking) makeEventURL(evType analytics.EventType, pbsBid *entiti func (ev *eventTracking) isEventAllowed() bool { return ev.enabledForAccount || ev.enabledForRequest } + +func (ev *eventTracking) InjectTrackers(pbsBid *entities.PbsOrtbBid, bidderName openrtb_ext.BidderName) { + ev.macroProvider.PopulateBidMacros(pbsBid, bidderName.String()) + ti := injector.NewTrackerInjector( + macros.NewStringIndexBasedReplacer(), + ev.macroProvider, + ev.events, + ) + + if adm, err := ti.InjectTracker(pbsBid.Bid.AdM, pbsBid.Bid.NURL); err == nil { + pbsBid.Bid.AdM = adm + } +} + +func convertToVastEvent(events config.Events) injector.VASTEvents { + ve := injector.VASTEvents{ + TrackingEvents: make(map[string][]string), + } + + for _, event := range events.VASTEvents { + switch event.CreateElement { + case config.ImpressionVASTElement: + ve.Impressions = appendURLs(ve.Impressions, event, events.DefaultURL) + case config.ErrorVASTElement: + ve.Errors = appendURLs(ve.Errors, event, events.DefaultURL) + case config.TrackingVASTElement: + ve.TrackingEvents[string(event.Type)] = appendURLs(ve.TrackingEvents[string(event.Type)], event, events.DefaultURL) + case config.ClickTrackingVASTElement: + ve.VideoClicks = appendURLs(ve.VideoClicks, event, events.DefaultURL) + } + } + + return ve +} + +// appendURLs appends event URLs to the provided slice and, if not excluded, the default URL. +func appendURLs(urls []string, event config.VASTEvent, defaultURL string) []string { + urls = append(urls, event.URLs...) + if !event.ExcludeDefaultURL { + urls = append(urls, defaultURL) + } + return urls +} diff --git a/exchange/exchange.go b/exchange/exchange.go index 5c27b0d3c5a..f2afba57688 100644 --- a/exchange/exchange.go +++ b/exchange/exchange.go @@ -452,7 +452,7 @@ func (e *exchange) HoldAuction(ctx context.Context, r *AuctionRequest, debugLog } } - evTracking := getEventTracking(requestExtPrebid, r.StartTime, &r.Account, e.bidderInfo, e.externalURL) + evTracking := getEventTracking(requestExtPrebid, r.StartTime, &r.Account, e.bidderInfo, e.externalURL, macros.NewProvider(r.BidRequestWrapper)) adapterBids = evTracking.modifyBidsForEvents(adapterBids) r.HookExecutor.ExecuteAllProcessedBidResponsesStage(adapterBids)