AspectAutoLog 是一个用于实现 iOS 项目中自动追踪的库,使追踪变得更加简单和高效。(也称“ios 自动日志”,“ios 自动追踪”)此库支持页面显示追踪和按钮点击追踪。
- 支持 Swift 和 Objective-C 语言
- 自动追踪 UIViewController 的出现/消失/返回前台
- 自动追踪 UIControl 的点击
- 提供常用追踪参数,可通过 .aal.xxx 直接访问和修改,即插即用
- 追踪参数包括一个 extraParams 属性,用于添加更多自定义参数
- 基于运行时方法交换(method swizzling)
- 使用面向方面的编程范式实现
- 第 1 步:添加到 Podfile
pod 'AspectAutoLog'
- 第 2 步:实现 AALAspectAutoLogProtocol 协议
import AspectAutoLog
@objcMembers
public class YourCustom: NSObject, AspectAutoLogProtocol {
public static func logUIViewControllerAppear(_ viewController: UIViewController) {
// Report tracking events to your server, page appear event
}
public static func logUIViewControllerAppearing(whenAppEnterForeground viewController: UIViewController) {
// Report tracking events to your server, app returning to the foreground causing the page to be displayed to the user again
}
public static func logUIViewControllerDisAppear(_ viewController: UIViewController) {
let pageName = viewController.aal.pageNode.name // Page ID
let pageChain = viewController.aal.pageNode.nodeChain(containSelf: false) // Page access chain
let appearDuration = viewController.aal.appearDurationWhenDisappear // Page display time
let extraParams = viewController.aal.extraParams // Other parameters of the page
// Then use this data to report tracking events to your server, page removal event
....
}
public static func logUIControlWillTouchUpInside(control: UIControl, inViewController viewController: UIViewController?) {
let buttonName = control.aal.name // Button ID
let params = control.aal.extraParams // Other parameters of the button
let page = viewController // Page where the button is located
// Then use this data to report tracking events to your server
}
}
// .h file
#import <Foundation/Foundation.h>
#import <AspectAutoLog/AALAspectAutoLogProtocol.h>
NS_ASSUME_NONNULL_BEGIN
@interface YourCustom : NSObject <AALAspectAutoLogProtocol>
@end
NS_ASSUME_NONNULL_END
// .m file
#import "YourCustom.h"
#import <UIKit/UIKit.h>
@import AspectAutoLog;
@implementation YourCustom
+ (void)logUIViewControllerAppear:(UIViewController *)viewController {
// Report tracking events to your server, page appear event
}
+ (void)logUIViewControllerAppearingWhenAppEnterForeground:(UIViewController *)viewController {
// Report tracking events to your server, app returning to the foreground causing the page to be displayed to the user again
}
+ (void)logUIViewControllerDisAppear:(UIViewController *)viewController {
NSString *pageName = viewController.aal_pageNode.name; // Page ID
NSArray<AALPageNode *> *pageChain = [viewController.aal_pageNode nodeChainWithContainSelf:NO]; // Page access chain
NSTimeInterval appearDuration = viewController.aal_appearDurationWhenDisappear; // Page display time
NSDictionary<NSString *, id> *extraParams = viewController.aal_extraParams; // Other parameters of the page
// Then use this data to report tracking events to your server, page removal event
....
}
+ (void)logUIControlWillTouchUpInside:(UIControl *)control
inViewController:(UIViewController *)viewController {
NSString *buttonName = control.aal_name; // Button ID
NSDictionary<NSString *, id> *extraParams = control.aal_extraParams; // Other parameters of the button
UIViewController *page = viewController // Page where the button is located
// Then use this data to report tracking events to your server
}
@end
- 第 3 步:设置 AALAspectAutoFrogExecutor
// NSObject+YourCustom.h
@interface NSObject (YourCustom)
@end
// NSObject+YourCustom.m
#import "NSObject+YourCustom.h"
@import AspectAutoLog;
@implementation NSObject (YourCustom)
+ (void)load {
[AALAspectAutoLogExecutor registerWithLogger: [YourCustom class]];
}
@end
class SomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.aal.pageNode.name = "DemoPage" // Set the page name
self.aal.extraParams = [
"status": "hasGoNextButton"
] // Set and modify additional parameters at any time
let button = UIButton()
button.aal.name = "DemoDetailButton" // Name the button
button.aal.extraParams = [
"gender": "male"
] // Set and modify additional parameters at any time
button.setTitle("Detail", for: .normal)
button.addTarget(self, action: #selector(handleGoNextPageButtonPressed), for: .touchUpInside)
self.view.addSubview(button)
button.frame = CGRect(x: 100, y: 100, width: 300, height: 40)
}
@objc
private func handleGoNextPageButtonPressed() {
let page = UIViewController()
page.aal.pageNode.name = "DemoDetailPage"
page.aal.pageNode.prevNode = self.aal.pageNode // When navigating to a new page, set the page chain data
// Show other page
// self.navigationController?.pushViewController(page, animated: true)
// self.present(page, animated: true)
}
}
欢迎贡献、问题和特性请求。
AspectAutoLog 在 MIT 许可证下发布。