登录

iOS

连接iOS版MyTrackerSDK。作为例子,您可以在Objective-C和Swift中使用现成的应用程序演示

最低要求

从2023年10月16日的SDK 3.1.0版本开始,最低支持的iOS版本是12.4。

集成

使用CocoaPods来连接

把以下依赖项添加到您的Podfile中:

pod 'myTrackerSDK'

启动pod install。

Using Swift Package Manager

  1. In your project, select the menu File → Swift Packages → Add Package Dependency... to add a dependency.
  2. Select target you need to add SDK. If your project has one target, it will be selected automatically.
  3. In the Enter package repository URL field enter MyTracker SDK repository URL: https://github.com/myTrackerSDK/mytracker-ios-spm. XCode will prompt you to choose an SDK version. The latest version will be used by default.
  4. Click Next. MyTracker SDK will be added as a dependency to your project.

Due to some limitations on the Swift Package Manager side, you should add the AdServices framework to the app target after adding MyTracker SDK.

  1. Select the project file in the XCode navigator and select your app from the Targets list.
  2. Go to the Build Phases tab, add AdServices.framework and AppTrackingTransparency.framework to Link Binary With Libraries. After adding, make sure that you set Status to Optional.

手动

  1. 请下载MyTracker iOS SDK最新版本并且解压存档。

  2. 将 MyTrackerSDK.framework框架从解压存档中连接到项目。

  3. 将UIKit、AdSupport、SystemConfiguration、CoreTelephony、CoreData、 iAd、StoreKit、AdServices、AppTrackingTransparency框架连接到项目。

  4. 将libz.tbd程序库连接到项目。

初始化

为了确保MyTracker正确工作,需要在您的AppDelegate类应用程序中使用application:didFinishLaunchingWithOptions方法对跟踪器进行调整和初始化。为了进行跟踪器初始化,您需要输入SDK_KEY,但在此之前还需要进行必要的调整(配置、用户跟踪、深层链接等调整)。

SDK_KEY is generated automatically after you added your application to MyTracker. To get the key, go to the Application list page, select the required application, and copy the key from the Overview tab.

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //  如有需要,对跟踪器配置进行调整
    MRMyTrackerConfig *trackerConfig = [MRMyTracker trackerConfig];
    // …
    // 调整跟踪器的参数
    // …
    // 进行跟踪器初始化
    [MRMyTracker setupTracker:SDK_KEY];

   return YES;
}

@end
import MyTrackerSDK

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{

    // 如有需要,对跟踪器配置进行调整
    let trackerConfig = MRMyTracker.trackerConfig()
    // ...
    // 调整跟踪器的参数
    // ...
    // 进行跟踪器初始化
    MRMyTracker.setupTracker(SDK_KEY)

    return true
}        

API

跟踪器的配置

可以通过MRMyTrackerConfig类的实例调整跟踪器配置,通过[MRMyTracker trackerConfig]方法进行调整。可以设定以下参数:

trackLaunch: 应用程序启动的跟踪。默认值:YES。

@property(nonatomic) BOOL trackLaunch;
var trackLaunch: Bool

launchTimeout:当应用程序最小化时,在以秒为单位计算的时间间隔内,应用的新启动不会被计算,且会话不被中断。默认值:30秒。调整范围:30—7200秒。

@property(nonatomic) NSTimeInterval launchTimeout;
var launchTimeout: NSTimeInterval

bufferingPeriod:在以秒为单位计算的时间间隔内,事件在发送到服务器之前将积累在相关设备上。默认值:900秒。调整范围:1—86400秒。

@property(nonatomic) NSTimeInterval bufferingPeriod;
var bufferingPeriod: NSTimeInterval

forcingPeriod:应用安装或更新后在以秒为单位计算的时间间隔内,事件会立刻被发送到服务器,无需进行本地缓冲。默认值:0秒(包含立刻发送)。调整范围:0—432000秒。

@property(nonatomic) NSTimeInterval forcingPeriod;
var forcingPeriod: NSTimeInterval

autotrackPurchase:自动跟踪应用内购买情况。默认值:YES。

@property(nonatomic) BOOL autotrackPurchase;
var autotrackPurchase: Bool

