VizuryEventLoggerCPF 1.2.9

VizuryEventLoggerCPF 1.2.9

Anurag Bhowmick 维护。



  • 作者:
  • Ayon Chowdhury

Vizury Logo

VizuryEventLogger

Version License

Vizury 是一个电子商务营销平台,其个性化重定向堆栈被数字公司用于增长营销 ROI 并提高交易。

概述

这是 iOS SDK 集成指南。该 SDK 支持 iOS9 及以上版本。

基本集成

通过 CocoaPods 集成

CocoaPods 是 Objective C & Swift 项目的依赖管理器,它使集成更加简单。

  1. 如果您还没有安装 CocoaPods,可以在终端中执行以下行来完成安装。

    sudo gem install cocoapods

  2. 如果您还没有 Podfile,可以在 Xcode 项目目录中创建一个名为 Podfile 的纯文本文件,并输入以下内容,确保设定与您的应用程序匹配的平台和版本。

    pod 'VizuryEventLogger'
    pod 'VizuryRichNotification'

    为了避免出现不必要的错误,请添加以下 Firebase pod
    pod 'Firebase/Messaging'

  3. 在 Xcode 项目目录中执行以下命令以安装 VizuryEventLoggerVizuryRichNotification SDK。

    pod install

  4. 现在,打开您的项目工作区并检查是否已正确添加 VizuryEventLoggerVizuryRichNotification SDK。

初始化 Vizury SDK

在您的AppDelegate文件中导入VizuryEventLogger

Objective-C


#import <VizuryEventLogger/VizuryEventLogger.h>

在 AppDelegate 的 didFinishLaunchingWithOptions 方法中添加以下代码以初始化 SDK

  [VizuryEventLogger initializeEventLoggerInApplication:(UIApplication*)application
                            WithPackageId:(NSString *)packageId
                            ServerURL:(NSString *)serverURL
                            WithCachingEnabled:(BOOL) caching
                            AndWithFCMEnabled:(BOOL) isFCMEnabled];

Swift


#import VizuryEventLogger

更新您的 AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate

在 AppDelegate 的 didFinishLaunchingWithOptions 方法中添加以下代码以初始化 SDK

VizuryEventLogger.initializeEventLogger(in: application,
			withPackageId: packageId, 
			serverURL: serverUrl,
			withCachingEnabled: caching, 
			AndWithFCMEnabled: isFCMEnabled)
Where 
  packageId     : packageId obtained from vizury
  serverURL     : serverURL obtained from vizury
  caching       : pass true if your app supports offline usage and you want to send user behaviour data 
                  to vizury while he was offline. Pass false otherwise
  isFCMEnabled  : true/false depending on if you want to use vizury for push

完成啦!!SDK 已经成功集成并初始化到项目中,可以开始使用了。

事件日志

当用户浏览应用程序时,会发生各种活动,例如浏览产品、将产品添加到购物车、进行购买等。这些被称为事件。对于每个事件,应用程序需要向 SDK 传递某些变量,SDK 将自动将这些变量传递给 Vizury 服务器。

Objective-C


