Commit 2c98ab14 authored by 叶凤鸣's avatar 叶凤鸣

Merge branch 'master' into yefengming/GMPhobos

parents 9a6f952b 945921bf
......@@ -97,6 +97,19 @@
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow;
+ (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
*
......
......@@ -263,7 +263,6 @@ static NSString *sdkVersion = @"110";
[sharedClient sendArray:array cleanCacheRightNow:YES];
}
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow{
NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes];
if (sendNow) {
......@@ -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{
@try {
NSData *data = [jsonString dataUsingEncoding:NSUnicodeStringEncoding];
......@@ -326,6 +349,10 @@ static NSString *sdkVersion = @"110";
[dict setObject:page.currentTabName ? : @"" forKey:@"referrer_tab_name"];
NSAssert(page.inTime.length > 0, @"页面显示时间不能为空!");
[Phobos track:@"page_view" attributes:dict];
if (page.inTime.length > 0) {
// 页面显示时间为空时不记录页面pv事件
[Phobos track:@"page_view" attributes:dict];
}
}
@catch (NSException *exception) {
phobosLog(exception);
......@@ -454,6 +481,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方法负责发送,数据一旦转移到缓存区,就把原有的数据干掉。
......@@ -482,6 +533,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
- (void)catchNullForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
......
......@@ -31,6 +31,14 @@ typedef void (^SendDataSuccessBlock)(NSInteger code);
*/
+ (void)sendData:(NSData *)data success:(SendDataSuccessBlock)success;
/**
* @brief 上传数据
*
*
* @7735 精准曝光
*/
+ (void)sendData:(NSData *)data currentAPI:(NSString *)currentAPI success:(SendDataSuccessBlock)success;
+ (NSString *)currentTime;
+ (NSString *)getAppVersion;
+ (BOOL)isNonEmpty:(NSString *)string;
......
......@@ -114,25 +114,49 @@
+ (void)sendData:(NSData *)data success:(SendDataSuccessBlock)success {
@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.HTTPBody = data;
request.HTTPMethod = @"POST";
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
NSInteger code = res.statusCode;
if (code == 200) {
// 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(code);
success(200);
}
}
}];
[dataTask resume];
}
@catch (NSException *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格式数据
......
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