locationTrackingMode: 跟踪用户的位置信息。可达到的值

  • MRLocationTrackingModeNone — 不进行跟踪位置

  • MRLocationTrackingModeCached — 使用系统缓存的值

  • MRLocationTrackingModeActive — 使用当前位置请求(默认值)

无论设定值如何,只有在应用程序获取用户允许的情况下,才可以跟踪用户的位置。

@property(nonatomic) MRLocationTrackingMode locationTrackingMode;
var locationTrackingMode: MRLocationTrackingMode

region:统计收集服务器所在的区域。例如,由于法律要求,需要更换区域。可用的值:

Since March 1, 2023, the region parameter is disabled. Regardless of the selected value, the data will be sent to servers located in the Russian Federation. To select a different region, please contact our support team

  • MRRegionNotSet — 默认值

  • MRRegionRu — 俄罗斯联邦境内的服务器

  • MRRegionEu — 欧盟境内的服务器

@property(nonatomic) MRRegion region
var region: MRRegion

registerForSKAdAttribution: SKAdNetwork attribution support. YES by default.

SKAdNetwork is Apple's solution to attribute app installations to a particular campaign while maintaining user privacy.

By default, when a user first launches the app on iOS 14.5+ devices, MyTracker SDK v2.1.2 and newer calls the SKAdNetwork method — registerAppForAdNetworkAttribution, to pass advertisement-driven install notifications to ad networks. For its parts, the ad network can send data to MyTracker. Since iOS 15, you can send a copy of the SKAN data directly to MyTracker in addition to sending it to ad networks.

You can choose to turn the registerForSKAdAttribution parameter off and set up SKAdNetwork methods on your own. Without SKAdNetwork, you will not be able to track attribution on devices if the user denied permission.

For more details, refer to the iOS & SKAdNetwork section and the Apple documentation

@property(nonatomic) BOOL registerForSKAdAttribution;
var registerForSKAdAttribution: Bool

打开/关闭调试模式

通过MRMyTracker类的静态方法打开/关闭调试模式。默认值:false

+ (void)setDebugMode:(BOOL)enabled;
+ (BOOL)isDebugMode;
static func setDebugMode(_ enabled: Bool)static func isDebugMode() -> Bool

Get SDK logs

You can create your own logger implementation to get MyTracker SDK logs not only in the console, but in your code.

To do this, use the static methods of the MRMyTracker class.

+ (void)setCustomLogger:(nullable id <MRLogger>)customLogger;
+ (nullable id <MRLogger>)customLogger;
static func setCustomLogger(_ customLogger: Logger?)
static func customLogger() -> Logger?

用户跟踪

请设置customUserId参数,以便既跟踪设备,又跟踪用户统计。这个参数是您项目中在用户注册时分配给用户的唯一ID。即使用户使用其他设备进行登录, ID仍保持不变。设置该参数以后,无论用户使用多少设备,您都可以评估应用软件受众的规模以及活动详情。如果用户决定更换设备,您也不会丢失用户数据累积的历史记录。

跟踪器的参数可以通过MRMyTrackerConfig类的实列进行配置,通过[MRMyTracker trackerConfig]方法进行调整。重要的是一定要在事件跟踪之前设置参数,以保证每次跟踪事件时,都会发送有关UserID。

-(void)setUserInfo
{
    MRMyTrackerParams *trackerParams = [MRMyTracker trackerParams];

    // 设置用户的ID
    trackerParams.customUserId = @"user_id";

}
func setUserInfo()
{
    let trackerParams = MRMyTracker.trackerParams()

    // 设置用户的ID
    trackerParams.customUserId = "user_id"
}

如果在设置customUserId之前,在应用程序中形成了已注册用户的数据库,那么MyTracker将无法获取有关该用户注册时间的数据,并且无法进行Lifetime指标的准确计算。对于此类用户,生命周期统计将根据customUserId首次目标跟踪的日期进行计算。

若需要禁用用户跟踪,请传递customUserId参数中的空值。

事件跟踪

通过MRMyTracker类静态方法可以发送事件。在方法调用之前设置customUserId参数,保证每次跟踪事件时,都会发送有关用户的ID。

