Skip to content

Commit

Permalink
refactor(ocpc): 重构转化追踪相关API
Browse files Browse the repository at this point in the history
  • Loading branch information
bububa committed Apr 17, 2024
1 parent 360bcd4 commit 7d91e42
Show file tree
Hide file tree
Showing 17 changed files with 481 additions and 134 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
- 查询图片 [ GetImage(clt *core.SDKClient, auth model.RequestHeader, reqBody *image.GetImageRequest) ([]image.Image, error) ]
- 视频 (api/asset/video)
- 查询图片 [ GetVideo(clt *core.SDKClient, auth model.RequestHeader, reqBody *image.GetVideoRequest) ([]video.Video, error) ]
- 转化上报 (api/ocpc)
- 转化追踪 (api/ocpc)
- 广告主回传转化数据接口 [ UploadConvertData(clt *core.SDKClient, req *ocpc.UploadConvertDataRequest) error ]
- 广告主回传无效转化数据接口 [ UploadInvalidConvertData(clt *core.SDKClient, req *ocpc.UploadInvalidConvertDataRequest) error ]
- APP 转化数据收集 [ ActionCb(req *ocpc.ActionCbRequest) error ]
- APP 转化数据收集 [ ActionCb(clt *core.SDKClient, req model.ActionCbRequest) error ]
28 changes: 4 additions & 24 deletions api/ocpc/actionCb.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
package ocpc

import (
"encoding/json"
"net/http"

"github.com/bububa/baidu-marketing/model/ocpc"
"github.com/bububa/baidu-marketing/core"
"github.com/bububa/baidu-marketing/model"
)

// ActionCb APP转化数据收集
func ActionCb(req *ocpc.ActionCbRequest) error {
httpReq, err := http.NewRequest("GET", req.Url(), nil)
if err != nil {
return err
}
httpReq.Header.Add("Content-Type", "application/json")
httpResp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return err
}
defer httpResp.Body.Close()
var resp ocpc.ActionCbResponse
err = json.NewDecoder(httpResp.Body).Decode(&resp)
if err != nil {
return err
}
if resp.IsError() {
return resp
}
return nil
func ActionCb(clt *core.SDKClient, req model.ActionCbRequest) error {
return clt.ActionCb(req)
}
42 changes: 42 additions & 0 deletions api/ocpc/clickmonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ocpc

import "github.com/bububa/baidu-marketing/util"

var defaultFields = map[string]string{
"advertiser_id": "__USER_ID__",
"cid": "__IDEA_ID__",
"campaign_id": "__PLAN_ID__",
"aid": "__UNIT_ID__",
"word_id": "__WORD_ID__",
"callback_url": "__CALLBACK_URL__",
"ext_info": "__EXT_INFO__",
"clickid": "__CLICK_ID__",
"idfa": "__IDFA__",
"imei": "__IMEI__",
"android_id": "__ANDROIDID__",
"oaid_md5": "__OAID_MD5__",
"oaid": "__OAID__",
"caid": "__CAID__",
"ts": "__TS__",
"os_type": "__OS_TYPE__",
"bd_vid": "__BD_VID__",
}

func ClickMonitorUrl(baseUrl string, fields []string, version int) {
values := util.GetUrlValues()
defer util.PutUrlValues(values)
for _, f := range fields {
if version == 2 && f == "callback_ur" {
continue
}
if v, ok := defaultFields[f]; ok {
values.Set(f, v)
}
}
if version == 2 {
values.Set("callType", "v2")
} else {
values.Set("callType", "v1")
}
values.Set("sign", "__SIGN__")
}
6 changes: 0 additions & 6 deletions api/ocpc/const.go

This file was deleted.

26 changes: 3 additions & 23 deletions api/ocpc/uploadConvertData.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
package ocpc

import (
"bytes"
"encoding/json"

"github.com/bububa/baidu-marketing/core"
"github.com/bububa/baidu-marketing/model"
"github.com/bububa/baidu-marketing/model/ocpc"
)

