VizuryEventLogger
Vizury 是一个电子商务营销平台,其个性化重定向堆栈被数字公司用于增长营销 ROI 和增强交易。
总结
这是 iOS SDK 集成指南。SDK 支持 iOS9 及以上版本。
基本集成
通过 CocoaPods 集成
CocoaPods 是 Objective C & Swift 项目的依赖管理器,它使集成更加容易。
-
如果您还没有安装 CocoaPods,您可以在您的终端中通过执行以下行来安装:
sudo gem install cocoapods
-
如果您还没有 Podfile,请创建一个名为 Podfile 的纯文本文件,并在 Xcode 项目目录中包含以下内容,确保设置与您的应用匹配的平台和版本。
pod 'VizuryEventLogger'
pod 'VizuryRichNotification'
为了避免不必要的问题,请添加以下 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
创建一个存储与事件相关的属性的 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
创建一个与事件相关联的属性字典,并调用 `VizuryEventLogger.logEvent` 方法传递事件名称和属性字典。
let attributeDictionary = [ "productId":"AKSJDASNBD",
"price" : "999",
"category" : "shirt"]
VizuryEventLogger.logEvent("productPage", withAttributes: attributeDictionary)
推送通知
为了发送推送通知,我们使用 GCM-APNS 接口。为此,您需要在 Apple Developer Member Center 中配置推送通知应用,并从谷歌获取配置文件。
配置 Apple 开发者设置
要启用通过 APNs 发送推送通知,您需要
a) 创建身份验证密钥。
b) 创建 App ID。
c) 为该 App ID 设置配置文件。
您可以在 Apple Developer Member Center 中通过这些步骤创建它们。
为 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 控制台 中创建一个。输入 项目名称
。
点击 iOS
选项,并在下一屏添加 iOS Bundle Id
。该 iOS Bundle Id
应与您的应用捆绑标识符相同。您可以在下一步中下载 GoogleService-Info.plist
文件。
注意:您下载的 GoogleService-Info.plist 文件中会设置一些设置,例如 IS_ADS_ENABLED、IS_SIGNIN_ENABLED 设置为 YES。您必须添加相应的 pod 依赖项,或者如果您不使用它们,可以将其关闭
接下来,点击已创建项目的 设置图标
选项。
点击 云消息
选项卡,上传 APNs 认证密钥 (.p8 格式)。同时记下 服务密钥
,因为您在集成期间将需要它。您还可以上传 APNs 证书,但推荐使用具有授权密钥的配置,因为它们是发送 iOS 通知的更现代方式
在上传 APNs 认证密钥 (.p8 格式) 时,您需要输入密钥 ID 和团队 ID。
- “密钥 ID” 是您在 Apple 开发者控制台
证书、标识符和配置文件
->密钥
-> 选择特定的证书下创建的认证密钥的 ID。
- “团队 ID” 是 Apple 开发者控制台会员下的
会员资格
->会员资格详情
中的团队成员 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();
}
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支持。