您可以使用以下方法跟踪各种事件:

注册事件。 用户在应用程序中完成注册,并获取唯一ID后,您就要立刻调用该方法。在调用TrackRegistrationEvent前一定要设定 customUserId参数,否则有关事件将在没有用户ID信息的情况下被传送,导致统计不准确。

userId is a required parameter since SDK version 2.0.6

+ (void)trackRegistrationEvent:(NSString *)userId;
static func trackRegistrationEvent(userId: String) 

授权事件。 用户在应用程序中完成登录并获取唯一ID后,您就要立刻调用该方法。在调用TrackLoginEvent前一定要设定 customUserId参数,否则有关事件将在没有用户ID信息的情况下被传送,导致统计不准确。

userId is a required parameter since SDK version 2.0.6

+ (void)trackLoginEvent:(NSString *)userId;
static func trackLoginEvent(userId: String)

邀请发送事件。eventParams附加参数允许为事件设定任意键值参数。键和值的最大长度为255个字符。

+ (void)trackInviteEvent;
+ (void)trackInviteEventWithParams:(NSDictionary<NSString *, NSString *>*)eventParams;
static func trackInviteEvent()
static func trackInviteEvent(eventParams: [String: String]?)

升级事件。该事件发送时可以包括具体等级信息(level参数),也可以不包括。eventParams附加参数允许为事件设定任意键值参数。键和值的最大长度为255个字符。

+ (void)trackLevelAchieved;
+ (void)trackLevelAchievedWithLevel:(NSNumber *)level;
+ (void)trackLevelAchievedWithLevel:(NSNumber *)level eventParams:(NSDictionary<NSString*, NSString *> *)eventParams;
static func trackLevelAchieved()
static func trackLevelAchieved(level: NSNumber?)
static func trackLevelAchieved(level: NSNumber?, eventParams:[String: String]?)

具有指定名称的任意事件。eventParams附加参数允许为事件设定任意键值参数。名称,键和值的最大长度为255个字符。

+ (void)trackEventWithName:(NSString *)name;
+ (void)trackEventWithName:(NSString *)name eventParams:(NSDictionary<NSString*, NSString *> *)eventParams;
static func trackEvent(name: NSString)
static func trackEvent(name: NSString, eventParams: [String: String]?)

例子:

NSDictionary *eventCustomParams = @{
    @"someParamKey1" : @"someParamValue1",
    @"someParamKey2" : @"someParamValue2"
  };
[MRMyTracker trackEventWithName:@"eventName" eventParams:eventCustomParams];
let eventCustomParams = ["someParamKey1": "someParamValue1",
                         "someParamKey2": "someParamValue2"]
MRMyTracker.trackEvent(name: "eventName", eventParams:eventCustomParams)

强制发送所有事件并且重置发送计时器。

为了减少信道负载,且最大程度地减少对应用程序性能的影响,在把事件发送到服务器之前,SDK会把设备上的所有事件积累在缓冲区,并定期以压缩包的形式发送收集到的数据。默认情况下,每15分钟将数据发送到服务器。您可以通过bufferingPeriod参数在1秒到1天的范围内调整时间间隔。如果用户已关闭了该应用程序,数据将在下次启动时被发送。但是,某些事件对于尽早进入分析至关重要,尤其是在应用程序安装后的第一个会话。用flush()方法可以达成这个目的。

+ (void)flush;
static func flush()

To control the result of forced sending, use the flush() method with the Completion parameter. If the events are sent or there are no events to send, the log will show Flush complete, success: 1, if the events failed to be sent for some reason, Flush complete, success: 0.

+ (void)flushWithCompletionBlock:(void (^)(BOOL success))completionBlock;
static func flush(completion: (Bool) -> Void)

应用内购买跟踪

所有应用内的购买都被自动跟踪。

如果您想手动跟踪购买,需要使用参数(autotrackPurchase = NO)关闭自动跟踪购买,并使用以下方法:

