【发布时间】:2023-03-20 01:52:01
【问题描述】:
我正在尝试使用 Mixpanel 在我的 OS X 应用程序中跟踪事件。以下是他们的 API 文档:https://mixpanel.com/help/reference/http#tracking-via-http
我正在尝试发送带有事件名称和一堆属性的偶数。其中一个属性是文档中看到的令牌。看起来我需要将参数转换为字符串,然后对它们进行 base64 编码。最好的方法是什么?
我为此使用 Alamofire,参数是 Alamofire 参数。
这些是 Mixpanel 对 Base64 编码的要求
To Base64 encode data for the Mixpanel API, you should use the following characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789+/=
Mixpanel will only accept padded Base64 requests.
到目前为止,这是我的代码。我只是不知道如何对参数进行base64编码。
var headers: HTTPHeaders = [
"content-type": "application/json"
]
var parameters:Parameters = [String : Any]()
parameters["event"] = "hello world"
var properties = [String:String]
props["token"] = "INSERT_TOKEN_HERE"
parameters["properties"] = properties
var url = "http://api.mixpanel.com/track/?data=\()" //after the data= is where the base64 encoded parameters are supposed to go
Alamofire.request(url, method: .post, parameters: nil , encoding: JSONEncoding.default, headers: headers)
.responseJSON { (response) in
switch response.result {
case .success(let value):
print ("return: \(value)")
case .failure(let error):
print ("error: \(error)")
}
}
【问题讨论】:
-
顺便说一句,MixPanel 有自己的 iOS SDK,似乎可以让您摆脱构建他们时髦请求的杂草。 mixpanel.com/help/reference/swift
-
所以他们的 iOS SDK 的问题是它不支持 OS X 应用程序。我将 iOS SDK 用于 iOS 应用程序!
标签: swift macos alamofire swift4