Skip to content
项目

appmetrica/appmetrica-sdk-ios

在 GitHub 查看

关于

AppMetrica SDK for iOS

网站与应用开发

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

AppMetrica SDK

CocoaPods Compatible SPM Index Swift Versions SPM Index Platforms

AppMetrica is a one-stop marketing platform for install attribution, app analytics, and push campaigns. AppMetrica provides the three key features for assessing your app's performance: ad tracking, usage analytics, and crash analytics.

Installation

Swift Package Manager

Starting with version 7.1.0, we no longer publish the AppMetrica SDK to the public CocoaPods Trunk. Use Swift Package Manager to install these versions.

Through Xcode:

  1. Go to File > Add Package Dependency.
  2. Put the GitHub link of the AppMetrica SDK: https://github.com/appmetrica/appmetrica-sdk-ios.
  3. In Add to Target, select None for modules you don't want.

Via Package.swift Manifest:

  1. Add the SDK to your project's dependencies:
dependencies: [
    .package(url: "https://github.com/appmetrica/appmetrica-sdk-ios", from: "7.0.0")
],
  1. List the modules in your target's dependencies:
.target(
    name: "YourTargetName",
    dependencies: [
        // For all analytics features, add this umbrella module:
        .product(name: "AppMetricaAnalytics", package: "appmetrica-sdk-ios"),

        // Or add specific modules:
        // .product(name: "AppMetricaCore", package: "appmetrica-sdk-ios"),
        // .product(name: "AppMetricaCrashes", package: "appmetrica-sdk-ios"),
        // .product(name: "AppMetricaAdSupport", package: "appmetrica-sdk-ios"),
        // .product(name: "AppMetricaProductFlow", package: "appmetrica-sdk-ios"),
    ]
)

Optional

Children's Apps:

To meet Apple's App Store rules regarding children's privacy (like COPPA), add AppMetrica but leave out the AppMetricaAdSupport module:

  • SPM: Don't include AppMetricaAdSupport. Either choose None for this module when selecting packages in Xcode or specify dependencies in Package.swift.

Modules Overview

  • AppMetricaAnalytics: Umbrella module that includes all analytics features (Core, Crashes, AdSupport, WebKit, etc).
  • AppMetricaCore: Required for basic SDK use.
  • AppMetricaCrashes: Enables crash reports.
  • AppMetricaWebKit: Used for handling events from WebKit.
  • AppMetricaAdSupport: Needed for IDFA collection, don't include for children's apps.
  • AppMetricaScreenshot: Allows AppMetrica SDK to collect screenshot taken events.
  • AppMetricaProductFlow: Reports offer impressions and product acquisition funnel events. This optional module is not included in AppMetricaAnalytics and must be added separately.

Integration Quickstart

Here's how to add AppMetrica to your project (works for both SwiftUI and UIKit):

  1. import AppMetricaCore in your AppDelegate.

  2. Initialize AppMetrica with your API key in the application(_:didFinishLaunchingWithOptions:) method.

For UIKit:

Put this in your 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
}

For SwiftUI:

Create a new Swift file for AppDelegate compatibility and use this code:

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
    }
}

Then in your App struct:

@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()
        }
    }
}

Note: Replace "Your_API_Key" with your actual AppMetrica API key, which is a unique identifier for your application provided in the AppMetrica web interface under Settings.

Early Native Crash Monitoring

You can install native crash handlers before normal AppMetrica activation. The call is synchronous and does not activate AppMetrica. The first crash configuration supplied through early monitoring initialization or normal activation is copied and frozen. Normal activation is still mandatory to process and send persisted reports. Only native crash monitoring is started by the early call; ANR and probable-unhandled detection, report processing, state callbacks, and nonfatal reporting start after normal activation. Early monitoring initialization does not start or poll external crash providers; their existing registration and push behavior is unchanged.

Swift:

import AppMetricaCore
import AppMetricaCrashes

