Golang对接Appsflyer

GUIN蚂蚁 · · 1115 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

AppsFlyer:官方文档
我的代码: Appsflyer-go

golang请求Appslfyer的接口

常量定义

const (
    AppID               = "com.your_appid"  // 在AppsFlyer上的产品名称
    AppsFlyerDevKey     = "your_appsflyer_devkey" // AppsFlyer的devKey
    AppsFlyerUrlFormat  = "https://api2.appsflyer.com/inappevent/%s"  // Appslfyer事件的url
    AppsFlyerTimeFormat = "2006-01-02 15:04:05.000" // utc时间格式
)

Event,存储事件相关信息

type Event struct {
    AppID  string         `json:"app_id"`  // AppsFyler的ID
    DevKey string         `json:"dev_key"` // 校验key
    Body   AppsFlyerEvent `json:"body"`    // 传输数据
}

AppsFlyerEvent,这个结构体包含的信息就是要上传的

type AppsFlyerEvent struct {
    AppsflyerID    string `json:"appsflyer_id"`               // 手机设备的AppsflyerID
    AdvertisingID  string `json:"advertising_id,omitempty"`   // 安卓设备ID(可选)
    IDFA           string `json:"idfa,omitempty"`             // IOS(可选)
    CustomerUserId string `json:"customer_user_id,omitempty"` // IOS(可选)
    BundleID       string `json:"bundle_id,omitempty"`        // IOS(可选)
    EventName      string `json:"eventName"`                  // 事件名称
    EventValue     string `json:"eventValue"`                 // 事件值(可选),json string or ''
    EventTime      string `json:"eventTime"`                  // 时间, utc时间
    AfEventsApi    string `json:"af_events_api"`              // 默认为true
}

AppsFlyer实现的方法

// AppsFlyer
type AppsFlyer struct {
    Client *http.Client  // http请求客户端
    event  *Event
}

// AppsFlyer发送事件的方法
func (s *AppsFlyer) SendEvent(event *Event) (err error) {
    if s.Client == nil {
        s.NewClient()
    }
    if event == nil {
        err = fmt.Errorf("SendEvent# param event is nil")
        return
    }
    s.event = event
    if err = s.post2AppsFlyer(); err != nil {
        return
    }
    return
}

使用示例

func main() {
    af := appsflyer.AppsFlyer{}
        // 事件信息,key-value
    eventValue := map[string]interface{}{
        "name": "gg",
        "age":  18,
    }
    event := appsflyer.Event{
        DevKey: appsflyer.AppsFlyerDevKey,
        AppID:  appsflyer.AppID,
        Body: appsflyer.AppsFlyerEvent{
            AppsflyerID: "your appsFlyer id",
            EventName:   "appsflyer_event",
            EventValue:  af.Json2String(eventValue), // json 字符串
            EventTime:   time.Now().In(time.UTC).Format(appsflyer.AppsFlyerTimeFormat),
            AfEventsApi: "true", // 必须为true
        },
    }
    if err := af.SendEvent(&event); err != nil {
        fmt.Printf("AppsFlyer SendEvent error, %v\n", err)
        return
    }
}

AppsflyerID:手机调用接口生成AppsflyerID。但其实随便填也可以,只是不能区分设备
EventName:事件名,可以自定义
EventValue: 该值可以为空,如果不为空的话,必须是json字符串
EventTime: 官方要求是UTC时间,格式也有要求
AfEventsApi:默认为true

注: 因为我是在服务器发送事件到AppsFlyer的,所以这里安卓/ios的信息我没填写,但不影响使用。


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:GUIN蚂蚁

查看原文:Golang对接Appsflyer

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1115 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传