segment-appsflyer-ios 6.14.3

segment-appsflyer-ios 6.14.3

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2024年5月

af-builderMaxim ShoustinGil MerozAndrii HahanVitaly SokolovMargot GuettaShahar CohenMoris Gateno维护。



  • Appsflyer

AppsFlyer对Segment的集成。

这是一个使用iOS SDK v6.10.1构建的AppsFlyer SDK的Segment包装器。

Version Carthage compatible CI - Tests


🛠为了提供最佳支持,我们恳请您将任何问题提交到[email protected]

提交问题时,请指定您的AppsFlyer注册(账户)电子邮件、应用ID、生产步骤、日志、代码片段以及任何其他相关信息。


目录

重大更改

  • 从版本 6.3.2 开始,我们在 iOS 平台使用 xcframework,因此您需要使用 Cocoapods 版本 >= 1.10

安装

Cocoapods

要安装 segment-appsflyer-ios 集成

  1. 只需将以下行添加到您的 CocoaPods Podfile

生产版本

pod 'segment-appsflyer-ios', '6.10.1'

严格模式 SDK 版本

pod 'segment-appsflyer-ios/Strict', '6.10.1'

使用严格模式 SDK 可以完全移除 IDFA 收集功能和对 AdSupport 框架的依赖(例如,开发针对孩子的应用时)。

  1. 在项目目录中运行 pod install

Swift 包管理器

在 Xcode 中,前往 文件 > Swift 包 > 添加包依赖...,并添加 https://github.com/AppsFlyerSDK/segment-appsflyer-ios 作为包依赖 URL。

手动模式

从版本 6.8.0 开始,我们支持手动模式以分离 AppsFlyer SDK 的初始化和 SDK 的启动。在这种情况下,AppsFlyer SDK 不会自动启动,这给了开发者更多自由去决定何时启动 AppsFlyer SDK。请注意,在手动模式下,开发者需要实现 API AppsFlyerLib.shared().start() 以启动 SDK。

示例

  [SEGAppsFlyerIntegrationFactory createWithManualMode:YES];

请查看以下示例,了解如何使用代理使用手动模式。
要启动 AppsFlyer SDK,使用 start() API,如下所示

 [[AppsFlyerLib shared] start];

使用方法

首先,您必须在 Segment 的 仪表板 中为 AppsFlyer 集成提供 AppsFlyer 开发者密钥、Apple App ID(iTunes)和客户端密钥的值。

使用 - Objective-C

打开 AppDelegate.h 并添加

#import "SEGAppsFlyerIntegrationFactory.h"

AppDelegate.mdidFinishLaunchingWithOptions

    // For ApsFlyer debug logs
    [AppsFlyerLib shared].isDebug = YES;
    
//    [[AppsFlyerLib shared] waitForATTUserAuthorizationWithTimeoutInterval:60];
    /*
     Based on your needs you can either pass a delegate to process deferred
     and direct deeplinking callbacks or disregard them.
     If you choose to use the delegate, see extension to this class below
     */
    SEGAppsFlyerIntegrationFactory* factoryNoDelegate = [SEGAppsFlyerIntegrationFactory instance];
//    SEGAppsFlyerIntegrationFactory* factoryWithDelegate = [SEGAppsFlyerIntegrationFactory createWithLaunchDelegate:self];

// To use the manual mode, use the following method
//    SEGAppsFlyerIntegrationFactory* factoryNoDelegate = [SEGAppsFlyerIntegrationFactory createWithManualMode:YES];
//    SEGAppsFlyerIntegrationFactory* factoryWithDelegate = [SEGAppsFlyerIntegrationFactory createWithLaunchDelegate:self andManualMode:YES];

    
    SEGAnalyticsConfiguration *config = [SEGAnalyticsConfiguration configurationWithWriteKey:@"SEGMENT_KEY"];
    [config use:factoryNoDelegate];
//    [config use:factoryWithDelegate];  // use this if you want to get conversion data in the app. Read more in the integration guide
    config.enableAdvertisingTracking = YES;       //OPTIONAL
    config.trackApplicationLifecycleEvents = YES; //OPTIONAL
    config.trackDeepLinks = YES;                  //OPTIONAL
    config.trackPushNotifications = YES;          //OPTIONAL
    [SEGAnalytics debug:YES];                     //OPTIONAL
    [SEGAnalytics setupWithConfiguration:config];

