VizuryEventLogger
Vizury 是一个电商平台营销平台,其个性化再营销堆栈被数字公司用于提高营销回报率和增强交易。
摘要
这是 iOS SDK 集成指南。该 SDK 支持 iOS9 及以上版本。
基本集成
通过 CocoaPods 集成
CocoaPods 是 Objective C & Swift 项目的依赖管理器,它使集成更加容易。
-
如果您还没有安装 CocoaPods,您可以在终端中运行以下命令来安装。
sudo gem install cocoapods
-
如果您没有 Podfile,则在 Xcode 项目目录中创建一个名为 Podfile 的纯文本文件,并确保设置平台和版本与您的应用匹配。
pod 'VizuryEventLogger'
pod 'VizuryRichNotification'
为了避免不必要的 Issues,请添加以下 Firebase Pod
pod 'Firebase/Messaging'
-
在 Xcode 项目目录中执行以下操作来安装
VizuryEventLogger
和VizuryRichNotification
SDK。pod install
-
现在,打开您的项目工作区并检查
VizuryEventLogger
和VizuryRichNotification
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
创建一个关联事件的属性字典,并使用事件名称和属性字典调用 [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
创建一个与事件相关联的属性字典,并调用`VizuryEventLogger.logEvent`方法,传入事件名称和属性字典。
let attributeDictionary = [ "productId":"AKSJDASNBD",
"price" : "999",
"category" : "shirt"]
VizuryEventLogger.logEvent("productPage", withAttributes: attributeDictionary)
推送通知
为发送推送通知,我们使用GCM-APNS接口。为此,您需要在Apple开发者会员中心中配置应用以推送通知,并从Google获取配置文件。
配置Apple开发者设置
要启用通过APNs发送推送通知,您需要
a) 创建认证密钥。
b) 创建App ID
c) 为该App ID创建配置文件。
您可以在Apple开发者会员中心中按照以下步骤创建它们
配置项目以使用FCM
设置CocoaPods依赖项
- 如果您还没有创建Xcode项目,请现在创建一个
- 如果您还没有创建,请创建Podfile
$ cd your-project directory
$ pod init
- 添加
Firebase/Messaging
pod
pod 'Firebase/Messaging'
- 安装pod并打开.xcworkspace文件以在Xcode中查看项目
$ pod install
$ open your-project.xcworkspace
启用FCM
如果您还没有,请在Firebase控制台中创建一个Firebase项目。输入项目名称
。
点击iOS
选项,在下一个屏幕中添加iOS Bundle Id
。该iOS Bundle Id
应与您的应用程序bundle标识符相同。您可以在下一步中下载GoogleService-Info.plist
文件。
注意:您下载的GoogleService-Info.plist文件将包含一些设置,如IS_ADS_ENABLED、IS_SIGNIN_ENABLED设置为YES。您必须添加相应的pod依赖项或如果您不使用这些功能,可以将其关闭。
接下来点击创建的项目中的设置图标
选项。
点击云消息
标签页,并上传APNs身份验证密钥(.p8格式)。同时,记下服务器密钥
,因为稍后在集成时需要它。您还可以上传APNs证书,但使用身份验证密钥进行配置是推荐的,因为这是发送通知到iOS的更现代方法。
在上传APNs身份验证密钥(.p8格式)时,您需要输入密钥ID和团队ID。
- '密钥ID'是在苹果开发者控制台中创建的认证密钥的ID,在
Certificates, Identifiers & Profiles
->Keys
-> 选择特定的证书下。
- '团队ID'是苹果开发者控制台下
Membership
->Membership Details
中的团队成员ID。
配置应用程序
- 将您刚刚下载的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();
}
深链接
为了打开与推送通知一起作为键/值对发送到设备的深链接,您必须实现一个自定义处理器
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支持。