// UploadConvertData 广告主回传转化数据接口
// 广告主通过调用该接口,将匹配到的转化数据发送给百度服务器。
func UploadConvertData(clt *core.SDKClient, req *ocpc.UploadConvertDataRequest) error {
if req.Token == "" {
req.Token = clt.OcpcToken()
}
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(req)
if err != nil {
return err
}
var resp ocpc.Response
err = clt.Post(UPLOAD_CONVERT_DATA_URL, buffer.Bytes(), &resp)
if err != nil {
return err
}
if resp.IsError() {
return resp
}
return nil
func UploadConvertData(clt *core.SDKClient, req *ocpc.UploadConvertDataRequest) (*model.ResponseHeader, error) {
return clt.Conversion(req, nil)
}
22 changes: 3 additions & 19 deletions api/ocpc/uploadInvalidConvertData.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
package ocpc

import (
"encoding/json"

"github.com/bububa/baidu-marketing/core"
"github.com/bububa/baidu-marketing/model"
"github.com/bububa/baidu-marketing/model/ocpc"
)

// UploadInvalidConvertData 广告主回传无效转化数据接口
// 广告主通过调用该接口,将认为是无效的转化数据发送给百度服务器。
func UploadInvalidConvertData(clt *core.SDKClient, req *ocpc.UploadInvalidConvertDataRequest) error {
if req.Token == "" {
req.Token = clt.Token()
}
reqBytes, err := json.Marshal(req)
if err != nil {
return err
}
var resp ocpc.Response
err = clt.Post(UPLOAD_CONVERT_DATA_URL, reqBytes, &resp)
if err != nil {
return err
}
if resp.IsError() {
return resp
}
return nil
func UploadInvalidConvertData(clt *core.SDKClient, req *ocpc.UploadInvalidConvertDataRequest) (*model.ResponseHeader, error) {
return clt.Conversion(req, nil)
}
111 changes: 103 additions & 8 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,56 @@ import (
"bytes"
"encoding/json"
"net/http"
"sync"
"time"

"github.com/bububa/baidu-marketing/core/internal/debug"
"github.com/bububa/baidu-marketing/model"
"github.com/bububa/baidu-marketing/util"
)

var (
onceInit sync.Once
httpClient *http.Client
)

func defaultHttpClient() *http.Client {
onceInit.Do(func() {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.MaxIdleConns = 100
transport.MaxConnsPerHost = 100
transport.MaxIdleConnsPerHost = 100
httpClient = &http.Client{
Transport: transport,
Timeout: time.Second * 60,
}
})
return httpClient
}

// SDKClient object
type SDKClient struct {
token string
ocpcToken string
username string
password string
debug bool
httpClient *http.Client
token string
ocpcToken string
username string
password string
debug bool
}

// NewSDKClient init sdk client
func NewSDKClient(token string, ocpcToken string) *SDKClient {
return &SDKClient{
token: token,
ocpcToken: ocpcToken,
token: token,
ocpcToken: ocpcToken,
httpClient: defaultHttpClient(),
}
}

func (c *SDKClient) SetHttpClient(httpClient *http.Client) {
c.httpClient = httpClient
}

// Token get token
func (c SDKClient) Token() string {
return c.token
Expand Down Expand Up @@ -89,6 +116,54 @@ func (c *SDKClient) Do(req *model.Request, resp interface{}) (*model.ResponseHea
return &reqResp.Header, nil
}

func (c *SDKClient) Conversion(req model.ConversionRequest, resp interface{}) (*model.ResponseHeader, error) {
if req.OcpcToken() == "" {
req.SetOcpcToken(c.ocpcToken)
}
buf := util.GetBufferPool()
defer util.PutBufferPool(buf)
if err := json.NewEncoder(buf).Encode(req); err != nil {
return nil, err
}
var reqResp model.Response
err := c.Post(req.Url(), buf.Bytes(), &reqResp)
if err != nil {
return nil, err
}
if reqResp.IsError() {
if reqResp.Header.Status == 1 && resp != nil {
err = json.Unmarshal(reqResp.Body, resp)
if err != nil {
return &reqResp.Header, err
}
}
return &reqResp.Header, reqResp
}
if resp != nil {
err = json.Unmarshal(reqResp.Body, resp)
if err != nil {
return &reqResp.Header, err
}
}
return &reqResp.Header, nil
}

func (c *SDKClient) ActionCb(req model.ActionCbRequest) error {
buf := util.GetBufferPool()
defer util.PutBufferPool(buf)
if err := json.NewEncoder(buf).Encode(req); err != nil {
return err
}
var reqResp model.ActionCbResponse
if err := c.Get(req.Url(), &reqResp); err != nil {
return err
}
if reqResp.IsError() {
return reqResp
}
return nil
}

// Post data through api
func (c *SDKClient) Post(reqUrl string, bs []byte, resp interface{}) error {
debug.PrintPostJSONRequest(reqUrl, bs, c.debug)
Expand All @@ -97,7 +172,27 @@ func (c *SDKClient) Post(reqUrl string, bs []byte, resp interface{}) error {
return err
}
httpReq.Header.Add("Content-Type", "application/json;charset=utf-8")
httpResp, err := http.DefaultClient.Do(httpReq)
httpResp, err := c.httpClient.Do(httpReq)
if err != nil {
return err
}
defer httpResp.Body.Close()
err = debug.DecodeJSONHttpResponse(httpResp.Body, resp, c.debug)
if err != nil {
debug.PrintError(err, c.debug)
return err
}
return nil
}

// Get data through api
func (c *SDKClient) Get(reqUrl string, resp interface{}) error {
debug.PrintGetRequest(reqUrl, c.debug)
httpReq, err := http.NewRequest(http.MethodGet, reqUrl, nil)
if err != nil {
return err
}
httpResp, err := c.httpClient.Do(httpReq)
if err != nil {
return err
}
Expand Down
43 changes: 43 additions & 0 deletions enum/aType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package enum

// Atype 表明用户的转化数据
type AType string

const (
// AType_ACTIVATE 激活
AType_ACTIVATE AType = "activate"
// ATYPE_REGISTER 注册
AType_REGISTER AType = "register"
// ATYPE_ORDERS 付费(成单)
AType_ORDERS AType = "orders"
// ATYPE_RETAIN_1DAY 次日留存
AType_RETAIN_1DAY AType = "retain_1day"
// AType_USER_DEFINED 客户自定义
AType_USER_DEFINED AType = "user_defined"
// AType_ECBuy 商品下单成功
AType_ECBuy AType = "ec_buy"
// AType_DEEP_PAGE_ACCESS 深度页面访问
AType_DEEP_PAGE_ACCESS AType = "deep_page_access"
// ATYPE_CREDT_GRANTING 授信
AType_CREDT_GRANTING AType = "credit_granting"
// AType_DEEPLINK 应用调起
AType_DEEPLINK AType = "deep_link"
// AType_FEED_DEEPLINK 应用调起
AType_FEED_DEEPLINK AType = "feed_deeplink"
// AType_PAY_TO_READ 付费阅读
AType_PAY_TO_READ AType = "pay_to_read"
// AType_ENTER_BOOKSTORE_READ 进入书城阅读
AType_ENTER_BOOKSTORE_READ AType = "enter_bookstore_read"
// AType_ADD_TO_DESKTOP 添加到桌面
AType_ADD_TO_DESKTOP AType = "add_to_desktop"
// AType_LOGIN 登录
AType_LOGIN AType = "log_in"
// AType_ORDER_SUBMIT_SUCCESS 订单提交成功
AType_ORDER_SUBMIT_SUCCESS AType = "order_submit_success"
// AType_PAY_TO_WATCH 付费观剧
AType_PAY_TO_WATCH AType = "pay_to_watch"
// AType_KEY_ACTION 关键行为
AType_KEY_ACTION AType = "key_action"
// AType_DERIVED_EVENT 衍生事件
AType_DERIVED_EVENT AType = "derived_event"
)
11 changes: 11 additions & 0 deletions enum/actType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package enum

// ActType 请求类型
type ActType int

const (
// ActType_CLICK 点击请求
ActType_CLICK ActType = 2
// ActType_IMPRESSION 曝光请求
ActType_IMPRESSION ActType = 3
)
4 changes: 4 additions & 0 deletions model/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ const (
BASE_URL_SMS = "https://api.baidu.com/json/sms/service/" // api base url
// BASE_URL_FEED feed/v2 api base url
BASE_URL_FEED = "https://api.baidu.com/json/feed/v1/"
// BASE_URL_OCPC
BASE_URL_OCPC = "https://ocpc.baidu.com/ocpcapi/api/"
// ACTIONCB_URL
ACTIONCB_URL = "https://als.baidu.com/cb/actionCb"
)
Loading

0 comments on commit 7d91e42

Please sign in to comment.