AppDelegate.mapplicationDidBecomeActive

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Getting user consent dialog. Please read https://dev.appsflyer.com/hc/docs/integrate-ios-sdk#enabling-app-tracking-transparency-att-support
    if (@available(iOS 14, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            //....
        }];
    }
}

使用 - Swift

  1. 打开/创建 <Your-App-name>-Bridging-Header.h 并添加
#import "SEGAppsFlyerIntegrationFactory.h"

image

  1. 在构建设置中添加路径到桥接头:Build Settings > Swift Compiler - General > Objective-C Bridging Header 图像

  2. 打开 AppDelegate.swift 并添加

import Segment
import AppsFlyerLib
import segment_appsflyer_ios
  1. didFinishLaunchingWithOptions 添加
    // For AppsFLyer debug logs uncomment the line below
    // AppsFlyerLib.shared().isDebug = true
//    AppsFlyerLib.shared().waitForATTUserAuthorization(withTimeoutInterval: 60)

    /*
     Based on your needs you can either pass a delegate to process deferred
     and direct deeplinking callbacks or disregard them.
     If you choose to use the delegate, see extension to this class below
     */
//    let factoryWithDelegate : SEGAppsFlyerIntegrationFactory = SEGAppsFlyerIntegrationFactory.create(withLaunch: self)
    let factoryNoDelegate = SEGAppsFlyerIntegrationFactory()
    
    /* If you want to use the manual mode, please use the following methods
      with or without delegate */
     //  let factoryWithDelegate : SEGAppsFlyerIntegrationFactory = SEGAppsFlyerIntegrationFactory.create(withLaunch: self, andManualMode:true)
   // let factoryNoDelegate = SEGAppsFlyerIntegrationFactory.create(withManualMode: true)
    
    
    // Segment initialization
    let config = AnalyticsConfiguration(writeKey: "SEGMENT_KEY")
//    config.use(factoryWithDelegate)  // use this if you want to get conversion data in the app. Read more in the integration guide
    config.use(factoryNoDelegate)
    config.enableAdvertisingTracking = true       //OPTIONAL
    config.trackApplicationLifecycleEvents = true //OPTIONAL
    config.trackDeepLinks = true                  //OPTIONAL
    config.trackPushNotifications = true          //OPTIONAL
    
    Analytics.debug(false)
    Analytics.setup(with: config)

applicationDidBecomeActive 添加

func applicationDidBecomeActive(_ application: UIApplication) {
    // If you want to collect IDFA, please add the code below and read https://support.appsflyer.com//hc/en-us/articles/207032066#integration-35-support-apptrackingtransparency-att
    if #available(iOS 14, *) {
        ATTrackingManager.requestTrackingAuthorization(completionHandler: { (status) in
            // ...
        })
    }
}

AppsFlyer 集成响应对 identify 调用。要了解更多信息,请访问 Segment identify 方法文档。在 identify 调用中,在 traits 字典中设置 setCustomerUserIDcurrencyCode

获取转换数据

为了将转换数据发送到 Segment,请确保您已在 AppsFlyer 目的地设置中启用了“跟踪归因数据”并指定了 App ID

image

Objective-C

为了获取转换数据,您需要

  1. SEGAppsFlyerLibDelegate 协议添加到您的 AppDelegate.h (或其他) 类中
#import <UIKit/UIKit.h>
#import "SEGAppsFlyerIntegrationFactory.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate, SEGAppsFlyerLibDelegate>
  1. 当使用AppsFlyer配置Segment Analytics时,请传递AppDelegate(或其他)类。将代码[config use:[SEGAppsFlyerIntegrationFactory instance]];更改为[config use:[SEGAppsFlyerIntegrationFactory createWithLaunchDelegate:self]];
  2. 在上面的方法传递的类中(默认情况下为AppDelegate.m)实现SEGAppsFlyerLibDelegate协议的方法。下面是示例代码
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

-(void)onConversionDataSuccess:(NSDictionary*) installData {
    BOOL first_launch_flag = [[installData objectForKey:@"is_first_launch"] boolValue];
    NSString *status = [installData objectForKey:@"af_status"];
    
    if(first_launch_flag) {
        if ([status isEqualToString:@"Non-organic"]){
            NSString *sourceID = [installData objectForKey:@"media_source"];
            NSString *campaign = [installData objectForKey:@"campaign"];
            NSLog(@"This is a non-organic install. Media source: %@ Campaign: %@", sourceID, campaign);
        } else {
            NSLog(@"This is an organic install");
        }
    } else {
        NSLog(@"Not first launch");
    }
};

