Authored by hongyong.zhao

针对特殊情况修改

... ... @@ -9,7 +9,7 @@
Pod::Spec.new do |s|
s.name = "YHEventReport"
s.version = "1.0.5"
s.version = "1.0.6"
s.summary = "YHEventReport."
s.description = "无埋点SDK"
s.homepage = "http://git.yoho.cn/mobile/YH_EventReport"
... ...
... ... @@ -93,11 +93,50 @@
}
/**是否真正的开始展示界面了,直接调用self.view等属性会导致viewdidload加载,在一级界面也会有这种情况*/
- (BOOL)yher_isAcutuallyLoaded {
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController;
if ([[navigationController.viewControllers firstObject] isEqual:self]) {//要考虑应用启动时间等
return NO;
}
}
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabController = (UITabBarController *)rootViewController;
if ([tabController.viewControllers containsObject:self]) {
return NO;
}
}
if ([rootViewController isEqual:self]) {
return NO;
}
if (!self.presentingViewController && !self.navigationController) {//都没有,当做只初始化,还没有正式加载界面,在tabbarController里面的默认为一级界面
return NO;
}
if (self.navigationController && [[self.navigationController.viewControllers firstObject] isEqual:self]) {//如果存在UINavigationController并且此类为第一个则视为首页的界面
return NO;
}
return YES;
}
- (void)yher_viewDidLoad{
@try {
if (self.shouldTrack) {
[[YH_EventCollector sharedInstance] timeEventStartWithViewController:self];
[self performSelector:@selector(yher_checkVisiable) withObject:nil afterDelay:0];
if ( [self yher_isAcutuallyLoaded]) {
[[YH_EventCollector sharedInstance] timeEventStartWithViewController:self];
} else {
[[YH_EventCollector sharedInstance] timeEventIgnoreWithViewController:self];
}
// [self performSelector:@selector(yher_checkVisiable) withObject:nil afterDelay:0];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
... ... @@ -107,8 +146,12 @@
-(void)yher_viewWillAppear:(BOOL)animated {
@try {
if (self.shouldTrack && self.shouldResetStartLoadDate && !self.yher_hasReported) {
[[YH_EventCollector sharedInstance] timeEventStartWithViewController:self];
if (self.shouldTrack && !self.yher_hasReported) {
if ([[YH_EventCollector sharedInstance] timeEventIgnorContainsViewController:self]) {
[[YH_EventCollector sharedInstance] timeEventIgnoreRemoveWithViewController:self];
[[YH_EventCollector sharedInstance] timeEventStartWithViewController:self];
}
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
... ... @@ -119,14 +162,18 @@
-(void)yher_viewDidAppear:(BOOL)animated {
@try {
if (self.shouldTrack && !self.yher_hasReported) {
CFRunLoopRef mainrunloop = CFRunLoopGetMain();
CFStringRef runLoopMode = kCFRunLoopDefaultMode;
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
[[YH_EventCollector sharedInstance] timeEventEndWithViewController:self];
CFRunLoopRemoveObserver(mainrunloop, observer, runLoopMode);
});
CFRunLoopAddObserver(mainrunloop, observer, runLoopMode);
CFRelease(observer);
if (![self yher_isAcutuallyLoaded]) {//是首页的几个界面,因为会同时加载就不用主线程空闲
[[YH_EventCollector sharedInstance] timeEventEndWithViewController:self];
}else {
CFRunLoopRef mainrunloop = CFRunLoopGetMain();
CFStringRef runLoopMode = kCFRunLoopDefaultMode;
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
[[YH_EventCollector sharedInstance] timeEventEndWithViewController:self];
CFRunLoopRemoveObserver(mainrunloop, observer, runLoopMode);
});
CFRunLoopAddObserver(mainrunloop, observer, runLoopMode);
CFRelease(observer);
}
}
if ([YHEventReport sharedInstance].isAppCrashReportEnable && [YHEventReport sharedInstance].isAutoTrackEnabled) {
... ...
... ... @@ -16,6 +16,9 @@
+ (YH_EventCollector *)sharedInstance;
- (void)timeEventIgnoreWithViewController:(UIViewController *)viewController;
- (void)timeEventIgnoreRemoveWithViewController:(UIViewController *)viewController;
- (BOOL)timeEventIgnorContainsViewController:(UIViewController *)viewController;
- (void)timeEventStartWithViewController:(UIViewController *)viewController;
- (void)timeEventEndWithViewController:(UIViewController *)viewController;
... ...
... ... @@ -46,6 +46,7 @@ NS_INLINE NSString *YH_Objectkey(id anyObject) {
@property (nonatomic, strong) NSMutableArray *tabViewControllerArray;
/**页面调用链*/
@property (nonatomic, strong) NSMutableArray *pageChains;
@property (nonatomic, strong) NSMutableSet *ignoredViewControllers;
//@property (nonatomic, strong) NSLock *lock;
@end
... ... @@ -68,6 +69,7 @@ NS_INLINE NSString *YH_Objectkey(id anyObject) {
self.serialQueue = dispatch_queue_create([kYHEventReportQueue UTF8String], DISPATCH_QUEUE_SERIAL);
self.timedEvents = [NSMutableDictionary dictionary];
self.pageChains = [NSMutableArray array];
self.ignoredViewControllers = [NSMutableSet set];
// self.lock = [[NSLock alloc] init];
// self.lock.name = @"com.yoho.buy.EventCollector";
NSArray *tabAry= @[];
... ... @@ -191,7 +193,52 @@ NS_INLINE NSString *YH_Objectkey(id anyObject) {
}
}
- (double)eventElapsedTime:(NSString *)viewId accurateEndTime:(NSTimeInterval)endtime {
if (IsStrEmpty(viewId)) {
return 0;
}
__block double startTime = 0;
dispatch_sync(self.serialQueue, ^{
startTime = [self.timedEvents[viewId] doubleValue];
});
if (startTime <= 0) {
return 0;
} else {
return MAX((endtime - startTime), 0);
}
}
#pragma mark - viewcontroller performance
- (void)timeEventIgnoreWithViewController:(UIViewController *)viewController {
if (!viewController) {
return;
}
NSString *viewId = [NSString stringWithFormat:@"%@%p",[viewController class], viewController];
dispatch_sync(self.serialQueue, ^{
[self.ignoredViewControllers addObject:viewId];
});
}
- (void)timeEventIgnoreRemoveWithViewController:(UIViewController *)viewController {
if (!viewController) {
return;
}
NSString *viewId = [NSString stringWithFormat:@"%@%p",[viewController class], viewController];
[self.ignoredViewControllers removeObject:viewId];
}
- (BOOL)timeEventIgnorContainsViewController:(UIViewController *)viewController {
NSString *viewId = [NSString stringWithFormat:@"%@%p",[viewController class], viewController];
__block BOOL isContain = NO;
dispatch_sync(self.serialQueue, ^{
isContain = [self.ignoredViewControllers containsObject:viewId];
});
return isContain;
}
- (void)timeEventStartWithViewController:(UIViewController *)viewController{
NSParameterAssert(viewController);
if (!viewController) {
... ... @@ -218,6 +265,7 @@ NS_INLINE NSString *YH_Objectkey(id anyObject) {
return;
}
NSTimeInterval elapsedTime = 0;
NSTimeInterval endtimeStamp = [[NSDate date] timeIntervalSince1970];
if ([self isTabViewControllerString:NSStringFromClass([viewController class])]) {
UITabBarController *tabBarVC = [viewController yher_tabBarController];
double didSelectTime = [tabBarVC.tabBar.selectedItem.yh_didSelectTime doubleValue];
... ... @@ -225,13 +273,13 @@ NS_INLINE NSString *YH_Objectkey(id anyObject) {
return;
}
if (didSelectTime==0) {
elapsedTime = [self eventElapsedTime:viewController.yh_viewId];
elapsedTime = [self eventElapsedTime:viewController.yh_viewId accurateEndTime:endtimeStamp];
}else{
elapsedTime = [[NSDate date] timeIntervalSince1970] - didSelectTime;
elapsedTime = endtimeStamp - didSelectTime;
viewController.yher_hasReported = YES;
}
}else{
elapsedTime = [self eventElapsedTime:viewController.yh_viewId];
elapsedTime = [self eventElapsedTime:viewController.yh_viewId accurateEndTime:endtimeStamp];
}
if (elapsedTime==0) {
... ...