如何将 Google Analytics 添加到 Cocoa Touch Framework for iOS 设备

How to add Google Analytics to Cocoa Touch Framework for iOS devices

我已经创建了一个框架,它可以从我们的 API 向使用它的客户提供数据。我想在此框架中使用 Google 分析。

但我不知道该怎么做,没有可用的文档或教程,特别是我要求在框架中使用 Google Analytics。

我用Google Analytics Measurement Protocol解决了。

我想使用 Google Analytics 跟踪我的框架的使用情况。但是当我将我的 .framework 文件嵌入 xcode 项目时它不起作用。因为它与项目本身的现有 Google 分析冲突。

最终我使用 Google Analytics Measurement Protocol 解决了这个问题,它只不过是对 GA 的简单 HTTP 请求。

这是我的 iOS 代码,使用 Swift 3 将屏幕名称发送到 GA -

let screen_payload = [
    "v": "app_version",                    // app_version
    "tid": "your_GA_tracking_id",          // tracking id
    "uid": "unique_user_id",               // app_id debug
    "an":"app_id_or_name",                 // app name
    "aid":"1.0.0",                         // app version id
    "t":"screenview",                      // type of GA analytic request
    "cd":"your_custom_screen_name"         // screen name
] as [String : Any]

// Request using Alamofire
Alamofire.request( "https://www.google-analytics.com/collect", method: .post, parameters: screen_payload).response{ response in
    print("\(String(describing: screen_payload))")
    print(response)
}

这里有一些更有用的链接 -