创建一个与事件相关联的 attributeDictionary,并使用事件名称和 attributeDictionary 调用 `[VizuryEventLogger logEvent]

    #import <VizuryEventLogger/VizuryEventLogger.h>

    NSDictionary *attributeDictionary  =   [[NSDictionary alloc] initWithObjectsAndKeys:
                                            @"AKSJDASNBD",@"productid",
                                            @"789", @"productPrice",
                                            @"Shirt",@"category",
                                            nil];

    [VizuryEventLogger logEvent:@"productPage" WithAttributes:attributeDictionary];
    
    // sending a JSONObject
    NSDictionary *productDetail = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"62112",@"product_id",
                                   @"1",@"quantity",
                                   @"50", @"price",
                                   nil];
    
    NSData *productjsonData = [NSJSONSerialization dataWithJSONObject:productDetail options:0 error:nil];
    NSString *productjsonStr = [[NSString alloc] initWithData:productjsonData encoding:NSUTF8StringEncoding];
    
    NSDictionary *productjsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                           productjsonStr,@"viz_data",
                                           nil];
    [VizuryEventLogger logEvent:@"productDetails" WithAttributes:productjsonDictionary];

Swift


创建一个与事件相关联的 attributeDictionary,并使用 `VizuryEventLogger.logEvent` 方法传递事件名称和 attributeDictionary。

        let attributeDictionary = [ "productId":"AKSJDASNBD",
                                        "price" : "999",
                                        "category" : "shirt"]
        VizuryEventLogger.logEvent("productPage", withAttributes: attributeDictionary)

推送通知

为了发送推送通知,我们使用GCM-APNS接口。为此,您需要在苹果开发者中心中配置推送通知应用程序,并从谷歌获取配置文件。

配置苹果开发者设置

要启用通过APNs发送推送通知,您需要

a) 创建身份验证密钥。

b) 创建App ID。

c) 为该App ID创建配置文件。

您可以在苹果开发者中心中通过这些步骤创建它们。

配置FCM项目

设置CocoaPods依赖

  • 如果您还没有Xcode项目,现在请创建一个。
  • 如果您还没有Podfile,请创建一个。
$ cd your-project directory
$ pod init
  • 添加Firebase/Messaging pod。
pod 'Firebase/Messaging'
  • 安装pods并打开.xcworkspace文件以在Xcode中查看项目。
$ pod install
$ open your-project.xcworkspace

启用FCM

如果还没有,请先在Firebase控制台中创建一个Firebase项目,并输入项目名称

createProject-1

点击iOS选项,在下一个屏幕中添加iPhone包标识。该iPhone包标识应与您的应用程序包标识符相同。在下一步中,您可以下载GoogleService-Info.plist文件。

createProject-2

注意:您下载的GoogleService-Info.plist文件将会包含某些设置,如IS_ADS_ENABLED、IS_SIGNIN_ENABLED已经设置为YES。您需要添加相应的pod依赖项,或者如果您不使用它们,可以将其关闭。

接下来,点击创建的项目中的设置图标选项。

createProject-3

点击云消息选项卡并上传APNs认证密钥(.p8格式)。同时,请注意将服务器密钥记录下来,因为这在后续集成时是必需的。您还可以上传APNs证书,但推荐使用认证密钥进行配置,因为它们是发送iOS通知的最新方法。

createProject-4

在上传APNs认证密钥(.p8格式)时,您需要输入“密钥ID”和“团队ID”。

  1. ‘密钥ID’是指您在Apple开发者控制台“证书、标识符和配置文件” -> “证书” -> “选择特定的证书”下创建的认证密钥的ID。

createProject-4

  1. ‘团队ID’是指您在Apple开发者控制台下的“成员” -> “成员详细信息”中的团队成员ID。

createProject-4

配置应用程序

  • 将您刚下载的GoogleService-Info.plist文件拖放到Xcode项目的根目录中,并添加到所有目标
  • 在您的AppDelegate中的didFinishLaunchingWithOptions方法内部注册推送通知

Objective-C


    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
        // iOS 7.1 or earlier. Disable the deprecation warnings.
        #pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        UIRemoteNotificationType allNotificationTypes =
        (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
        [application registerForRemoteNotificationTypes:allNotificationTypes];
        #pragma clang diagnostic pop
    } else {
        // iOS 8 or later
        // [START register_for_notifications]
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
            #pragma clang diagnostic push
            #pragma clang diagnostic ignored "-Wdeprecated-declarations"
            UIUserNotificationType allNotificationTypes =
            (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
            UIUserNotificationSettings *settings =
            [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        } else {
            // iOS 10 or later
            #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
            // For iOS 10 display notification (sent via APNS)
            [UNUserNotificationCenter currentNotificationCenter].delegate = self;
            UNAuthorizationOptions authOptions =
            UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
            [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
            }];
            #endif
        }
        
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        // [END register_for_notifications]

Swift


        if #available(iOS 10.0, *) {
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
            UNUserNotificationCenter.current().delegate = self
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        application.registerForRemoteNotifications()
  • 对于iOS10及以上版本,您还需要添加以下代码。您可以参考示例应用。
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
#endif

// Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices
// running iOS 10 and above.
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
#endif
  • 注册后

将APNS令牌传递给Vizury

Objective-C


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    [VizuryEventLogger registerForPushWithToken:deviceToken];
}

Swift


    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        VizuryEventLogger.registerForPush(withToken: deviceToken)
    }

在任何注册失败的情况下

Objective-C


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    [VizuryEventLogger didFailToRegisterForPush];
}

Swift


    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        VizuryEventLogger.didFailToRegisterForPush()
    }
  • 处理通知有效负载

Objective-C


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(							UIBackgroundFetchResult))completionHandler {
    [VizuryEventLogger didReceiveRemoteNotificationInApplication:application withUserInfo:userInfo];
 }

Swift


    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler 			completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {        
        VizuryEventLogger.didReceiveRemoteNotification(in: application, withUserInfo: userInfo)
        if (application.applicationState == UIApplicationState.inactive) {
            self.customPushHandler(userInfo: userInfo)
        }
        completionHandler(UIBackgroundFetchResult.newData)
    }
    
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler 			completionHandler: @escaping () -> Void) {
        let pushDictionary = response.notification.request.content.userInfo
        VizuryEventLogger.didReceiveResponse(userInfo: pushDictionary)
        self.customPushHandler(userInfo: pushDictionary)
        completionHandler();
    }

Deeplinks

为了打开作为键/值对与推送通知一起发送到设备的深度链接,您必须实现一个自定义处理器

Objective-C


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(							UIBackgroundFetchResult))completionHandler {
    [VizuryEventLogger didReceiveRemoteNotificationInApplication:application withUserInfo:userInfo];
    if(application.applicationState == UIApplicationStateInactive) {
        NSLog(@"Appilication Inactive - the user has tapped in the notification when app was closed or in background");
    	[self customPushHandler:userInfo];
    }
 }

- (void) customPushHandler:(NSDictionary *)notification {
    if (notification !=nil && [notification objectForKey:@"deeplink"] != nil) {
        NSString* deeplink = [notification objectForKey:@"deeplink"];
        NSLog(@"%@",deeplink);
        // Here based on the deeplink you can open specific screens that's part of your app
    }
}

Swift


    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler 			completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {        
        VizuryEventLogger.didReceiveRemoteNotification(in: application, withUserInfo: userInfo)
        if (application.applicationState == UIApplicationState.inactive) {
            self.customPushHandler(userInfo: userInfo)
        }
        completionHandler(UIBackgroundFetchResult.newData)
    }
    
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler 			completionHandler: @escaping () -> Void) {
        let pushDictionary = response.notification.request.content.userInfo
        VizuryEventLogger.didReceiveResponse(userInfo: pushDictionary)
        self.customPushHandler(userInfo: pushDictionary)
        completionHandler();
    }
    
    func customPushHandler(userInfo : [AnyHashable : Any]) {        
        if let deeplink =  userInfo[AnyHashable("deeplink")] {
	    // handle the deeplink
            print("deeplink is ", deeplink)
        } 
    }

支持

请访问此存储库的GitHub问题追踪器,针对我们的iOS SDK提交特定错误报告。对于其他问题和支持,请从您的仪表板联系Vizury支持。