AppMetrica SDK

CocoaPods Compatible SPM Index Swift Versions SPM Index Platforms

AppMetrica 是一个一站式营销平台,用于安装归因、应用分析和推送活动。AppMetrica 提供三大关键功能来评估您的应用性能:广告追踪、使用分析和崩溃分析。

安装

Swift Package Manager

通过 Xcode

  1. 前往 File > Add Package Dependency
  2. 放入 AppMetrica SDK 的 GitHub 链接:https://github.com/appmetrica/appmetrica-sdk-ios
  3. Add to Target 中,为您不需要的模块选择 None

通过 Package.swift Manifest

  1. 将 SDK 添加到您的项目依赖项中
dependencies: [
    .package(url: "https://github.com/appmetrica/appmetrica-sdk-ios", from: "5.0.0")
],
  1. 在您的目标的依赖项中列出模块
.target(
    name: "YourTargetName",
    dependencies: [
        .product(name: "AppMetricaCore", package: "appmetrica-sdk-ios"),
        // Add other modules like AppMetricaCrashes if needed.
    ]
)

CocoaPods

  1. 如果您还没有设置 CocoaPods,请在您的项目目录中运行 pod init
  2. 在您的 Podfile 中,添加 AppMetrica 依赖项
target 'YourAppName' do
    # For all analytics features, add this umbrella module:
    pod 'AppMetricaAnalytics', '~> 5.0.0'

    # If you need specific integration, skip 'AppMetricaAnalytics' and add specific modules:
    pod 'AppMetricaCore', '~> 5.0.0'
    # Add other modules like 'AppMetricaCrashes', 'AppMetricaWebKit' or 'AppMetricaAdSupport' if needed.
end
  1. 使用 pod install 安装依赖项。
  2. 使用 .xcworkspace 文件在 Xcode 中打开您的项目。

可选

儿童应用

为了符合 Apple 应用商店关于儿童隐私的规则(例如 COPPA),请添加 AppMetrica,但排除 AppMetricaAdSupport 模块

模块概览

集成快速入门

以下是如何将 AppMetrica 添加到您的项目(适用于 SwiftUI 和 UIKit)

  1. 在您的 AppDelegateimport AppMetricaCore

  2. application(_:didFinishLaunchingWithOptions:) 方法中使用您的 API 密钥初始化 AppMetrica。

对于 UIKit

将此代码放入您的 AppDelegate.swift

import AppMetricaCore

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
    if let configuration = AppMetricaConfiguration(apiKey: "Your_API_Key") {
        AppMetrica.activate(with: configuration)
    }
    return true
}

对于 SwiftUI

创建一个新的 Swift 文件以实现 AppDelegate 兼容性,并使用以下代码

import UIKit
import AppMetricaCore

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        if let configuration = AppMetricaConfiguration(apiKey: "Your_API_Key") {
            AppMetrica.activate(with: configuration)
        }
        return true
    }
}

然后在您的 App 结构体中

@main
struct YourAppNameApp: App {
    // Use the `@UIApplicationDelegateAdaptor` property wrapper to work with AppDelegate and set up AppMetrica
    @UIApplicationDelegateAdaptor var appDelegate: AppDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

注意:"Your_API_Key" 替换为您的实际 AppMetrica API 密钥,该密钥是在 AppMetrica Web 界面设置下提供的,它是您应用程序的唯一标识符。

高级配置

配置事件、配置文件属性和收入的发送

测试 SDK 集成

在继续测试之前,建议将测试数据与实际应用统计信息隔离。考虑使用单独的 API 密钥进行测试数据,方法是将统计信息发送到其他 API 密钥或在 AppMetrica 界面中添加具有新 API 密钥的另一个应用实例。

测试库操作的步骤

  1. 启动应用:启动与 AppMetrica SDK 集成的应用程序,并与之交互一段时间以生成测试数据。

  2. 互联网连接:确保运行该应用程序的设备已连接到互联网,以便将数据传输到 AppMetrica。

  3. 在 AppMetrica 界面中验证数据:登录到 AppMetrica 界面并确认以下内容

    • 新用户已出现在受众报告中,表明用户跟踪成功。
    • 会话数在参与度→会话报告中可见增加,表明应用正在被积极使用。
    • 您设置的自定义事件和配置文件属性反映在事件配置文件报告中,这意味着事件跟踪和用户分析按预期工作。

如果您遇到任何问题,请查阅故障排除部分

文档

您可以在我们的完整文档中找到全面的集成详细信息以及安装、配置、测试等说明。

许可证

AppMetrica 在 MIT 许可证下发布。 许可协议可在 LICENSE 中找到。