Commit cc7598e1 authored by 朱璇's avatar 朱璇

解决 精准曝光实时埋点后 remove掉 所有的埋点缓存数据 的问题

parent 9e0e1988
...@@ -28,6 +28,10 @@ static NSString *sdkVersion = @"110"; ...@@ -28,6 +28,10 @@ static NSString *sdkVersion = @"110";
@property (strong, nonatomic) NSString *sessionId; @property (strong, nonatomic) NSString *sessionId;
/* 每一条埋点数据的物理ID,自增,生命周期和sessionId相同。特别注意:在sessionOver的时候,要把他置为0 */ /* 每一条埋点数据的物理ID,自增,生命周期和sessionId相同。特别注意:在sessionOver的时候,要把他置为0 */
@property (assign, nonatomic) NSInteger serialId; @property (assign, nonatomic) NSInteger serialId;
// 用来记录除serverAPI以外的API
@property (strong, nonatomic) NSMutableArray *APIArray;
@end @end
@implementation Phobos @implementation Phobos
...@@ -56,6 +60,7 @@ static NSString *sdkVersion = @"110"; ...@@ -56,6 +60,7 @@ static NSString *sdkVersion = @"110";
_serverAPI = @""; _serverAPI = @"";
_userType = [[NSMutableDictionary alloc] initWithCapacity:0]; _userType = [[NSMutableDictionary alloc] initWithCapacity:0];
_appVersion = [PhobosUtil getAppVersion]; _appVersion = [PhobosUtil getAppVersion];
_APIArray = [NSMutableArray array];
[self setupNotification]; [self setupNotification];
[self handleSessionStart]; [self handleSessionStart];
[self synchronizePhobosKey]; [self synchronizePhobosKey];
...@@ -276,7 +281,7 @@ static NSString *sdkVersion = @"110"; ...@@ -276,7 +281,7 @@ static NSString *sdkVersion = @"110";
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI { + (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI {
[self track:eventId attributes:attributes sendNow:NO currentAPI:currentAPI]; [self track:eventId attributes:attributes sendNow:NO currentAPI:currentAPI];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI]];
//超过一定数量的话,统一发送一次 //超过一定数量的话,统一发送一次
if (array.count > PhobosShardCount) { if (array.count > PhobosShardCount) {
[sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:YES]; [sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:YES];
...@@ -284,13 +289,14 @@ static NSString *sdkVersion = @"110"; ...@@ -284,13 +289,14 @@ static NSString *sdkVersion = @"110";
} }
+ (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 {
[sharedClient addNewApi:currentAPI]; // 记录新的API
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 currentAPI:currentAPI cleanCacheRightNow:NO]; [sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:NO];
}else{ }else{
[sharedClient save:dict]; [sharedClient save:dict currentAPI:currentAPI];
} }
} }
...@@ -443,6 +449,26 @@ static NSString *sdkVersion = @"110"; ...@@ -443,6 +449,26 @@ static NSString *sdkVersion = @"110";
[GMCache storeObjectAtDocumentPathWithkey:PhobosCacheKey object:dataArray]; [GMCache storeObjectAtDocumentPathWithkey:PhobosCacheKey object:dataArray];
} }
/**
* @brief 保存数据到缓存层
*
* @param data 数据
*
*/
- (void)save:(NSDictionary *)data currentAPI:(NSString *)currentAPI
{
if (_logEnabled) {
phobosLog([NSString stringWithFormat:@"save dictionary: %@",data]);
}
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI]];
if (dataArray) {
[dataArray addObject:data];
}else{
dataArray = [NSMutableArray arrayWithObject:data];
}
[GMCache storeObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI] object:dataArray];
}
/** /**
* @brief 从缓存中获取数据,并发送 * @brief 从缓存中获取数据,并发送
* *
...@@ -453,6 +479,13 @@ static NSString *sdkVersion = @"110"; ...@@ -453,6 +479,13 @@ static NSString *sdkVersion = @"110";
if (paramsArray.count>0) { if (paramsArray.count>0) {
[self sendArray:paramsArray cleanCacheRightNow:YES]; [self sendArray:paramsArray cleanCacheRightNow:YES];
} }
// 其他接口的埋点
for (NSString *newApi in self.APIArray) {
NSArray *paramsArray = [GMCache fetchObjectAtDocumentPathWithkey:[PhobosUtil MD5String:newApi]];
if (paramsArray.count>0) {
[self sendArray:paramsArray currentAPI:newApi cleanCacheRightNow:YES];
}
}
} }
/* /*
...@@ -484,7 +517,8 @@ static NSString *sdkVersion = @"110"; ...@@ -484,7 +517,8 @@ static NSString *sdkVersion = @"110";
从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉 从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉
*/ */
- (void)sendArrayWithCurrentAPI:(NSString *)currentAPI { - (void)sendArrayWithCurrentAPI:(NSString *)currentAPI {
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosExpusureTempCacheKey]; NSString *PhobosTempCacheKeyStr = [PhobosUtil MD5String:[PhobosUtil MD5String:currentAPI]]; // 两次加密作为key值,和缓存的key值作区分
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr];
if (_logEnabled) { if (_logEnabled) {
NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray options:NSJSONWritingPrettyPrinted error:nil]; NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
...@@ -496,7 +530,7 @@ static NSString *sdkVersion = @"110"; ...@@ -496,7 +530,7 @@ static NSString *sdkVersion = @"110";
if (compressedData) { if (compressedData) {
[PhobosUtil sendData:compressedData currentAPI:currentAPI success:^(NSInteger code) { [PhobosUtil sendData:compressedData currentAPI:currentAPI success:^(NSInteger code) {
phobosLog(@"✈ ---------- ✈ data arrived Mars"); phobosLog(@"✈ ---------- ✈ data arrived Mars");
[GMCache removeObjectAtDocumentPathWithkey:PhobosExpusureTempCacheKey]; [GMCache removeObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr];
}]; }];
} }
} }
...@@ -538,18 +572,18 @@ static NSString *sdkVersion = @"110"; ...@@ -538,18 +572,18 @@ static NSString *sdkVersion = @"110";
- (void)sendArray:(NSArray *)array currentAPI:(NSString *)currentAPI cleanCacheRightNow:(BOOL)clean { - (void)sendArray:(NSArray *)array currentAPI:(NSString *)currentAPI cleanCacheRightNow:(BOOL)clean {
@try { @try {
//1.获取缓存区的数据,把新数据追加进去 //1.获取缓存区的数据,把新数据追加进去
NSString *PhobosTempCacheKeyStr = [PhobosUtil MD5String:[PhobosUtil MD5String:currentAPI]]; // 两次加密作为key值,和缓存的key值作区分
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosExpusureTempCacheKey]; NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr];
if (dataArray) { if (dataArray) {
[dataArray addObjectsFromArray:array]; [dataArray addObjectsFromArray:array];
}else{ }else{
dataArray = [NSMutableArray arrayWithArray:array]; dataArray = [NSMutableArray arrayWithArray:array];
} }
[GMCache storeObjectAtDocumentPathWithkey:PhobosExpusureTempCacheKey object:dataArray]; [GMCache storeObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr object:dataArray];
//2.把缓存区的数据发送给服务器 //2.把缓存区的数据发送给服务器
[self sendArrayWithCurrentAPI:currentAPI]; [self sendArrayWithCurrentAPI:currentAPI];
//3.把原有的数据删除 //3.把原有的数据删除
// [GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey]; [GMCache removeObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI]];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
phobosLog(exception); phobosLog(exception);
...@@ -573,4 +607,13 @@ static NSString *sdkVersion = @"110"; ...@@ -573,4 +607,13 @@ static NSString *sdkVersion = @"110";
}); });
} }
- (void)addNewApi:(NSString *)api {
for (NSString *item in self.APIArray) {
if ([api isEqualToString:item]) {
break;
}
[self.APIArray addObject:api];
}
}
@end @end
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#define PhobosEndTime @"PhobosEndTime" //记录APP退出|退到后台时的时间戳 #define PhobosEndTime @"PhobosEndTime" //记录APP退出|退到后台时的时间戳
#define PhobosCacheKey @"PhobosCacheKey" //存放持久化埋点数据的key #define PhobosCacheKey @"PhobosCacheKey" //存放持久化埋点数据的key
#define PhobosTempCacheKey @"PhobosTempCacheKey" //临时存放待发送埋点数据的key #define PhobosTempCacheKey @"PhobosTempCacheKey" //临时存放待发送埋点数据的key
#define PhobosExpusureTempCacheKey @"PhobosExpusureTempCacheKey" //临时存放待发送埋点数据的key(精准曝光)
#define PhobosShardCount 50 //收集数据分段发送的个数 #define PhobosShardCount 50 //收集数据分段发送的个数
......
...@@ -80,7 +80,10 @@ typedef void (^SendDataSuccessBlock)(NSInteger code); ...@@ -80,7 +80,10 @@ typedef void (^SendDataSuccessBlock)(NSInteger code);
+ (NSString *)deviceRunTime; + (NSString *)deviceRunTime;
/**
* MD5加密
*/
+ (NSString *)MD5String:(NSString *)str;
@end @end
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#import <mach/machine.h> #import <mach/machine.h>
#import "sys/utsname.h" #import "sys/utsname.h"
#import "Phobos.h" #import "Phobos.h"
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h>
#define IOS_CELLULAR @"pdp_ip0" #define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0" #define IOS_WIFI @"en0"
...@@ -427,4 +429,21 @@ ...@@ -427,4 +429,21 @@
return [NSString stringWithFormat:@"%ld", uptime]; return [NSString stringWithFormat:@"%ld", uptime];
} }
+ (NSString *)MD5String:(NSString *)str
{
if(str == nil || [str length] == 0)
return nil;
const char *value = [str UTF8String];
unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(value, (uint32_t)strlen(value), outputBuffer);
NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
[outputString appendFormat:@"%02x",outputBuffer[count]];
}
return outputString;
}
@end @end
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