github.com/prebid/prebid-server/v2@v2.18.0/adapters/smaato/image.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 imageAd struct {
    13  	Image image `json:"image"`
    14  }
    15  type image struct {
    16  	Img                img      `json:"img"`
    17  	Impressiontrackers []string `json:"impressiontrackers"`
    18  	Clicktrackers      []string `json:"clicktrackers"`
    19  }
    20  type img struct {
    21  	URL    string `json:"url"`
    22  	W      int    `json:"w"`
    23  	H      int    `json:"h"`
    24  	Ctaurl string `json:"ctaurl"`
    25  }
    26  
    27  func extractAdmImage(adMarkup string) (string, error) {
    28  	var imageAd imageAd
    29  	if err := json.Unmarshal([]byte(adMarkup), &imageAd); err != nil {
    30  		return "", &errortypes.BadServerResponse{
    31  			Message: fmt.Sprintf("Invalid ad markup %s.", adMarkup),
    32  		}
    33  	}
    34  
    35  	var clickEvent strings.Builder
    36  	for _, clicktracker := range imageAd.Image.Clicktrackers {
    37  		clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'.replace(/\\+/g, ' ')), " +
    38  			"{cache: 'no-cache'});")
    39  	}
    40  
    41  	var impressionTracker strings.Builder
    42  	for _, impression := range imageAd.Image.Impressiontrackers {
    43  		impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
    44  	}
    45  
    46  	imageAdMarkup := fmt.Sprintf(`<div style="cursor:pointer" onclick="%s;window.open(decodeURIComponent('%s'.replace(/\+/g, ' ')));"><img src="%s" width="%d" height="%d"/>%s</div>`,
    47  		&clickEvent, url.QueryEscape(imageAd.Image.Img.Ctaurl), imageAd.Image.Img.URL, imageAd.Image.Img.W, imageAd.Image.Img.H, &impressionTracker)
    48  
    49  	return imageAdMarkup, nil
    50  }