埋点是一种了解用户行为,分析用户行为,提升用户体验的一种方式。 常见的解决方案有三种,代码埋点、可视化埋点、和无埋点三种。bash
无埋点能够作到,埋点被统一维护,与业务代码解耦,知足大部分需求。app
无埋点的实现主要是基于,runtime,在运行时,替换原有方法,实现埋点。ide
新建一个class,主要代码以下,ui
#import "SMHook.h"
#import <objc/runtime.h>
@implementation SMHook
+(void)hookClass:(Class)classObject fromSelector:(SEL)fromSelector toSelector:(SEL)toselector{
Class class = classObject;
// 获得被替换类的实例方法
Method fromMethod = class_getInstanceMethod(class, fromSelector);
// 获得替换类的实例方法
Method toMethod = class_getInstanceMethod(class, toselector);
if (class_addMethod(class, fromSelector, method_getImplementation(toMethod), method_getTypeEncoding(fromMethod))) {
class_replaceMethod(class, toselector, method_getImplementation(fromMethod), method_getTypeEncoding(toMethod));
}else{
method_exchangeImplementations(fromMethod, toMethod);
}
}
@end
复制代码
其中主要的做用是交换两个IMP指针的实现spa
method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)
复制代码
IMP是具体方法的实现。指针
分析:获取页面的停留时间,主要hook住UIViewController
的viewWillAppear:
和 viewWillDisappear:
的两个方法,获取执行这两个方法的时间差,就能够获取停留时长 ,给UIViewController
建一个Category
实现方法code
#import "ViewController+time.h"
#import "SMHook.h"
@implementation UIViewController (logger)
+(void)initialize{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL fromSelector = @selector(viewWillAppear:);
SEL toSelector = @selector(hook_viewWillAppear:);
[SMHook hookClass:self fromSelector:fromSelector toSelector:toSelector];
SEL fromDisappear = @selector(viewWillDisappear:);
SEL toDisappear = @selector(hook_viewWillDisAppear:);
[SMHook hookClass:self fromSelector:fromDisappear toSelector:toDisappear];
});
}
复制代码
hook方法的实现orm
-(void)hook_viewWillAppear:(BOOL)animated{
NSLog(@"hook viwe");
// 进来的时间 根据具体的业务去加时间的统计
[self comeIn];
[self hook_viewWillAppear:animated];
}
-(void)hook_viewWillDisAppear:(BOOL)animated{
// 出去的时间 统计方法根据具体的业务加
[self comeOut];
[self hook_viewWillDisAppear:animated];
}
复制代码
注意: 在实现了 hook_viewWillAppear:
方法后,又调用了一遍 [self hook_viewWillAppear:animated]
;这里的IMP执行的是viewWillAppear:
方法,注意前面的对象
method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)
复制代码
方法,这样不会由于hook住viewWillAppear:
的实现,而影响了业务代码中,viewWillAppear:
内容的实现,并不会形成循环调用。事件
UIButton的点击事件的hook和UIViewController
的viewWillAppear:
的hook基本上同样, 这里面要hook的是:
// send the action. the first method is called for the event and is a point at which you can observe or override behavior. it is called repeately by the second.
- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;
复制代码
这个方法,在点击的时候会被调用。 不是
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
复制代码
这个方法。
给UIButton新建Category,主要有三步
#import "UIButton+logger.h"
#import "SMHook.h"
#import <objc/runtime.h>
@implementation UIButton (logger)
+(void)initialize{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL fromSelector = @selector(sendAction:to:forEvent:);
SEL toSelector = @selector(hook_sendAction:to:forEvent:);
[SMHook hookClass:self fromSelector:fromSelector toSelector:toSelector];
});
}
-(void)hook_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
[self insertAction:action to:target forEvent:event];
[self hook_sendAction:action to:target forEvent:event];
}
-(void)insertAction:(SEL)action to:(id)target forEvent:(UIEvent*)event{
NSString * actionName = NSStringFromSelector(action);
NSString * targetName = NSStringFromClass([target class]);
NSString *name = self.name;
UIView *targetView = (UIView *)target;
NSLog(@"%@",targetView);
NSLog(@"button name == %@ actionName == %@, targetName == %@",name, actionName,targetName);
// 缺乏获取view_path的方法
NSLog(@"viewPath %@",viewPath);
}
复制代码
这里有个关键,不单单是UIButton,而是任何你想Hook住对象的View_Path
每一个控件须要有惟一的标识,这样才能对其埋点进行分析,若是一个视图下有多个UIButton,这样就不能仅仅经过 actionName 和 targetName 对齐分析 有一种解决办法是经过视图的层级结构,树状结构来分析 以下:
ViewController[0]/UIView[0]/UITableView[0]/UITableViewCell[0:2]/UIButton[0]
复制代码
其中:
UITableViewCell
和 UICollectionViewCell
及相似的自定义组件,序号部分由两部分组成:section
和 row
,并以: 拼接。具体的实现:
+(NSString *)viewPath:(UIView *)currentView{
__block NSString *viewPath = @"";
for (UIView *view = currentView;view;view = view.superview) {
NSLog(@"%@",view);
if ([view isKindOfClass:[UICollectionViewCell class]]) {
// 是一个
UICollectionViewCell *cell = (UICollectionViewCell *)view;
UICollectionView *cv = (UICollectionView *)cell.superview;
NSIndexPath *indexPath = [cv indexPathForCell:cell];
NSString *className = NSStringFromClass([cell class]);
viewPath = [NSString stringWithFormat:@"%@[%ld:%ld]/%@",className,indexPath.section,indexPath.row,viewPath];
continue;
}
if ([view isKindOfClass:[UITableViewCell class]]) {
// 是一个
UITableViewCell *cell = (UITableViewCell *)view;
UITableView *tb = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tb indexPathForCell:cell];
NSString *className = NSStringFromClass([cell class]);
viewPath = [NSString stringWithFormat:@"%@[%ld:%ld]/%@",className,indexPath.section,indexPath.row,viewPath];
continue;
}
if ([view isKindOfClass:[UIView class]]) {
[view.superview.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj == view) {
NSString *className = NSStringFromClass([view class]);
viewPath = [NSString stringWithFormat:@"%@[%ld]/%@",className,idx,viewPath];
*stop = YES;
}
}];
}
UIResponder *responder = [view nextResponder];
if ([responder isKindOfClass:[UIViewController class]]) {
NSString *className = NSStringFromClass([responder class]);
viewPath = [NSString stringWithFormat:@"%@/%@",className,viewPath];
return viewPath;
}
}
return viewPath;
}
复制代码
感受不是最优解,哈哈, 以上能够拿到。一个元素在当前控制器的路径。能够以此进行数据分析。 以上结束。谢谢!