广告点击添加
This commit is contained in:
@@ -122,6 +122,13 @@ type Advertisement struct {
|
||||
AdvertiseCode string `json:"advertiseCode"`
|
||||
AdvertiseLocationCode string `json:"advertiseLocationCode"`
|
||||
AdvertiseLocationName string `json:"advertiseLocationName"`
|
||||
// AdsID 点击上报用的广告 id(上游 id 为数字字符串),在 advertiseCode 覆盖 ID 之前解析;无效为 0。
|
||||
AdsID int64 `json:"-"`
|
||||
}
|
||||
|
||||
// CanReportClick 广告 id 与 location 均为正整数时才上报点击(POST /api/app/ads/click)。
|
||||
func (a Advertisement) CanReportClick() bool {
|
||||
return a.AdsID > 0 && a.Location > 0
|
||||
}
|
||||
|
||||
// NewAddressInfo 对应 swagger proto.NewAddressInfo(GET /api/app/config/info)。
|
||||
|
||||
@@ -131,6 +131,7 @@ func buildAdvertisementList(list []Advertisement) []Advertisement {
|
||||
item.AdvertiseCode = strings.TrimSpace(item.AdvertiseCode)
|
||||
item.AdvertiseLocationCode = strings.TrimSpace(item.AdvertiseLocationCode)
|
||||
item.AdvertiseLocationName = strings.TrimSpace(item.AdvertiseLocationName)
|
||||
item.AdsID = parseAdsID(item.ID)
|
||||
if item.AdvertiseCode != "" {
|
||||
item.ID = item.AdvertiseCode
|
||||
}
|
||||
@@ -145,6 +146,15 @@ func buildAdvertisementList(list []Advertisement) []Advertisement {
|
||||
return out
|
||||
}
|
||||
|
||||
// parseAdsID 解析上游数字字符串广告 id;非正整数返回 0(不上报)。
|
||||
func parseAdsID(id string) int64 {
|
||||
n, err := strconv.ParseInt(strings.TrimSpace(id), 10, 64)
|
||||
if err != nil || n <= 0 {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// firstAdvertisementList 返回第一个有可用广告的列表(按字段优先级)。
|
||||
func firstAdvertisementList(lists ...[]Advertisement) []Advertisement {
|
||||
for _, list := range lists {
|
||||
|
||||
@@ -45,3 +45,30 @@ func TestBuildAppConfigVMFallsBackToLegacyBannerField(t *testing.T) {
|
||||
t.Fatalf("DetailBannerList = %#v, want detailbannerList fallback", vm.DetailBannerList)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildAppConfigVMParsesAdsClickID(t *testing.T) {
|
||||
vm := BuildAppConfigVM(&ConfigInfoResp{
|
||||
Advitise: AdvitiseData{
|
||||
NineGridList: []Advertisement{
|
||||
{ID: " 1138200002 ", Location: 2, Images: "https://cdn.example.com/a.gif"},
|
||||
{ID: "1138200003", Location: 2, AdvertiseCode: "code-x", Images: "https://cdn.example.com/b.gif"},
|
||||
{ID: "abc", Location: 2, Images: "https://cdn.example.com/c.gif"},
|
||||
{ID: "1138200004", Location: 0, Images: "https://cdn.example.com/d.gif"},
|
||||
},
|
||||
},
|
||||
})
|
||||
got := vm.NineGridList
|
||||
if len(got) != 4 {
|
||||
t.Fatalf("NineGridList = %#v", got)
|
||||
}
|
||||
if got[0].AdsID != 1138200002 || !got[0].CanReportClick() {
|
||||
t.Fatalf("item0 = %#v, want reportable id 1138200002", got[0])
|
||||
}
|
||||
// advertiseCode 覆盖展示 ID,但点击上报仍用上游原始 id
|
||||
if got[1].ID != "code-x" || got[1].AdsID != 1138200003 {
|
||||
t.Fatalf("item1 = %#v, want AdsID from raw id", got[1])
|
||||
}
|
||||
if got[2].CanReportClick() || got[3].CanReportClick() {
|
||||
t.Fatalf("invalid id/location must not report: %#v %#v", got[2], got[3])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user