+ (void)trackPurchaseWithProduct:(SKProduct *)product transaction:(SKPaymentTransaction *)transaction;
+ (void)trackPurchaseWithProduct:(SKProduct *)product transaction:(SKPaymentTransaction *)transaction eventParams:(NSDictionary<NSString *, NSString *> *)eventParams;
static func trackPurchase(product: SKProduct, transaction: SKPaymentTransaction)
static func trackPurchase(product: SKProduct, transaction: SKPaymentTransaction, eventParams: [String: String]?)

在启用自动跟踪模式的情况下,以上方法的调用会被忽略。强制参数(选项):

eventParams附加参数允许为事件设定任意键-值参数。键和值的最大长度为255个字符。


获取instanceId

设备IDinstanceId是应用程序首次启动时所生成的UUID v4值,在应用从设备删除之前保持不变。 通过MRMyTracker类讲台方法可以获取instanceId值(该方法不得用于主线程)。

+ (NSString *)instanceId;
static let instanceId: String

It's essential to collect instanceId as soon as possible and send identifiers to your server if you use these IDs for work with S2S API.

Instead of instanceId you can use any other device ID: idfa, iosVendorId, and/or the user identifier customUserID (in this case, S2S data will generate stats on users). Learn more

深层链接 允许在应用启动时向该应用发送附加参数。这些参数可用于转换到特定的应用程序界面。

延迟深度链接 允许在安装后首次启动时将附加参数传送到应用程序。这些参数的用法跟常规深层链接相同。

有关支持不同类型的深层链接以及集成所需步骤的详细信息,您可以参见我们的说明书 — 深层链接设置

为了支持深层链接和延迟深度链接,您需要在自己的应用中安装一个实现MRMyTrackerAttributionDelegate协议的委托(delegate)。如果发现针对当前安装的延迟深度链接,该委托将在首次启动应用时,以及每次通过深层链接启动应用时被调用。包含deeplink属性的MRMyTrackerAttribution归因对象将被作为参数发送到didReceiveAttribution方法。

@interface AppDelegate () <MRMyTrackerAttributionDelegate>
@end


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //  如有需要,对跟踪器配置进行调整
    MRMyTrackerConfig *trackerConfig = [MRMyTracker trackerConfig];
    // …
    // 调整跟踪器的参数
    // …

    // 安装用于获取延迟深度链接的委托
    [MRMyTracker setAttributionDelegate:self];

    // 对跟踪器进行初始化
    [MRMyTracker setupTracker:SDK_KEY];

   return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
{
    return [MRMyTracker handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
    return [MRMyTracker handleOpenURL:url options:options];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler
{
    return [MRMyTracker continueUserActivity:userActivity restorationHandler:restorationHandler];
}

#pragma mark - MRMyTrackerAttributionDelegate


- (void)didReceiveAttribution:(MRMyTrackerAttribution *)attribution
{
    NSString *deeplink = attribution.deeplink;
    // 深层链接处理
    // ...
}

@end
import MyTrackerSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MRMyTrackerAttributionDelegate
{
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
    {
        // 如有需要,对跟踪器配置进行调整
        let trackerConfig = MRMyTracker.trackerConfig()
        // ...
        // 调整跟踪器的参数
        // ...

        // 安装用于获取延迟深度链接的委托
        MRMyTracker.setAttributionDelegate(self)

        // 对跟踪器进行初始化
        MRMyTracker.setupTracker(SDK_KEY)

        return true
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
    {
        return MRMyTracker.handleOpen(url, sourceApplication: sourceApplication, annotation: annotation)
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
    {
        return MRMyTracker.handleOpen(url, options: options)
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
    {
        let handler: ([Any]?) -> Void = {_ in }
        return MRMyTracker.continue(userActivity, restorationHandler: handler)
    }

    // MARK: MRMyTrackerAttributionDelegate

    func didReceive(attribution: MRMyTrackerAttribution)
    {
        let deeplink = attribution.deeplink

        // 深层链接处理
        // ...
    }
}

如果需要,在安装委托时您可以通过setAttributionDelegate:delegateQueue方法设置执行委托方法的队列NSOperationQueue。如果安装委托时没有设置执行队列,didReceiveAttribution方法就将被调用于主线程。

Was this article helpful?
    Sorry, the translation has not been completed yet