Skip to content

Commit

Permalink
Smaato: Rework multi imp support and add adpod support (prebid#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
el-chuck authored and shunj-nb committed Nov 8, 2022
1 parent f5dc00a commit 93326db
Show file tree
Hide file tree
Showing 46 changed files with 3,926 additions and 424 deletions.
42 changes: 19 additions & 23 deletions adapters/smaato/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package smaato
import (
"encoding/json"
"fmt"
"github.com/prebid/prebid-server/errortypes"
"net/url"
"strings"
)
Expand All @@ -22,32 +23,27 @@ type img struct {
Ctaurl string `json:"ctaurl"`
}

func extractAdmImage(adapterResponseAdm string) (string, error) {
var imgMarkup string
var err error

func extractAdmImage(adMarkup string) (string, error) {
var imageAd imageAd
err = json.Unmarshal([]byte(adapterResponseAdm), &imageAd)
var image = imageAd.Image

if err == nil {
var clickEvent strings.Builder
var impressionTracker strings.Builder

for _, clicktracker := range image.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'.replace(/\\+/g, ' ')), " +
"{cache: 'no-cache'});")
if err := json.Unmarshal([]byte(adMarkup), &imageAd); err != nil {
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Invalid ad markup %s.", adMarkup),
}
}

for _, impression := range image.Impressiontrackers {

impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
var clickEvent strings.Builder
for _, clicktracker := range imageAd.Image.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'.replace(/\\+/g, ' ')), " +
"{cache: 'no-cache'});")
}

imgMarkup = fmt.Sprintf(`<div style="cursor:pointer" onclick="%s;window.open(decodeURIComponent('%s'.replace(/\+/g, ' ')));"><img src="%s" width="%d" height="%d"/>%s</div>`,
&clickEvent, url.QueryEscape(image.Img.Ctaurl), image.
Img.URL, image.Img.W, image.Img.
H, &impressionTracker)
var impressionTracker strings.Builder
for _, impression := range imageAd.Image.Impressiontrackers {
impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
return imgMarkup, err

imageAdMarkup := fmt.Sprintf(`<div style="cursor:pointer" onclick="%s;window.open(decodeURIComponent('%s'.replace(/\+/g, ' ')));"><img src="%s" width="%d" height="%d"/>%s</div>`,
&clickEvent, url.QueryEscape(imageAd.Image.Img.Ctaurl), imageAd.Image.Img.URL, imageAd.Image.Img.W, imageAd.Image.Img.H, &impressionTracker)

return imageAdMarkup, nil
}
59 changes: 33 additions & 26 deletions adapters/smaato/image_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
package smaato

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestRenderAdMarkup(t *testing.T) {
type args struct {
adType adMarkupType
adapterResponseAdm string
}
expectedResult := `<div style="cursor:pointer"` +
` onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});;window.open(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fctaurl%2F1'.replace(/\+/g, ' ')));">` +
`<img src="//prebid-test.smaatolabs.net/img/320x50.jpg" width="350" height="50"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`

func TestExtractAdmImage(t *testing.T) {
tests := []struct {
testName string
args args
result string
testName string
adMarkup string
expectedAdMarkup string
expectedError string
}{
{"imageTest", args{"Img",
"{\"image\":{\"img\":{\"url\":\"//prebid-test.smaatolabs.net/img/320x50.jpg\"," +
{
testName: "extract image",
adMarkup: "{\"image\":{\"img\":{\"url\":\"//prebid-test.smaatolabs.net/img/320x50.jpg\"," +
"\"w\":350,\"h\":50,\"ctaurl\":\"//prebid-test.smaatolabs.net/track/ctaurl/1\"}," +
"\"impressiontrackers\":[\"//prebid-test.smaatolabs.net/track/imp/1\",\"//prebid-test.smaatolabs.net/track/imp/2\"]," +
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}"},
expectedResult,
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}",
expectedAdMarkup: `<div style="cursor:pointer"` +
` onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});;window.open(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fctaurl%2F1'.replace(/\+/g, ' ')));">` +
`<img src="//prebid-test.smaatolabs.net/img/320x50.jpg" width="350" height="50"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`,
expectedError: "",
},
{
testName: "invalid adMarkup",
adMarkup: "{",
expectedAdMarkup: "",
expectedError: "Invalid ad markup {.",
},
}

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
got, err := renderAdMarkup(tt.args.adType, tt.args.adapterResponseAdm)
if err != nil {
t.Errorf("error rendering ad markup: %v", err)
}
if got != tt.result {
t.Errorf("renderAdMarkup() got = %v, result %v", got, tt.result)
adMarkup, err := extractAdmImage(tt.adMarkup)

if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
} else {
assert.NoError(t, err)
}

assert.Equal(t, tt.expectedAdMarkup, adMarkup)
})
}
}
2 changes: 2 additions & 0 deletions adapters/smaato/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func TestInvalidParams(t *testing.T) {

var validParams = []string{
`{"publisherId":"test-id-1234-smaato","adspaceId": "1123581321"}`,
`{"publisherId":"test-id-1234-smaato","adbreakId": "4123581321"}`,
`{"publisherId":"test-id-1234-smaato","adspaceId": "1123581321","adbreakId": "4123581321"}`,
}

var invalidParams = []string{
Expand Down
41 changes: 19 additions & 22 deletions adapters/smaato/richmedia.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package smaato
import (
"encoding/json"
"fmt"
"github.com/prebid/prebid-server/errortypes"
"net/url"
"strings"
)
Expand All @@ -22,31 +23,27 @@ type richmedia struct {
Clicktrackers []string `json:"clicktrackers"`
}

func extractAdmRichMedia(adapterResponseAdm string) (string, error) {
var richMediaMarkup string
var err error

func extractAdmRichMedia(adMarkup string) (string, error) {
var richMediaAd richMediaAd
err = json.Unmarshal([]byte(adapterResponseAdm), &richMediaAd)
var richMedia = richMediaAd.RichMedia

if err == nil {
var clickEvent strings.Builder
var impressionTracker strings.Builder

for _, clicktracker := range richMedia.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'), " +
"{cache: 'no-cache'});")
if err := json.Unmarshal([]byte(adMarkup), &richMediaAd); err != nil {
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Invalid ad markup %s.", adMarkup),
}
for _, impression := range richMedia.Impressiontrackers {
}

impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
var clickEvent strings.Builder
var impressionTracker strings.Builder

richMediaMarkup = fmt.Sprintf(`<div onclick="%s">%s%s</div>`,
&clickEvent,
richMedia.MediaData.Content,
&impressionTracker)
for _, clicktracker := range richMediaAd.RichMedia.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'), " +
"{cache: 'no-cache'});")
}
for _, impression := range richMediaAd.RichMedia.Impressiontrackers {
impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
return richMediaMarkup, err

richmediaAdMarkup := fmt.Sprintf(`<div onclick="%s">%s%s</div>`,
&clickEvent, richMediaAd.RichMedia.MediaData.Content, &impressionTracker)

return richmediaAdMarkup, nil
}
53 changes: 31 additions & 22 deletions adapters/smaato/richmedia_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
package smaato

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestExtractAdmRichMedia(t *testing.T) {
type args struct {
adType adMarkupType
adapterResponseAdm string
}
expectedResult := `<div onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'),` +
` {cache: 'no-cache'});"><div>hello</div><img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`
tests := []struct {
testName string
args args
result string
testName string
adMarkup string
expectedAdMarkup string
expectedError string
}{
{"richmediaTest", args{"Richmedia", "{\"richmedia\":{\"mediadata\":{\"content\":\"<div>hello</div>\"," +
"" + "\"w\":350," +
"\"h\":50},\"impressiontrackers\":[\"//prebid-test.smaatolabs.net/track/imp/1\",\"//prebid-test.smaatolabs.net/track/imp/2\"]," +
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}"},
expectedResult,
{
testName: "extract richmedia",
adMarkup: "{\"richmedia\":{\"mediadata\":{\"content\":\"<div>hello</div>\"," +
"" + "\"w\":350," +
"\"h\":50},\"impressiontrackers\":[\"//prebid-test.smaatolabs.net/track/imp/1\",\"//prebid-test.smaatolabs.net/track/imp/2\"]," +
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}",
expectedAdMarkup: `<div onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'),` +
` {cache: 'no-cache'});"><div>hello</div><img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`,
expectedError: "",
},
{
testName: "invalid adMarkup",
adMarkup: "{",
expectedAdMarkup: "",
expectedError: "Invalid ad markup {.",
},
}

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
got, err := renderAdMarkup(tt.args.adType, tt.args.adapterResponseAdm)
if err != nil {
t.Errorf("error rendering ad markup: %v", err)
}
if got != tt.result {
t.Errorf("renderAdMarkup() got = %v, result %v", got, tt.result)
adMarkup, err := extractAdmRichMedia(tt.adMarkup)

if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
} else {
assert.NoError(t, err)
}

assert.Equal(t, tt.expectedAdMarkup, adMarkup)
})
}
}
Loading

0 comments on commit 93326db

Please sign in to comment.