Commit 945921bf authored by 汪洋's avatar 汪洋

Merge branch 'zx/gmPhobos' into 'master'

Zx/gm phobos

See merge request !21
parents d69656b5 bb2ea48b
...@@ -97,6 +97,19 @@ ...@@ -97,6 +97,19 @@
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow; + (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow;
+ (void)track:(NSString *)eventId; + (void)track:(NSString *)eventId;
/**
* @brief 自定义事件,数量统计 7730 精准曝光.
*
* @param eventId 事件Id
* @attributes 参数
* @sendNow 是否实时发送,默认为NO
* @currentAPI 当前传过来的API
* @
*/
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventId currentAPI:(NSString *)currentAPI;
/** /**
* @author 翟国钧, 16-02-03 16:02:30 * @author 翟国钧, 16-02-03 16:02:30
* *
......
...@@ -263,7 +263,6 @@ static NSString *sdkVersion = @"110"; ...@@ -263,7 +263,6 @@ static NSString *sdkVersion = @"110";
[sharedClient sendArray:array cleanCacheRightNow:YES]; [sharedClient sendArray:array cleanCacheRightNow:YES];
} }
} }
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow{ + (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow{
NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes]; NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes];
if (sendNow) { if (sendNow) {
...@@ -275,6 +274,30 @@ static NSString *sdkVersion = @"110"; ...@@ -275,6 +274,30 @@ static NSString *sdkVersion = @"110";
} }
} }
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI {
[self track:eventId attributes:attributes sendNow:NO currentAPI:currentAPI];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
//超过一定数量的话,统一发送一次
if (array.count > PhobosShardCount) {
[sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:YES];
}
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI {
NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes];
if (sendNow) {
NSArray *array = @[dict];
// 实时发送的埋点,不能立即清楚缓存
[sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:NO];
}else{
[sharedClient save:dict];
}
}
+ (void)track:(NSString *)eventId currentAPI:(NSString *)currentAPI {
[Phobos track:eventId attributes:@{} currentAPI:currentAPI];
}
+ (void)trackJsEvent:(NSString *)jsonString{ + (void)trackJsEvent:(NSString *)jsonString{
@try { @try {
NSData *data = [jsonString dataUsingEncoding:NSUnicodeStringEncoding]; NSData *data = [jsonString dataUsingEncoding:NSUnicodeStringEncoding];
...@@ -453,6 +476,30 @@ static NSString *sdkVersion = @"110"; ...@@ -453,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方法负责发送,数据一旦转移到缓存区,就把原有的数据干掉。
...@@ -481,6 +528,29 @@ static NSString *sdkVersion = @"110"; ...@@ -481,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,25 +114,49 @@ ...@@ -114,25 +114,49 @@
+ (void)sendData:(NSData *)data success:(SendDataSuccessBlock)success { + (void)sendData:(NSData *)data success:(SendDataSuccessBlock)success {
@try { @try {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[Phobos sharedClient].serverAPI]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[Phobos sharedClient].serverAPI]];
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"]; [request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
request.HTTPBody = data; request.HTTPBody = data;
request.HTTPMethod = @"POST"; request.HTTPMethod = @"POST";
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { // sendAsynchronousRequest 在iOS9以后y被废除了
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response; NSURLSession *session = [NSURLSession sharedSession];
NSInteger code = res.statusCode; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (code == 200) { if (!error) {
//没有错误,返回正确;
if (success) { if (success) {
success(code); success(200);
} }
} }
}]; }];
[dataTask resume];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
phobosLog(exception); phobosLog(exception);
} }
} }
+ (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