使用 Aptabase 为您的应用添加分析功能,Aptabase 是一个开源、注重隐私且简单易用的移动、桌面和 Web 应用分析平台。
将以下代码添加到您的 Package.swift
文件中
let package = Package(
...
dependencies: [
...
.package(name: "Aptabase", url: "https://github.com/aptabase/aptabase-swift.git", from: "0.3.4"),
],
targets: [
.target(
name: "MyApp",
dependencies: ["Aptabase"] // Add as a dependency
)
]
)
使用此指南将 aptabase-swift
添加到您的项目中。当 Xcode 询问 URL 时,使用 https://github.com/aptabase/aptabase-swift。
Aptabase 也可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile
pod 'Aptabase', :git => 'https://github.com/aptabase/aptabase-swift.git', :tag => '0.3.4'
如果您面向 macOS,您必须首先启用
App Sandbox
部分下的Outgoing Connections (Client)
复选框。
首先,您需要从 Aptabase 获取您的 App Key
,您可以在左侧菜单的 Instructions
菜单中找到它。
在您的应用中尽早初始化 SDK,例如:
import SwiftUI
import Aptabase
@main
struct ExampleApp: App {
init() {
Aptabase.shared.initialize(appKey: "<YOUR_APP_KEY>") // 👈 this is where you enter your App Key
}
var body: some Scene {
WindowGroup {
MainView()
}
}
}
之后,您可以使用 trackEvent
开始跟踪事件
import Aptabase
Aptabase.shared.trackEvent("app_started") // An event with no properties
Aptabase.shared.trackEvent("screen_view", with: ["name": "Settings"]) // An event with a custom property
几个重要的注意事项
trackEvent
。trackEvent
函数是非阻塞操作,因为它在后台运行。当您将应用提交到 Apple App Store 时,您需要填写 App Privacy
表格。 您可以在我们的使用 Aptabase 时如何填写 Apple App Privacy 指南中找到所有答案。