Skip to content

Commit

Permalink
OTT-105: VCR - Video Event Trackers (#152)
Browse files Browse the repository at this point in the history
VCR - Video Event Trackers changes for PBS Module
  • Loading branch information
ShriprasadM authored and pm-nilesh-chate committed Jan 12, 2022
1 parent 56b32d2 commit 7f171ef
Show file tree
Hide file tree
Showing 9 changed files with 1,231 additions and 7 deletions.
9 changes: 5 additions & 4 deletions endpoints/cookie_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strconv"

"github.com/PubMatic-OpenWrap/prebid-server/analytics"
"github.com/PubMatic-OpenWrap/prebid-server/config"
"github.com/PubMatic-OpenWrap/prebid-server/gdpr"
Expand All @@ -17,10 +22,6 @@ import (
"github.com/buger/jsonparser"
"github.com/golang/glog"
"github.com/julienschmidt/httprouter"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
)

func NewCookieSyncEndpoint(
Expand Down
196 changes: 196 additions & 0 deletions endpoints/events/vtrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package events
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/PubMatic-OpenWrap/prebid-server/analytics"
"github.com/PubMatic-OpenWrap/prebid-server/config"
"github.com/PubMatic-OpenWrap/prebid-server/errortypes"
"github.com/PubMatic-OpenWrap/prebid-server/openrtb_ext"
"github.com/PubMatic-OpenWrap/prebid-server/prebid_cache_client"
"github.com/PubMatic-OpenWrap/prebid-server/stored_requests"
"github.com/golang/glog"
Expand Down Expand Up @@ -308,3 +310,197 @@ func ModifyVastXmlJSON(externalUrl string, data json.RawMessage, bidid, bidder,
}
return json.RawMessage(vast)
}

//InjectVideoEventTrackers injects the video tracking events
//Returns VAST xml contains as first argument. Second argument indicates whether the trackers are injected and last argument indicates if there is any error in injecting the trackers
func InjectVideoEventTrackers(trackerURL, vastXML string, bid *openrtb.Bid, bidder, accountID string, timestamp int64, bidRequest *openrtb.BidRequest) ([]byte, bool, error) {
// parse VAST
doc := etree.NewDocument()
err := doc.ReadFromString(vastXML)
if nil != err {
err = fmt.Errorf("Error parsing VAST XML. '%v'", err.Error())
glog.Errorf(err.Error())
return []byte(vastXML), false, err // false indicates events trackers are not injected
}

//Maintaining BidRequest Impression Map (Copied from exchange.go#applyCategoryMapping)
//TODO: It should be optimized by forming once and reusing
impMap := make(map[string]*openrtb.Imp)
for i := range bidRequest.Imp {
impMap[bidRequest.Imp[i].ID] = &bidRequest.Imp[i]
}

eventURLMap := GetVideoEventTracking(trackerURL, bid, bidder, accountID, timestamp, bidRequest, doc, impMap)
trackersInjected := false
// return if if no tracking URL
if len(eventURLMap) == 0 {
return []byte(vastXML), false, errors.New("Event URLs are not found")
}

creatives := FindCreatives(doc)

if adm := strings.TrimSpace(bid.AdM); adm == "" || strings.HasPrefix(adm, "http") {
// determine which creative type to be created based on linearity
if imp, ok := impMap[bid.ImpID]; ok && nil != imp.Video {
// create creative object
creatives = doc.FindElements("VAST/Ad/Wrapper/Creatives")
// var creative *etree.Element
// if len(creatives) > 0 {
// creative = creatives[0] // consider only first creative
// } else {
creative := doc.CreateElement("Creative")
creatives[0].AddChild(creative)

// }

switch imp.Video.Linearity {
case openrtb.VideoLinearityLinearInStream:
creative.AddChild(doc.CreateElement("Linear"))
case openrtb.VideoLinearityNonLinearOverlay:
creative.AddChild(doc.CreateElement("NonLinearAds"))
default: // create both type of creatives
creative.AddChild(doc.CreateElement("Linear"))
creative.AddChild(doc.CreateElement("NonLinearAds"))
}
creatives = creative.ChildElements() // point to actual cratives
}
}
for _, creative := range creatives {
trackingEvents := creative.SelectElement("TrackingEvents")
if nil == trackingEvents {
trackingEvents = creative.CreateElement("TrackingEvents")
creative.AddChild(trackingEvents)
}
// Inject
for event, url := range eventURLMap {
trackingEle := trackingEvents.CreateElement("Tracking")
trackingEle.CreateAttr("event", event)
trackingEle.SetText(fmt.Sprintf("%s", url))
trackersInjected = true
}
}

out := []byte(vastXML)
var wErr error
if trackersInjected {
out, wErr = doc.WriteToBytes()
trackersInjected = trackersInjected && nil == wErr
if nil != wErr {
glog.Errorf("%v", wErr.Error())
}
}
return out, trackersInjected, wErr
}

// GetVideoEventTracking returns map containing key as event name value as associaed video event tracking URL
// By default PBS will expect [EVENT_ID] macro in trackerURL to inject event information
// [EVENT_ID] will be injected with one of the following values
// firstQuartile, midpoint, thirdQuartile, complete
// If your company can not use [EVENT_ID] and has its own macro. provide config.TrackerMacros implementation
// and ensure that your macro is part of trackerURL configuration
func GetVideoEventTracking(trackerURL string, bid *openrtb.Bid, bidder string, accountId string, timestamp int64, req *openrtb.BidRequest, doc *etree.Document, impMap map[string]*openrtb.Imp) map[string]string {
eventURLMap := make(map[string]string)
if "" == strings.TrimSpace(trackerURL) {
return eventURLMap
}

// lookup custom macros
var customMacroMap map[string]string
if nil != req.Ext {
reqExt := new(openrtb_ext.ExtRequest)
err := json.Unmarshal(req.Ext, &reqExt)
if err == nil {
customMacroMap = reqExt.Prebid.Macros
} else {
glog.Warningf("Error in unmarshling req.Ext.Prebid.Vast: [%s]", err.Error())
}
}

for _, event := range trackingEvents {
eventURL := trackerURL
// lookup in custom macros
if nil != customMacroMap {
for customMacro, value := range customMacroMap {
eventURL = replaceMacro(eventURL, customMacro, value)
}
}
// replace standard macros
eventURL = replaceMacro(eventURL, VASTAdTypeMacro, string(openrtb_ext.BidTypeVideo))
if nil != req && nil != req.App {
// eventURL = replaceMacro(eventURL, VASTAppBundleMacro, req.App.Bundle)
eventURL = replaceMacro(eventURL, VASTDomainMacro, req.App.Bundle)
if nil != req.App.Publisher {
eventURL = replaceMacro(eventURL, PBSAccountMacro, req.App.Publisher.ID)
}
}
if nil != req && nil != req.Site {
eventURL = replaceMacro(eventURL, VASTDomainMacro, req.Site.Domain)
eventURL = replaceMacro(eventURL, VASTPageURLMacro, req.Site.Page)
if nil != req.Site.Publisher {
eventURL = replaceMacro(eventURL, PBSAccountMacro, req.Site.Publisher.ID)
}
}

if len(bid.ADomain) > 0 {
//eventURL = replaceMacro(eventURL, PBSAdvertiserNameMacro, strings.Join(bid.ADomain, ","))
domain, err := extractDomain(bid.ADomain[0])
if nil == err {
eventURL = replaceMacro(eventURL, PBSAdvertiserNameMacro, domain)
} else {
glog.Warningf("Unable to extract domain from '%s'. [%s]", bid.ADomain[0], err.Error())
}
}

eventURL = replaceMacro(eventURL, PBSBidderMacro, bidder)
eventURL = replaceMacro(eventURL, PBSBidIDMacro, bid.ID)
// replace [EVENT_ID] macro with PBS defined event ID
eventURL = replaceMacro(eventURL, PBSEventIDMacro, eventIDMap[event])

if imp, ok := impMap[bid.ImpID]; ok {
eventURL = replaceMacro(eventURL, PBSAdUnitIDMacro, imp.TagID)
}
eventURLMap[event] = eventURL
}
return eventURLMap
}

func replaceMacro(trackerURL, macro, value string) string {
macro = strings.TrimSpace(macro)
if strings.HasPrefix(macro, "[") && strings.HasSuffix(macro, "]") && len(strings.TrimSpace(value)) > 0 {
trackerURL = strings.ReplaceAll(trackerURL, macro, url.QueryEscape(value))
} else {
glog.Warningf("Invalid macro '%v'. Either empty or missing prefix '[' or suffix ']", macro)
}
return trackerURL
}

//FindCreatives finds Linear, NonLinearAds fro InLine and Wrapper Type of creatives
//from input doc - VAST Document
//NOTE: This function is temporarily seperated to reuse in ctv_auction.go. Because, in case of ctv
//we generate bid.id
func FindCreatives(doc *etree.Document) []*etree.Element {
// Find Creatives of Linear and NonLinear Type
// Injecting Tracking Events for Companion is not supported here
creatives := doc.FindElements("VAST/Ad/InLine/Creatives/Creative/Linear")
creatives = append(creatives, doc.FindElements("VAST/Ad/Wrapper/Creatives/Creative/Linear")...)
creatives = append(creatives, doc.FindElements("VAST/Ad/InLine/Creatives/Creative/NonLinearAds")...)
creatives = append(creatives, doc.FindElements("VAST/Ad/Wrapper/Creatives/Creative/NonLinearAds")...)
return creatives
}

func extractDomain(rawURL string) (string, error) {
if !strings.HasPrefix(rawURL, "http") {
rawURL = "http://" + rawURL
}
// decode rawURL
rawURL, err := url.QueryUnescape(rawURL)
if nil != err {
return "", err
}
url, err := url.Parse(rawURL)
if nil != err {
return "", err
}
// remove www if present
return strings.TrimPrefix(url.Hostname(), "www."), nil
}
Loading

0 comments on commit 7f171ef

Please sign in to comment.