开发者根据业务需求在相关的模块进行设置id,SDK通过运行时进行数据收集
Runtime
页面统计
关键代码
-(void)setPageEvent:(id)pageEvent;
使用方法
- (void)viewDidLoad {
[super viewDidLoad];
[self setPageEvent:@{@"id":@"window_10101_8383"}];
}
按钮统计
关键代码
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents event:(id)event;
使用方法,event参数与后端定义的统一@{@"id":@"message_111_222"},
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtn.bounds = CGRectMake(0, 0, 33, 33);
[rightBtn setImageEdgeInsets:UIEdgeInsetsMake(7, 14, 7, 0)];
[rightBtn addTarget:self action:@selector(rightBarButtonClick:) forControlEvents:UIControlEventTouchUpInside event:@{@"id":@"message"}];
[rightBtn setImage:[UIImage imageNamed:@"window_message"] forState:UIControlStateNormal];
UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
AOP
页面统计
关键代码
+ (void)trackViewAppear{
NSString *path = [[NSBundle mainBundle] pathForResource:@"ICPageList" ofType:@"plist"];
NSDictionary *pageStatisticsDict = [[NSDictionary alloc] initWithContentsOfFile:path];
[UIViewController aspect_hookSelector:@selector(viewWillAppear:)
withOptions:AspectPositionBefore
usingBlock:^(id<AspectInfo> info){
//用户统计代码写在此处
NSString *className = NSStringFromClass([info.instance class]);
ICLog(@"className-->%@",className);
NSString *ID = pageStatisticsDict[className] == nil ? className : pageStatisticsDict[className];
// 通过className从ICPageList匹配到对应的id上传到服务器
[ICClickRecord beginLogPageView:@{@"ID":ID}];
}
error:NULL];
[UIViewController aspect_hookSelector:@selector(viewWillDisappear:)
withOptions:AspectPositionBefore
usingBlock:^(id<AspectInfo> info){
//用户统计代码写在此处
NSString *className = NSStringFromClass([info.instance class]);
NSLog(@"className-->%@",className);
NSString *ID = pageStatisticsDict[className] == nil ? className : pageStatisticsDict[className];
// 通过className从ICPageList匹配到对应的id上传到服务器
[ICClickRecord endLogPageView:@{@"ID":ID}];
}
error:NULL];
}
使用方法
在ICPageList.plist文件中配置需要统计的页面类名以及页面ID
方法统计
关键代码
+ (void)trackBttonEvent{
__weak typeof(self) ws = self;
//设置事件统计
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//读取配置文件,获取需要统计的事件列表
NSString *path = [[NSBundle mainBundle] pathForResource:@"ICEventList" ofType:@"plist"];
NSDictionary *eventStatisticsDict = [[NSDictionary alloc] initWithContentsOfFile:path];
for (NSString *classNameString in eventStatisticsDict.allKeys) {
//使用运行时创建类对象
const char * className = [classNameString UTF8String];
//从一个字串返回一个类
Class newClass = objc_getClass(className);
NSArray *pageEventList = [eventStatisticsDict objectForKey:classNameString];
for (NSDictionary *eventDict in pageEventList) {
//获取事件方法名称
NSString *eventMethodName = eventDict[@"MethodName"];
SEL seletor = NSSelectorFromString(eventMethodName);
NSString *eventId = eventDict[@"EventId"];
[ws trackEventWithClass:newClass selector:seletor eventID:eventId];
[ws trackParameterEventWithClass:newClass selector:seletor eventID:eventId];
[ws trackTableViewEventWithClass:newClass selector:seletor eventID:eventId];
}
}
});
}
使用方法
在ICEventList.plist文件中配置需要统计的方法名称以及方法ID
项目中部分特殊业务逻辑可单独调用ICClickRecord中的event方法进行事件统计
统计数据存储模块
事件触发-->存储
ICClickRecord
+ (void)event:(id)info {
ICLog(@"发送到服务器%@", info);
ICUserBehaviorInfo *event = [ICUserBehaviorInfo mj_objectWithKeyValues:info];
event.time = [NSDate longFromDate:[NSDate date]];
[event saveToDB];
// 友盟统计
[MobClick event:event.ID];
}
统计数据上报模块
启动同步
每次启动时发送缓存的数据
- (void)uploadUserBehaviorRecod:(NSDictionary *)recodDic
success:(HttpSuccessCompletionHandle)success
failure:(HttpFailureCompletionHandle)failure {
NSString *url = [self appendRequestBaseURL:@""];
[ICNetworkHelper POST:url parameters:recodDic success:^(id responseObject) {
success(responseObject);
} failure:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
定时同步
应用在线时定时向服务端发送数据
/** 当reportPolicy == SEND_INTERVAL 时设定log发送间隔
@param second 单位为秒,最小90秒,最大86400秒(24hour).
@return void.
*/
+ (void)setLogSendInterval:(double)second;
配置解析模块(下个版本)
应用启动时(动态配置获取业务数据的类型,配置上报的规则和地址等)