Aptabase

Aptabase 的 Swift SDK

使用 Aptabase 为您的应用添加分析功能,Aptabase 是一个开源、注重隐私且简单易用的移动、桌面和 Web 应用分析平台。

安装

选项 1:Swift Package Manager

将以下代码添加到您的 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
        )
    ]
)

选项 2:使用 Xcode 添加包依赖

使用此指南aptabase-swift 添加到您的项目中。当 Xcode 询问 URL 时,使用 https://github.com/aptabase/aptabase-swift

选项 3:CocoaPods

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

几个重要的注意事项

  1. SDK 将自动增强事件,添加一些有用的信息,例如操作系统、应用版本和其他信息。
  2. 您可以控制发送到 Aptabase 的内容。 此 SDK 不会自动跟踪任何事件,您需要手动调用 trackEvent
    • 因此,通常建议至少在启动时跟踪一个事件
  3. trackEvent 函数是非阻塞操作,因为它在后台运行。
  4. 自定义属性只允许字符串和数字值

准备提交到 Apple App Store

当您将应用提交到 Apple App Store 时,您需要填写 App Privacy 表格。 您可以在我们的使用 Aptabase 时如何填写 Apple App Privacy 指南中找到所有答案。