/**
 Any errors that occurred during the conversion request.
 */
-(void)onConversionDataFail:(NSError *) error {
    NSLog(@"%@", [error description]);
};

/**
 `attributionData` contains information about OneLink, deeplink.
 */
- (void)onAppOpenAttribution:(NSDictionary *)attributionData{
    NSLog(@"onAppOpenAttribution");
    for(id key in attributionData){
        NSLog(@"onAppOpenAttribution: key=%@ value=%@", key, [attributionData objectForKey:key]);
    }
};

/**
 Any errors that occurred during the attribution request.
 */
- (void)onAppOpenAttributionFailure:(NSError *)error{
    NSLog(@"%@", [error description]);
};

// Rest of your AppDelegate code

Swift

为了获取转换数据,您需要

  1. SEGAppsFlyerLibDelegate协议添加到您的AppDelegate(或其他)类
  2. 当使用AppsFlyer配置Segment Analytics时,请传递AppDelegate(或其他)类。如果使用上述示例代码,将config.use(factoryNoDelegate)更改为config.use(factoryWithDelegate)
  3. 在作为代理传递的类中实现协议的方法。下面是使用AppDelegate的示例代码
class AppDelegate: UIResponder, UIApplicationDelegate, SEGAppsFlyerLibDelegate {
  
  var window: UIWindow?
  
  func onConversionDataSuccess(_ conversionInfo: [AnyHashable : Any]) {
      guard let first_launch_flag = conversionInfo["is_first_launch"] as? Int else {
          return
      }
      
      guard let status = conversionInfo["af_status"] as? String else {
          return
      }
      
      if(first_launch_flag == 1) {
          if(status == "Non-organic") {
              if let media_source = conversionInfo["media_source"] , let campaign = conversionInfo["campaign"]{
                  print("This is a Non-Organic install. Media source: \(media_source) Campaign: \(campaign)")
              }
          } else {
              print("This is an organic install.")
          }
      } else {
          print("Not First Launch")
      }
  }
  
  func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]) {
      print("Deep Link Data goes here:")
      if let data = attributionData{
        print("\(data)")
      }
  }

 func onConversionDataFail(_ error: Error) {
      }

  func onAppOpenAttributionFailure(_ error: Error?) {
  }
  //rest of you AppDelegate code
}

Unity Deep linking

Swift

要使用Unified Deep linking,您需要

  1. SEGAppsFlyerDeepLinkDelegate协议添加到您的AppDelegate(或其他)类
  2. 当使用AppsFlyer配置Segment Analytics时,请传递AppDelegate(或其他)类。从上述示例代码中,将factoryWithDelegate更改为
let factoryWithDelegate: SEGAppsFlyerIntegrationFactory = SEGAppsFlyerIntegrationFactory.create(withLaunch: self, andDeepLinkDelegate: self)
  1. 在作为代理传递的类中实现协议的方法。下面是使用AppDelegate的示例代码
extension AppDelegate: SEGAppsFlyerDeepLinkDelegate {
    func didResolveDeepLink(_ result: DeepLinkResult) {
        print(result)
    }
}

安装属性事件

如果在与不支持向第三方传递用户级别数据的网络合作,您需要在调用之前应用代码来过滤这些网络

// [self.analytics track:@"Install Attributed" properties:[properties copy]];

附加 AppsFlyer SDK 设置

@import AppsFlyerLib;

...
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(integrationDidStart:) name:SEGAnalyticsIntegrationDidStart object:nil];
    ...
}

...

- (void)integrationDidStart:(nonnull NSNotification *)notification {
    NSString *integration = notification.object;
    if ([integration isEqualToString:@"AppsFlyer"]) {
        /// Additional AppsFlyer SDK setup goes below
        /// All setup is optional
        /// To set Apple App ID and AppsFlyer Dev Key use Segment dashboard
        /// ...
        /// Enable ESP support for specific URLs
        [[AppsFlyerLib shared] setResolveDeepLinkURLs:@[@"afsdktests.com"]];
        /// Disable printing SDK messages to the console log
        [[AppsFlyerLib shared]  setIsDebug:NO];
        /// `OneLink ID` from OneLink configuration
        [[AppsFlyerLib shared]  setAppInviteOneLink:@"one_link_id"];
    }
}

示例

本项目包含 4 个示例,涵盖 Objective-C 和 Swift(包括故障排除)。要尝试,请克隆此仓库,然后从每个示例中首先运行 pod install 以安装项目依赖项。