iOS开发之友盟埋点

这两天在对项目进行友盟埋点,顺便对这块进行了一个详细的了解友盟集成就不多说了,百度一下教程一堆,什么你不知道怎么百度?
iOS开发之友盟埋点_第1张图片
image.png

如果只是对页面进行埋点的话,可以使用Runtime进行埋点
首先写一个UIViewController的分类方法
然后写一个替换方法

+ (void)ps_swizzleSelector:(SEL)originalSelector newSelector:(SEL)newSelector {
    Method originalMethod = class_getInstanceMethod(self, originalSelector);
    Method newMethod = class_getInstanceMethod(self, newSelector);
    
    BOOL methodAdded = class_addMethod([self class], originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (methodAdded) {
        class_replaceMethod([self class], newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }
    else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

然后对ViewWillAppear 和ViewWillDisappear方法进行一个提换

+ (void)load {
    
    SEL orgAppear = @selector(viewWillAppear:);
    SEL newAppear = @selector(ps_viewWillAppear:);
    [self ps_swizzleSelector:orgAppear newSelector:newAppear];
    
    SEL orgDisAppear = @selector(viewWillDisappear:);
    SEL newDisAppear = @selector(ps_viewWillDisappear:);
    [self ps_swizzleSelector:orgDisAppear newSelector:newDisAppear];
}

- (void)ps_viewWillAppear:(BOOL)animated {
    [self ps_viewWillAppear:animated];
    Class cls = [self class];
    if (![cls isSubclassOfClass:[UINavigationController class]] && ![cls isSubclassOfClass:[UITabBarController class]] ) {
        [MobClick beginLogPageView:NSStringFromClass([self class])];
    }
   
}

- (void)ps_viewWillDisappear:(BOOL)animated {
    [self ps_viewWillDisappear:animated];
    Class cls = [self class];
     if (![cls isSubclassOfClass:[UINavigationController class]] && ![cls isSubclassOfClass:[UITabBarController class]] ) {
         [MobClick endLogPageView:NSStringFromClass([self class])];
     }
}

以上是对页面进行埋点,要想对用户行为做一个更为详细的埋点,就需要对单独点击事件进行埋点,这个需要手动对每个方法进行埋点,如果是安卓跟iOS两个端的话工作量就要乘以二了,这也是前端埋点的弊端所在

iOS开发之友盟埋点_第2张图片
image.png

建议写一个单独的埋点类,在类方法里调用友盟提供的方法,方便以后传参,给自己留条后路,关于埋点的一些总结这位大神做了一个总结,感兴趣的可以去看看
传送门

你可能感兴趣的:(iOS开发之友盟埋点)