因爲咱們的業務愈來愈重,而且對於數據的要求也愈來愈高,因此在業務代碼中須要嵌入大量的數據上報代碼,並且大部分還都是膠水代碼。web
因此爲了提升人效,下降耦合,精簡業務代碼等目的,終於!我重構了這部分代碼。bash
而外部使用方法十分簡單,則能夠總結爲以下:session
例如:框架
//使用宏初始化model
YGTrackingModel(YGTrackingPlayActionModel)
//給model賦值業務須要的數據
ygTrackingModel.plan = self.plan;
ygTrackingModel.course = self.course;
ygTrackingModel.playTime = self.actualPlayingTime;
ygTrackingModel.actionType = YGTrackingPlayActionTypeFinish;
//發送事件
YGTrackingGo
複製代碼
外部調用只須要關心和業務有關的參數便可,其餘事情都被封裝在model中進行async
#import <Foundation/Foundation.h>
#import "SensorsAnalyticsSDK.h"
#import <ShareSDK/ShareSDK.h>
#import "YGTrackingEnum.h"
#import "TalkingData.h"
#pragma mark - model.h
#import "YGTrackingClickSearchResultModel.h"
#import "YGTrackingShareEndActionModel.h"
#import "YGTrackingEvaluateActionModel.h"
#import "YGTrackingDownloadActionModel.h"
#import "YGTrackingClickGeneralModel.h"
#import "YGTrackingWatchActionModel.h"
#import "YGTrackingShareActionModel.h"
#import "YGTrackingPageGeneralModel.h"
#import "YGTrackingPlayActionModel.h"
#import "YGTrackingShareModel.h"
#import "YGTrackingOldAdModel.h"
#import "YGTrackVideoAdModel.h"
#import "YGTrackingDealModel.h"
#import "YGTrackingBaseModel.h"
#import "YGTrackingPostModel.h"
#import "YGTrackingAdModel.h"
/// 初始化model的宏
#define YGTrackingModel(A) A *ygTrackingModel = [[A alloc]init];
/// 發送model事件的宏
#define YGTrackingGo [YGTracking trackModel:ygTrackingModel];
@interface YGTracking : NSObject
/**
初始化上報SDK
*/
+ (void)registTrackSDK;
/**
神策聯通webview中的上報
*/
+ (BOOL)showUpWebView:(id)webView WithRequest:(NSURLRequest *)request;
/**
發送model事件
*/
+ (void)trackModel:(YGTrackingBaseModel *)model;
/**
發送普通事件
*/
+ (void)trackEvent:(NSString *)event property:(NSDictionary *)property supportPlatform:(YGAdTrackingPlatform)supportPlatform;
@end
複製代碼
#import "YGTracking.h"
#import "NSString+YGAddition.h"
@implementation YGTracking
/**
初始化上報SDK
*/
+ (void)registTrackSDK {
// TalkingData
[TalkingData backgroundSessionEnabled];
[TalkingData sessionStarted:kYGTalkingDataKey withChannelId:@"AppStore"];
[TalkingData setLogEnabled:NO];
// 神策SDK初始化
[SensorsAnalyticsSDK sharedInstanceWithServerURL:kYGSensorsReportURL
andDebugMode:SensorsAnalyticsDebugOff];
//神策註冊公共屬性
[[SensorsAnalyticsSDK sharedInstance] registerSuperProperties:@
{
@"PlatformType": @"iOS"
}];
// 神策 打開自動採集, 並指定追蹤哪些 AutoTrack 事件
[[SensorsAnalyticsSDK sharedInstance] enableAutoTrack:SensorsAnalyticsEventTypeAppStart];
[[SensorsAnalyticsSDK sharedInstance]trackInstallation:@"AppInstall"];
//神策 打通H5
[[SensorsAnalyticsSDK sharedInstance] enableLog:NO];
NSLog(@"神策版本:%@", [[SensorsAnalyticsSDK sharedInstance]libVersion]);
}
/**
神策聯通webview中的上報
*/
+ (BOOL)showUpWebView:(id)webView WithRequest:(NSURLRequest *)request {
return [[SensorsAnalyticsSDK sharedInstance]showUpWebView:webView WithRequest:request];
}
/**
發送model事件
*/
+ (void)trackModel:(YGTrackingBaseModel *)model {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *event = [model enventName];
NSDictionary *property = [model trackParam];
if (![event formatNull]
|| !IS_DICTIONARY(property)) {
return;
}
if ([model supportPlatform] & YGAdTrackingPlatformSensor) {
[[SensorsAnalyticsSDK sharedInstance]track:event withProperties:property];
}
if ([model supportPlatform] & YGAdTrackingPlatformTD) {
[TalkingData trackEvent:event label:@"" parameters:property];
}
});
}
/**
發送普通事件
*/
+ (void)trackEvent:(NSString *)event property:(NSDictionary *)property supportPlatform:(YGAdTrackingPlatform)supportPlatform {
if (![event formatNull]
|| !IS_DICTIONARY(property)) {
return;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (supportPlatform & YGAdTrackingPlatformSensor) {
[[SensorsAnalyticsSDK sharedInstance]track:event withProperties:property];
}
if (supportPlatform & YGAdTrackingPlatformTD) {
[TalkingData trackEvent:event label:@"" parameters:property];
}
});
}
@end
複製代碼
#import <Foundation/Foundation.h>
@interface YGTrackingBaseModel : NSObject
/**
支持上報平臺的類型
*/
- (YGAdTrackingPlatform)supportPlatform;
/**
返回事件名
*/
- (NSString *)enventName;
/**
返回事件所需參數dic
*/
- (NSDictionary *)trackParam;
@end
複製代碼
#import "YGTrackingBaseModel.h"
@implementation YGTrackingBaseModel
/**
支持上報平臺的類型
*/
- (YGAdTrackingPlatform)supportPlatform {
return 0;
}
/**
返回事件名
*/
- (NSString *)enventName {
return @"";
}
/**
返回事件所需參數dic
*/
- (NSDictionary *)trackParam {
return nil;
}
@end
複製代碼
#import "YGTrackingBaseModel.h"
@interface YGTrackingAdModel : YGTrackingBaseModel
/// 表現類型 YGAdTrackingModelType 彈框/開屏。。。
@property(nonatomic, assign)YGAdTrackingModelType modelType;
/// 行爲類型 YGAdTrackingType 點擊/展現/關閉
@property(nonatomic, assign)YGAdTrackingType trackingType;
/// 廣告位id
@property(nonatomic, copy)NSString *adID;
/// 廣告物料類型(跳轉到哪裏)
@property(nonatomic, assign)YGOpenLinkType adType;
/// 廣告來源類型(美數/億動。。。)
@property(nonatomic, assign)YGAdResourceType adResourceType;
/// 推薦廣告所在頁面
@property(nonatomic, assign)NSInteger pageID;
/// 到達該頁面的訓練id/產品id
@property(nonatomic, copy)NSString *referID;
/// 廣告位中的廣告id
@property(nonatomic, copy)NSString *contentID;
/// 位置
@property(nonatomic, assign)NSInteger index;
@end
複製代碼
#import "YGTrackingAdModel.h"
@implementation YGTrackingAdModel
/**
支持上報平臺的類型
*/
- (YGAdTrackingPlatform)supportPlatform {
if (self.trackingType == YGAdTrackingTypeClick) {
return YGAdTrackingPlatformSensor | YGAdTrackingPlatformTD;
}
return YGAdTrackingPlatformTD;
}
/**
返回事件名
*/
- (NSString *)enventName {
if (self.modelType == YGAdTrackingTypeRecommend) {
if (self.trackingType == YGAdTrackingTypeDisplay) {
return @"view_operation_recommend";
}
if (self.trackingType == YGAdTrackingTypeClick) {
return @"click_operation_recommend";
}
if (self.trackingType == YGAdTrackingTypeClose) {
return @"close_operation_recommend";
}
}
if (self.modelType == YGAdTrackingTypeOpenScreen) {
if (self.trackingType == YGAdTrackingTypeDisplay) {
return @"view_operation_open_screen";
}
if (self.trackingType == YGAdTrackingTypeClick) {
return @"click_operation_open_screen";
}
}
if (self.modelType == YGAdTrackingTypeTips) {
if (self.trackingType == YGAdTrackingTypeDisplay) {
return @"view_operation_tips";
}
if (self.trackingType == YGAdTrackingTypeClick) {
return @"click_operation_tips";
}
if (self.trackingType == YGAdTrackingTypeClose) {
return @"close_operation_tips";
}
}
if (self.modelType == YGAdTrackingTypeTips) {
if (self.trackingType == YGAdTrackingTypeDisplay) {
return @"view_operation_banner";
}
if (self.trackingType == YGAdTrackingTypeClick) {
return @"click_operation_banner";
}
if (self.trackingType == YGAdTrackingTypeClose) {
return @"close_operation_banner";
}
}
return @"";
}
/**
返回事件所需參數dic
*/
- (NSDictionary *)trackParam {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:self.adID forKey:@"ad_id"];
[params setValue:@(self.adType) forKey:@"ad_type"];
[params setValue:@(self.adResourceType) forKey:@"ad_resource_type"];
[params setValue:@(self.pageID) forKey:@"page_id"];
[params setValue:[NSString stringWithFormat:@"%@_%@", @(self.pageID), self.referID] forKey:@"refer_id"];
[params setValue:self.contentID forKey:@"contentid"];
[params setValue:@(self.index) forKey:@"frame"];
return params;
}
@end
複製代碼
後續,不管是時間增改字段,仍是事件名替換,或者同一源行爲增長事件,都直接修改相應的model便可,對業務代碼的修改較少,尤爲是像課程或者廣告的相關事件,自己數據上報model接收的參數就是課程model或者廣告自身model,業務代碼幾乎不須要更改。ide
而且,不管是增改線程,更換數據上報平臺等等操做,均可以集中進行。函數
相信,後面咱們數據上報工做會愈來愈高效清晰,爲咱們數據驅動做爲業務行爲的同時,也大大提高了此部分的開發效率ui