Skip to content

Commit

Permalink
Merge pull request #87 from YellowBloomKnapsack/fix/script
Browse files Browse the repository at this point in the history
clicking an ad will now open a new tab
  • Loading branch information
MSPoulaei authored Aug 3, 2024
2 parents cf5714e + 5adb983 commit c269b96
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
10 changes: 2 additions & 8 deletions eventserver/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ func (h *EventServerHandler) PostClick(c *gin.Context) {
return
}

var req TokenRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

token := req.Token
c.Request.ParseForm()
token := c.Request.PostForm["token"][0]
data, err := h.tokenHandler.VerifyToken(token, key)
if err != nil {
grafana.TokenValidationTotal.WithLabelValues("invalid").Inc()
Expand Down Expand Up @@ -84,7 +79,6 @@ func (h *EventServerHandler) PostImpression(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

token := req.Token
data, err := h.tokenHandler.VerifyToken(token, key)
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions eventserver/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package handlers
import (
"YellowBloomKnapsack/mini-yektanet/common/models"
"bytes"
"strings"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
Expand Down Expand Up @@ -128,9 +130,12 @@ func TestPostClick(t *testing.T) {
r := gin.Default()
r.POST("/click", handler.PostClick)

req := httptest.NewRequest(http.MethodPost, "/click", nil)
req.Header.Set("Content-Type", "application/json")
req.Body = ioutil.NopCloser(bytes.NewReader([]byte(`{"token":"dummy-token"}`)))
data := url.Values{}
data.Set("token", "dummy-token")
req := httptest.NewRequest(http.MethodPost, "/click", strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// req.Header.Set("Content-Type", "application/json")
// req.Body = ioutil.NopCloser(bytes.NewReader([]byte(`{"token":"dummy-token"}`)))

w := httptest.NewRecorder()
r.ServeHTTP(w, req)
Expand Down
43 changes: 25 additions & 18 deletions panel/asset/scriptTemplate.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
const publisherId = parseInt("%d")
const AdServerAPILink = "%s"

function clickHandler(data) {
// form containing token
const form = document.createElement('form')
form.method = 'POST'
form.action = data['click_link']
form.target = '_blank'
form.style.display = 'none'

// add token to form
tokenField = document.createElement('input')
tokenField.type = 'hidden'
tokenField.name = 'token'
tokenField.value = data['click_token']
form.appendChild(tokenField)

// submit the form to server
document.body.appendChild(form)
form.submit()
document.body.removeChild(form)
}

fetch(AdServerAPILink+"/"+publisherId)
.then((res) => {
if (!res.ok) {
throw new Error("unable to load from ad server")
}
return res.json()
}).then((data) => {

console.log(data)
// console.log(data)

// ad image
const img = document.createElement("img")
Expand Down Expand Up @@ -44,9 +64,9 @@ fetch(AdServerAPILink+"/"+publisherId)
}

const impressionHandler = (entries, observer) => {
console.log(entries)
// console.log(entries)
entries.forEach((entry) => {
console.log(entry)
// console.log(entry)
if(entry.isIntersecting && !viewed) {
viewed = true
fetch(data["impression_link"], {
Expand All @@ -62,17 +82,4 @@ fetch(AdServerAPILink+"/"+publisherId)

let observer = new IntersectionObserver(impressionHandler, options)
observer.observe(adDiv)
})

function clickHandler(data) {
fetch(data["click_link"], {
method: "POST",
body: JSON.stringify({
token: data["click_token"]
})
})
.then(res=>{
console.log(res)
window.open(res.url)
})
}
})
43 changes: 25 additions & 18 deletions publisherwebsite/static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,35 @@ switch (publisherName) {
case "filimo": publisherId = 5; break;
}

function clickHandler(data) {
// form containing token
const form = document.createElement('form')
form.method = 'POST'
form.action = data['click_link']
form.target = '_blank'
form.style.display = 'none'

// add token to form
tokenField = document.createElement('input')
tokenField.type = 'hidden'
tokenField.name = 'token'
tokenField.value = data['click_token']
form.appendChild(tokenField)

// submit the form to server
document.body.appendChild(form)
form.submit()
document.body.removeChild(form)
}

fetch(AdServerAPILink+"/"+publisherId)
.then((res) => {
if (!res.ok) {
throw new Error("unable to load from ad server")
}
return res.json()
}).then((data) => {

console.log(data)
// console.log(data)

// ad image
const img = document.createElement("img")
Expand Down Expand Up @@ -59,9 +79,9 @@ fetch(AdServerAPILink+"/"+publisherId)
}

const impressionHandler = (entries, observer) => {
console.log(entries)
// console.log(entries)
entries.forEach((entry) => {
console.log(entry)
// console.log(entry)
if(entry.isIntersecting && !viewed) {
viewed = true
fetch(data["impression_link"], {
Expand All @@ -77,17 +97,4 @@ fetch(AdServerAPILink+"/"+publisherId)

let observer = new IntersectionObserver(impressionHandler, options)
observer.observe(adDiv)
})

function clickHandler(data) {
fetch(data["click_link"], {
method: "POST",
body: JSON.stringify({
token: data["click_token"]
})
})
.then(res=>{
console.log(res)
window.open(res.url)
})
}
})

0 comments on commit c269b96

Please sign in to comment.