广告点击添加
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package configser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"videoweb/app/service/upstream"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
adsClickPath = "/api/app/ads/click"
|
||||
// AdsClickAppID 广告点击上报的应用 ID,由广告后台分配,固定为 12。
|
||||
AdsClickAppID int64 = 12
|
||||
)
|
||||
|
||||
// AdsClickReq 对应上游 POST /api/app/ads/click(appId / adsId / location 均必填)。
|
||||
type AdsClickReq struct {
|
||||
AppID int64 `json:"appId"`
|
||||
AdsID int64 `json:"adsId"`
|
||||
Location int64 `json:"location"`
|
||||
}
|
||||
|
||||
// AdsClick 上报一次广告点击。
|
||||
func AdsClick(c *gin.Context, req AdsClickReq) (*APIEnvelope[any], error) {
|
||||
if req.AdsID <= 0 || req.Location <= 0 {
|
||||
return nil, fmt.Errorf("ads click invalid adsId=%d location=%d", req.AdsID, req.Location)
|
||||
}
|
||||
req.AppID = AdsClickAppID
|
||||
var env APIEnvelope[any]
|
||||
if _, err := upstream.PostJSON(c, adsClickPath, req, &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &env, nil
|
||||
}
|
||||
@@ -97,6 +97,20 @@ func buildAdvertisementList(list []Advertisement) []Advertisement {
|
||||
return out
|
||||
}
|
||||
|
||||
// ClickID 广告点击上报用的数字 ID(上游下发为数字字符串);非正整数时返回 0。
|
||||
func (a Advertisement) ClickID() int64 {
|
||||
id, err := strconv.ParseInt(strings.TrimSpace(a.ID), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return 0
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// ClickTrackable 广告 ID 与广告位均有效时才输出点击上报数据(data-ads-id / data-ads-location)。
|
||||
func (a Advertisement) ClickTrackable() bool {
|
||||
return a.ClickID() > 0 && a.Location > 0
|
||||
}
|
||||
|
||||
// firstAdvertisementList 返回第一个有可用广告的列表(按字段优先级)。
|
||||
func firstAdvertisementList(lists ...[]Advertisement) []Advertisement {
|
||||
for _, list := range lists {
|
||||
|
||||
@@ -80,3 +80,27 @@ func TestBuildAppConfigVMIncludesValidAdvertisements(t *testing.T) {
|
||||
t.Fatalf("DialogList[0] = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdvertisementClickTrackable(t *testing.T) {
|
||||
cases := []struct {
|
||||
ad Advertisement
|
||||
wantID int64
|
||||
trackable bool
|
||||
}{
|
||||
{Advertisement{ID: "1138200002", Location: 2}, 1138200002, true},
|
||||
{Advertisement{ID: " 1138200012 ", Location: 12}, 1138200012, true},
|
||||
{Advertisement{ID: "1138200003", Location: 0}, 1138200003, false},
|
||||
{Advertisement{ID: "", Location: 3}, 0, false},
|
||||
{Advertisement{ID: "0", Location: 3}, 0, false},
|
||||
{Advertisement{ID: "-5", Location: 3}, 0, false},
|
||||
{Advertisement{ID: "abc", Location: 3}, 0, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := tc.ad.ClickID(); got != tc.wantID {
|
||||
t.Fatalf("ClickID(%q) = %d, want %d", tc.ad.ID, got, tc.wantID)
|
||||
}
|
||||
if got := tc.ad.ClickTrackable(); got != tc.trackable {
|
||||
t.Fatalf("ClickTrackable(%q, %d) = %v, want %v", tc.ad.ID, tc.ad.Location, got, tc.trackable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user