从ios友盟统计说开去

来源:互联网 发布:淘宝买酒靠谱吗 编辑:程序博客网 时间:2024/06/07 06:09

友盟统计埋点,不应该和业务做强相关。
这是统计的思想.

不和业务做强相关,自然需要考虑将埋点埋在哪里。

需要统计的地方
1.控件的点击。
2.页面的使用次数,时间。
3.选项是否配置成功。

页面的进出时间

viewWillAppear 和 viewWillDisappear有关

分别代表进入页面和离开页面

那么怎么给每个页面做监听呢

先考虑写在基类控制器里 发送

NSStringFromClass([self class])

基类控制器写了可以满足大部分的需求

但是很多关键页面都重写了

viewWillAppear 和 viewWillDisappear

这两个方法,难道每个页面都要去写?

能不能每个页面先在这两个方法的时候,

先执行统计的代码,然后如果有其他方法

再执行其他的方法?

这里就想到了OC的Runtime的一个方法交换

可以在viewWillAppear 时候先做一次方法发送

然后再走业务本身的逻辑。

为了不影响原有的代码,

增加一个category

然后是具体代码了

#import <UIKit/UIKit.h>#import <objc/runtime.h>@interface UIViewController (swizzlingLogCurrentVC)@end
#import "UIViewController+swizzlingLogCurrentVC.h"#import "YYBCommonDefines.h"#import "UMMobClick/MobClick.h"@implementation UIViewController (swizzlingLogCurrentVC)+ (void)load {    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        ReplaceMethod([self class], @selector(viewWillAppear:), @selector(yyb_viewWillAppear:));        ReplaceMethod([self class], @selector(viewDidDisappear:), @selector(yyb_viewDidDisappear:));    });}- (void)yyb_viewWillAppear:(BOOL)animated{    [self yyb_viewWillAppear:animated];//    NSLog(@"%@ viewWillAppear",[self class]);    [MobClick beginLogPageView:NSStringFromClass([self class])];}- (void)yyb_viewDidDisappear:(BOOL)animated {    [self yyb_viewDidDisappear:animated];//    NSLog(@"%@ viewDidDisappear",[self class]);    [MobClick endLogPageView:NSStringFromClass([self class])];}@end
CG_INLINE voidReplaceMethod(Class _class, SEL _originSelector, SEL _newSelector) {    Method oriMethod = class_getInstanceMethod(_class, _originSelector);//类的实例方法    Method newMethod = class_getInstanceMethod(_class, _newSelector);    BOOL isAddedMethod = class_addMethod(_class, _originSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));    if (isAddedMethod) {        class_replaceMethod(_class, _newSelector, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));    } else {        method_exchangeImplementations(oriMethod, newMethod);    }}

把这段代码 加到基础类控制器,
这些东西就完成了。

同理
点击事件应该对

- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;

这个方法进行方法交换。可以在这里做防止重复点击的代码

至于统计事件的成功率
只要在网络请求发送前和发送回应的时候做统计就好了

发送前 记录请求发送 请求回应code = 0 时 请求发送成功

这就是全部的思路了

其余就是技术上的实现而已。

这样可以少量的代码并不改变原有代码的结构下,
实现所有的埋点统计,非常有效。

原创粉丝点击