VizuryEventLogger
Vizury 是一个商业营销平台,其个性化重定向堆栈被数字公司用于提高营销投资回报率并促进交易。
摘要
这是 iOS SDK 集成指南。SDK 支持iOS9及以上版本。
基本集成
通过 CocoaPods 集成
CocoaPods 是 Objective C & Swift 项目的依赖管理器,使集成更简单。
-
如果您尚未安装 CocoaPods,您可以在终端中执行以下行来安装它。
sudo gem install cocoapods
-
如果您还没有 Podfile,请在 Xcode 项目文件夹中创建一个名为 Podfile 的纯文本文件,并输入以下内容,确保设置的平台和版本与您的应用程序匹配。
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
创建一个与事件关联的属性字典,并使用事件名称和属性字典调用 [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接口。为此,您需要配置应用以在苹果开发者成员中心中推送通知并从谷歌获取配置文件。
配置苹果开发者设置
要启用通过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项目。输入项目名称
。
点击iOS
选项,然后在下一个屏幕中添加iOS Bundle Id
。 iOS Bundle Id
应与您的应用的bundle标识符相同。您可以在下一步中下载GoogleService-Info.plist
文件。
注意:您下载的
GoogleService-Info.plist
文件将包含某些设置,如IS_ADS_ENABLED、IS_SIGNIN_ENABLED设置为YES。您必须为相同的pod依赖项添加对应的内容,或者如果您不使用它们,可以将其关闭。
接下来,单击创建的项目中的Settings icon
选项。
单击Cloud Messaging
选项卡,上传APNs身份验证密钥(.p8格式)。同时注意记录Server key
,因为这将需要在集成过程中使用。您也可以上传APNs证书,但使用身份验证密钥进行配置是推荐的,因为它们是发送通知到iOS的更现代方法。
在上传APNs身份验证密钥(.p8格式)时,您需要输入Key Id和Team Id。
- “Key ID”是您在Apple开发者控制台中创建的身份验证密钥的ID,在“Certificates, Identifiers & Profiles” -> “Keys” -> 选择特定的证书。
- “Team ID”是Apple开发者控制台中的团队成员ID,在“Membership” -> “Membership Details”下。
配置应用
- 将您刚刚下载的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()
- 对于iOS 10及其以上版本,您还需要添加以下代码。您可以参考示例应用程序。
#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支持。