Commit bb2ea48b authored by 朱璇's avatar 朱璇

修改精准曝光方法

parent e280b83b
...@@ -77,11 +77,6 @@ ...@@ -77,11 +77,6 @@
*/ */
@property (copy, nonatomic) NSString *serverAPI; @property (copy, nonatomic) NSString *serverAPI;
/**
数据接收的服务器API(新开的接口)7730精准曝光
*/
@property (copy, nonatomic) NSString *currentServerAPI;
/** /**
从主项目获取当前显示的controller 从主项目获取当前显示的controller
*/ */
......
...@@ -279,17 +279,16 @@ static NSString *sdkVersion = @"110"; ...@@ -279,17 +279,16 @@ static NSString *sdkVersion = @"110";
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
//超过一定数量的话,统一发送一次 //超过一定数量的话,统一发送一次
if (array.count > PhobosShardCount) { if (array.count > PhobosShardCount) {
[sharedClient sendArray:array cleanCacheRightNow:YES]; [sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:YES];
} }
} }
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI { + (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI {
[Phobos sharedClient].currentServerAPI = currentAPI;
NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes]; NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes];
if (sendNow) { if (sendNow) {
NSArray *array = @[dict]; NSArray *array = @[dict];
// 实时发送的埋点,不能立即清楚缓存 // 实时发送的埋点,不能立即清楚缓存
[sharedClient sendArray:array cleanCacheRightNow:NO]; [sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:NO];
}else{ }else{
[sharedClient save:dict]; [sharedClient save:dict];
} }
...@@ -477,6 +476,30 @@ static NSString *sdkVersion = @"110"; ...@@ -477,6 +476,30 @@ static NSString *sdkVersion = @"110";
} }
} }
/*
从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉
*/
- (void)sendArrayWithCurrentAPI:(NSString *)currentAPI {
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
if (_logEnabled) {
NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
phobosLog([NSString stringWithFormat:@"array prepare to fly --✈: %@", jsonString]);
}
@try {
NSData *JSON = [PhobosUtil encodeJSON:dataArray];
NSData *compressedData = [PhobosUtil compressData:JSON];
if (compressedData) {
[PhobosUtil sendData:compressedData currentAPI:currentAPI success:^(NSInteger code) {
phobosLog(@"✈ ---------- ✈ data arrived Mars");
[GMCache removeObjectAtDocumentPathWithkey:PhobosTempCacheKey];
}];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
/** /**
该方法有改动,现在的逻辑是:当前方法只接受发送的请求,然后把数据转存到另一个缓存区, 该方法有改动,现在的逻辑是:当前方法只接受发送的请求,然后把数据转存到另一个缓存区,
让sendArray方法负责发送,数据一旦转移到缓存区,就把原有的数据干掉。 让sendArray方法负责发送,数据一旦转移到缓存区,就把原有的数据干掉。
...@@ -505,6 +528,29 @@ static NSString *sdkVersion = @"110"; ...@@ -505,6 +528,29 @@ static NSString *sdkVersion = @"110";
} }
} }
/**
上边方法新加了一个API字段
*/
- (void)sendArray:(NSArray *)array currentAPI:(NSString *)currentAPI cleanCacheRightNow:(BOOL)clean {
@try {
//1.获取缓存区的数据,把新数据追加进去
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
if (dataArray) {
[dataArray addObjectsFromArray:array];
}else{
dataArray = [NSMutableArray arrayWithArray:array];
}
[GMCache storeObjectAtDocumentPathWithkey:PhobosTempCacheKey object:dataArray];
//2.把缓存区的数据发送给服务器
[self sendArrayWithCurrentAPI:currentAPI];
//3.把原有的数据删除
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
#pragma mark - helpers #pragma mark - helpers
- (void)catchNullForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes { - (void)catchNullForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes {
dispatch_async(dispatch_get_global_queue(0, 0), ^{ dispatch_async(dispatch_get_global_queue(0, 0), ^{
......
...@@ -31,6 +31,14 @@ typedef void (^SendDataSuccessBlock)(NSInteger code); ...@@ -31,6 +31,14 @@ typedef void (^SendDataSuccessBlock)(NSInteger code);
*/ */
+ (void)sendData:(NSData *)data success:(SendDataSuccessBlock)success; + (void)sendData:(NSData *)data success:(SendDataSuccessBlock)success;
/**
* @brief 上传数据
*
*
* @7735 精准曝光
*/
+ (void)sendData:(NSData *)data currentAPI:(NSString *)currentAPI success:(SendDataSuccessBlock)success;
+ (NSString *)currentTime; + (NSString *)currentTime;
+ (NSString *)getAppVersion; + (NSString *)getAppVersion;
+ (BOOL)isNonEmpty:(NSString *)string; + (BOOL)isNonEmpty:(NSString *)string;
......
...@@ -114,9 +114,7 @@ ...@@ -114,9 +114,7 @@
+ (void)sendData:(NSData *)data success:(SendDataSuccessBlock)success { + (void)sendData:(NSData *)data success:(SendDataSuccessBlock)success {
@try { @try {
NSString *url = [Phobos sharedClient].currentServerAPI.length > 0 ? [Phobos sharedClient].currentServerAPI : [Phobos sharedClient].serverAPI; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[Phobos sharedClient].serverAPI]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[Phobos sharedClient].currentServerAPI = @""; // 用完置为空 防止下次判断无效出现错误
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"]; [request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
request.HTTPBody = data; request.HTTPBody = data;
request.HTTPMethod = @"POST"; request.HTTPMethod = @"POST";
...@@ -137,6 +135,28 @@ ...@@ -137,6 +135,28 @@
} }
} }
+ (void)sendData:(NSData *)data currentAPI:(NSString *)currentAPI success:(SendDataSuccessBlock)success {
@try {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:currentAPI]];
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
request.HTTPBody = data;
request.HTTPMethod = @"POST";
// sendAsynchronousRequest 在iOS9以后y被废除了
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
//没有错误,返回正确;
if (success) {
success(200);
}
}
}];
[dataTask resume];
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
/** /**
* @brief 将对象转成JSON格式数据 * @brief 将对象转成JSON格式数据
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment