github.com/prebid/prebid-server/v2@v2.18.0/adapters/smaato/richmedia.go (about)

     1  package smaato
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"strings"
     8  
     9  	"github.com/prebid/prebid-server/v2/errortypes"
    10  )
    11  
    12  type richMediaAd struct {
    13  	RichMedia richmedia `json:"richmedia"`
    14  }
    15  type mediadata struct {
    16  	Content string `json:"content"`
    17  	W       int    `json:"w"`
    18  	H       int    `json:"h"`
    19  }
    20  
    21  type richmedia struct {
    22  	MediaData          mediadata `json:"mediadata"`
    23  	Impressiontrackers []string  `json:"impressiontrackers"`
    24  	Clicktrackers      []string  `json:"clicktrackers"`
    25  }
    26  
    27  func extractAdmRichMedia(adMarkup string) (string, error) {
    28  	var richMediaAd richMediaAd
    29  	if err := json.Unmarshal([]byte(adMarkup), &richMediaAd); err != nil {
    30  		return "", &errortypes.BadServerResponse{
    31  			Message: fmt.Sprintf("Invalid ad markup %s.", adMarkup),
    32  		}
    33  	}
    34  
    35  	var clickEvent strings.Builder
    36  	var impressionTracker strings.Builder
    37  
    38  	for _, clicktracker := range richMediaAd.RichMedia.Clicktrackers {
    39  		clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'), " +
    40  			"{cache: 'no-cache'});")
    41  	}
    42  	for _, impression := range richMediaAd.RichMedia.Impressiontrackers {
    43  		impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
    44  	}
    45  
    46  	richmediaAdMarkup := fmt.Sprintf(`<div onclick="%s">%s%s</div>`,
    47  		&clickEvent, richMediaAd.RichMedia.MediaData.Content, &impressionTracker)
    48  
    49  	return richmediaAdMarkup, nil
    50  }