let crashConfiguration = AppMetricaCrashesConfiguration()
AppMetricaCrashes.crashes().initializeCrashMonitoring(with: crashConfiguration)

if let configuration = AppMetricaConfiguration(apiKey: "Your_API_Key") {
    AppMetrica.activate(with: configuration)
}

Objective-C:

#import <AppMetricaCore/AppMetricaCore.h>
#import <AppMetricaCrashes/AppMetricaCrashes.h>

AMAAppMetricaCrashesConfiguration *crashConfiguration = [AMAAppMetricaCrashesConfiguration new];
[[AMAAppMetricaCrashes crashes] initializeCrashMonitoringWithConfiguration:crashConfiguration];

AMAAppMetricaConfiguration *configuration =
    [[AMAAppMetricaConfiguration alloc] initWithAPIKey:@"Your_API_Key"];
[AMAAppMetrica activateWithConfiguration:configuration];

Advanced Configuration

Track Product Flows

Add the AppMetricaProductFlow module (available starting with version 6.5.0) to track a product acquisition funnel: an offer shown to the user, the start and intermediate steps of the flow, and its final result. Build each event and report it through the core AppMetrica API:

import AppMetricaCore
import AppMetricaProductFlow

let offerShown = ProductFlowEvents.offerShown(
    productOfferId: "offer-1",
    offerType: "insurance"
).withProductId("insurance-policy").build()
AppMetrica.report(event: offerShown, onFailure: nil)

let flowStart = ProductFlowEvents.flowStart(productId: "insurance-policy").build()
AppMetrica.report(event: flowStart, onFailure: nil)

let flowStep = ProductFlowEvents.flowStep(
    productId: "insurance-policy",
    stepType: "payment"
).build()
AppMetrica.report(event: flowStep, onFailure: nil)

let flowResult = ProductFlowEvents.flowResult(
    productId: "insurance-policy",
    status: .success
).build()
AppMetrica.report(event: flowResult, onFailure: nil)

String literals passed as product, offer, and step identifiers must not be empty. If a flow result is initially reported with the pending status, report another result event with the final status when it becomes known.

Configure Sending of Events, Profile Attributes, and Revenue

  • Sending Custom Events: To capture and analyze user actions within your app, you should configure the sending of custom events. For more information, see Events.

  • User Profiles: To gather insights into your user base, set up the sending of profile attributes. This allows for a richer analysis of user behavior segmented by custom attributes. Remember, a profile attribute can hold only one value, and sending a new value for an attribute will overwrite the existing one. For more information, see User profile.

  • In-App Purchases (Revenue Tracking): To monitor in-app purchases effectively, configure the sending of revenue events. This feature enables you to comprehensively track transactions within your application. For setup details, see In-app purchases.

Testing the SDK integration

Before you move on to testing, it's advisable to isolate your test data from actual app statistics. Consider using a separate API key for test data by sending statistics to an additional API key or adding another app instance with a new API key in the AppMetrica interface.

Steps to Test the Library's Operation:

  1. Launch the App: Start your application integrated with the AppMetrica SDK and interact with it for a while to generate test data.

  2. Internet Connection: Ensure that the device running the app is connected to the internet to allow data transmission to AppMetrica.

  3. Verify data in the AppMetrica Interface: Log into the AppMetrica interface and confirm the following:

    • A new user has appeared in the Audience report, indicating successful user tracking.
    • An increase in the number of sessions is visible in the Engagement → Sessions report, showing active app usage.
    • Custom events and profile attributes you've set up are reflected in the Events and Profiles reports, which means that event tracking and user profiling are working as intended.

If you encounter any issues, please consult the troubleshooting section.

Documentation

You can find comprehensive integration details and instructions for installation, configuration, testing, and more in our full documentation.

License

AppMetrica is released under the MIT License. License agreement is available at LICENSE.

探索相关项目

加载中…