Commit 0d5257e4 authored by 乔金柱's avatar 乔金柱

Merge branch 'jz/master' into 'master'

还原 phobos 1.3.0 代码

See merge request !46
parents 2a751ef1 3746b2e9
......@@ -33,13 +33,12 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
*/
+ (Phobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId;
+ (instancetype) alloc __attribute__((deprecated));
- (instancetype) init __attribute__((deprecated));
+ (instancetype) new __attribute__((deprecated));
+ (instancetype)sharedClient;
+ (void)setSharedClient:(Phobos *)client;
#pragma mark - SDK配置
@property (class, readonly, strong) Phobos *sharedClient;
#pragma mark - SDK配置
// Phobos在处理业务端传递来的参数时会检查是否某个value为空,如果为空会调用这个block以通知业务层,业务层可以上报这个异常,以助解决问题
@property(nonatomic, copy) void (^captureNullExpection) (NSString *eventId, NSDictionary *info);
......@@ -134,12 +133,12 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
*
* @since 0.0.1
*/
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes;
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow;
+ (void)track:(NSString *)eventName;
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes;
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow;
+ (void)track:(NSString *)eventId;
/**
* @brief 自定义事件,数量统计 7730 精准曝光/数据链路.
* @brief 自定义事件,数量统计 7730 精准曝光.
*
* @param eventId 事件Id
* @attributes 参数
......@@ -147,9 +146,9 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
* @currentAPI 当前传过来的API
* @
*/
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI;
+ (void)track:(NSString *)eventName currentAPI:(NSString *)currentAPI;
+ (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
......@@ -184,48 +183,5 @@ typedef NS_ENUM (NSInteger, PhobosSigningType) {
*/
- (void)simulativePV:(NSString *)pageName businessId:(NSString *)bid referer:(NSString *)referer;
@end
@interface Phobos (UtilTest)
/** 获取所有非立即发送埋点数量 */
+ (NSInteger)normalPhobosCount;
/** 获取所有非立即发送埋点数据 */
+ (NSDictionary *)normalPhobosData;
/** 获取url的非立即发送埋点数量 */
+ (NSInteger)normalPhobosCountForURL:(NSString *)url;
/** 获取serverAPI的非立即发送埋点数据 */
+ (NSArray *)normalPhobosDataForServerAPI;
/** 获取url的非立即发送埋点数据 */
+ (NSArray *)normalPhobosDataForURL:(NSString *)url;
/** 获取所有立即发送埋点数据 */
+ (NSDictionary *)immediatelyPhobosData;
/** 获取serverAPI的立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosForServerAPI;
/** 获取url的立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosDataForURL:(NSString *)url;
/** 获取url的立即发送埋点数量 */
+ (NSUInteger)immediatelyPhobosCountForURL:(NSString *)url;
/** 清除非立即发送埋点数据缓存 */
+ (void)removeAllNormalPhobosData;
/** 清除立即发送埋点数据缓存 */
+ (void)removeAllImmediatelyPhobosData;
/** 获取将要发送的数据 */
@property (nonatomic, copy) void (^phobosSendDataBlock)(NSArray *datas);
@end
NS_ASSUME_NONNULL_END
......@@ -13,11 +13,10 @@
#import "UIResponder+PhobosPV.h"
#import "PhobosUtil.h"
#import "PhobosCustomVisibleController.h"
#import <GMKit/UIDevice+Resolutions.h>
#import "UIDevice+Resolutions.h"
#import <GMCache/GMCache.h>
#import <objc/runtime.h>
static Phobos *_sharedClient;
static NSString *sdkVersion = @"1.3.1";
static Phobos *sharedClient = nil;
static NSString *sdkVersion = @"110";
@interface Phobos ()
@property (strong, nonatomic) UIViewController *visibleController;
......@@ -29,84 +28,48 @@ static NSString *sdkVersion = @"1.3.1";
/* 每一条埋点数据的物理ID,自增,生命周期和sessionId相同。特别注意:在sessionOver的时候,要把他置为0 */
@property (assign, nonatomic) NSInteger serialId;
//@property (nonatomic, weak) dispatch_semaphore_t immediatelySemaphore;// 保障需要立即发送的埋点数据安全
//@property (nonatomic, weak) dispatch_semaphore_t normalSemaphore;// 保障普通埋点数据安全
@property (nonatomic, assign) NSInteger normalCount;// 记录普通埋点数量
// 用来记录除serverAPI以外的API
@property (strong, nonatomic) NSMutableArray *APIArray;
@end
@implementation Phobos
static dispatch_semaphore_t _immediatelySemaphore;
static dispatch_semaphore_t _normalSemaphore;
static dispatch_queue_t _immediatelyQueue;
static dispatch_queue_t _normalQueue;
+ (Phobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId{
Phobos.sharedClient.appName = appName;
Phobos.sharedClient.channelId = channelId;
return Phobos.sharedClient;
}
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_immediatelySemaphore = dispatch_semaphore_create(1);
_normalSemaphore = dispatch_semaphore_create(1);
_immediatelyQueue = dispatch_queue_create("immdiately", DISPATCH_QUEUE_CONCURRENT);
_normalQueue = dispatch_queue_create("normal", DISPATCH_QUEUE_CONCURRENT);
});
return [[self alloc] initWithAppName:appName channelId:channelId];
}
- (instancetype)init {
if (self = [super init]) {
_appName = @"";
_channelId = @"";
+ (Phobos *)sharedClient{
return sharedClient;
}
+ (void)setSharedClient:(Phobos *)client{
sharedClient = client;
}
- (instancetype)initWithAppName:(NSString *)appName channelId:(NSString *)channelId{
self = [super init];
if (self) {
_appName = appName;
_channelId = channelId;
_logEnabled = NO;
_userId = @"";
_netStatus = @"";
_currentCityId = @"";
_serverAPI = @"";
_greyType = @"";
_userType = [[NSMutableDictionary alloc] init];
_userType = [[NSMutableDictionary alloc] initWithCapacity:0];
_appVersion = [PhobosUtil getAppVersion];
_APIArray = [NSMutableArray array];
_signingType = PhobosSigningTypeUndefined;
// self.immediatelySemaphore = dispatch_semaphore_create(1);
// self.normalSemaphore = dispatch_semaphore_create(1);
NSMutableDictionary *dataDict = [[GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey] mutableCopy];
for (NSArray *data in dataDict.allValues) {
self.normalCount += data.count;
}
[self setupNotification];
[self handleSessionStart];
[self synchronizePhobosKey];
phobosLog(@"starts to orbit");
}
return self;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
return Phobos.sharedClient;
}
- (id)copyWithZone:(struct _NSZone *)zone {
return Phobos.sharedClient;
}
+ (Phobos *)sharedClient{// 使用单例设计模式,保证Phobos只有一个实例,并提供了全局访问点
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[super allocWithZone:NULL] init];
});
return _sharedClient;
}
- (void)setAppName:(NSString *)appName channelId:(NSString *)channelId {
self.appName = appName;
self.channelId = channelId;
}
/**
* disk下的PhobosHaveOpenApp只要存在,就把他取出来放到document下(注:当不支持7.6.16版本的时候,干掉这个方法)
*/
......@@ -126,7 +89,7 @@ static dispatch_queue_t _normalQueue;
}
- (void)setUserType:(NSMutableDictionary *)userType {
if (userType == nil && userType.allKeys.count == 0) {
if (userType == nil && userType.count == 0) {
return;
}
NSArray *newKeys = userType.allKeys;
......@@ -225,7 +188,7 @@ static dispatch_queue_t _normalQueue;
phobosLog(@"handleAppInForeground");
[self handleSessionStart];
[self handleEventDeviceOpened];
[self sendImmediatelyNormalData];
[self fetchDataAndSend];
[self handlePVEventAppInForeground];
}
......@@ -238,7 +201,7 @@ static dispatch_queue_t _normalQueue;
phobosLog(@"handleAppInBackgound");
[self handlePVEventAppInBackgound];
[self handleSessionOver];
[self sendImmediatelyNormalData];
[self fetchDataAndSend];
}
/**
......@@ -293,10 +256,100 @@ static dispatch_queue_t _normalQueue;
}
}
#pragma mark - track event handler
+ (void)track:(NSString *)eventId{
[Phobos track:eventId attributes:@{}];
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes{
[self track:eventId attributes:attributes sendNow:NO];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
//超过一定数量的话,统一发送一次
if (array.count > PhobosShardCount) {
[sharedClient sendArray:array cleanCacheRightNow:YES];
}
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow{
NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes];
@try {
NSData *JSON = [PhobosUtil encodeJSON:dict];
if (sendNow) {
NSArray *array = @[dict];
// 实时发送的埋点,不能立即清楚缓存
[sharedClient sendArray:array cleanCacheRightNow:NO];
}else{
[sharedClient save:dict];
}
}
@catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE不能为NSObject ");
}
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI {
[self track:eventId attributes:attributes sendNow:NO currentAPI:currentAPI];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI]];
//超过一定数量的话,统一发送一次
if (array.count > PhobosShardCount) {
[sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:YES];
}
}
+ (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI {
[sharedClient addNewApi:currentAPI]; // 记录新的API
NSDictionary *dict = [sharedClient prepareDictionaryForEvent:eventId attributes:attributes];
@try {
NSData *JSON = [PhobosUtil encodeJSON:dict];
if (sendNow) {
NSArray *array = @[dict];
// 实时发送的埋点,不能立即清楚缓存
[sharedClient sendArray:array currentAPI:currentAPI cleanCacheRightNow:NO];
}else{
[sharedClient save:dict currentAPI:currentAPI];
}
}
@catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE不能为NSObject ");
}
}
+ (void)track:(NSString *)eventId currentAPI:(NSString *)currentAPI {
[Phobos track:eventId attributes:@{} currentAPI:currentAPI];
}
+ (void)trackJsEvent:(NSString *)jsonString{
@try {
NSData *data = [jsonString dataUsingEncoding:NSUnicodeStringEncoding];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
id pa = dict[@"params"];
NSDictionary *json;
if ([pa isKindOfClass:[NSString class]]) {
NSError *jsonError;
NSData *objectData = [pa dataUsingEncoding:NSUTF8StringEncoding];
json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
[Phobos track:dict[@"type"] attributes:json];
}else{
[Phobos track:dict[@"type"]];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
#pragma mark - PV
- (void)onPVStart:(UIResponder<PhobosPVProtocol> *)page {
// 必须在此处调用一下referer,因为onControllerStart
[page initReferer];
[page initRefererLink];
[page initReferrerIdIfNil];
[page initReferrerTabName];
page.inTime = [PhobosUtil currentTime];
......@@ -306,7 +359,6 @@ static dispatch_queue_t _normalQueue;
if (![PhobosUtil isNonEmpty:page.pageName] || !page.needLogPV) {
return;
}
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try {
[dict setObject:[PhobosUtil currentTime] forKey:@"out"];
......@@ -314,11 +366,13 @@ static dispatch_queue_t _normalQueue;
[dict setObject:page.pageName forKey:@"page_name"];
[dict setObject:page.businessId?:@"" forKey:@"business_id"];
[dict setObject:page.referer?:@"" forKey:@"referrer"];
[dict setObject:page.referrerLink ? : @[] forKey:@"referrer_link"];
[dict setObject:@(0) forKey:@"fake"];
[dict setObject:page.referrerId ? : @"" forKey:@"referrer_id"];
[dict setObject:page.extraParam ? : @"" forKey:@"extra_param"];
[dict setObject:page.referrerTabName ? : @"" forKey:@"referrer_tab_name"];
[dict setObject:page.isPush.intValue ? @(page.isPush.intValue) : @(0) forKey:@"is_push"];
if (page.inTime.length > 0) {
// 页面显示时间为空时不记录页面pv事件
[Phobos track:@"page_view" attributes:dict];
......@@ -347,44 +401,51 @@ static dispatch_queue_t _normalQueue;
}
}
#pragma mark - 事件存储、发送
/**
* @brief 将埋点时间封装成词典数据
*
* @since 0.0.1
*/
- (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventName attributes:(NSDictionary *)attributes{
[self catchNullForEvent:eventName attributes:attributes];
- (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes{
NSArray *referrerLink = sharedClient.visibleController.referrerLink;
if (![eventId isEqualToString:@"page_view"]) {
NSMutableDictionary *attributesParams = [NSMutableDictionary dictionaryWithDictionary:attributes];
[attributesParams setValue:referrerLink ? : @[] forKey:@"referrer_link"];
attributes = attributesParams;
}
[self catchNullForEvent:eventId attributes:attributes];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try {
NSString *currentTime = [PhobosUtil currentTime];
NSMutableDictionary *deviceParams = [NSMutableDictionary new];
[deviceParams setValue:[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString] forKey:@"idfa"];
[deviceParams setValue:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forKey:@"idfv"];
[deviceParams setValue:[PhobosUtil deviceId] forKey:@"device_id"];
[deviceParams setValue:@"ios" forKey:@"device_type"];
[deviceParams setValue:@"Apple" forKey:@"manufacturer"];
[deviceParams setValue:@(self.gps.coordinate.latitude) forKey:@"lat"];
[deviceParams setValue:@(self.gps.coordinate.longitude) forKey:@"lng"];
[deviceParams setValue:_netStatus forKey:@"is_WiFi"];
[deviceParams setValue:[PhobosUtil getIPAddress:YES] forKey:@"ip"];
NSMutableDictionary *deviceParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString],@"idfa",
[[[UIDevice currentDevice] identifierForVendor] UUIDString],@"idfv",
[PhobosUtil deviceId],@"device_id",
@"ios",@"device_type",
@"Apple",@"manufacturer",
@(self.gps.coordinate.latitude),@"lat",
@(self.gps.coordinate.longitude),@"lng",
_netStatus,@"is_WiFi",
[PhobosUtil getIPAddress:YES],@"ip",nil];
[deviceParams setValue:_networkStatus forKey:@"net_type"];
[deviceParams setValue:[UIDevice platform] forKey:@"model"];
[deviceParams setValue:[UIDevice currentDevice].systemVersion forKey:@"sys_version"];
NSMutableDictionary *appParams = [NSMutableDictionary new];
[appParams setValue:_greyType forKey:@"grey_type"];
[appParams setValue:_appName forKey:@"name"];
[appParams setValue:_appVersion forKey:@"version"];
[appParams setValue:_channelId forKey:@"channel"];
[appParams setValue:_userType forKey:@"user_type"];
[appParams setValue:_currentCityId forKey:@"current_city_id"];
[appParams setValue:@(_serialId++) forKey:@"serial_id"];
NSMutableDictionary *appParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
_greyType, @"grey_type",
self.appName, @"name",
self.appVersion, @"version",
self.channelId,@"channel",
_userType,@"user_type",
self.currentCityId,@"current_city_id",
@(_serialId++), @"serial_id",nil];
if (_signingType == PhobosSigningTypeDebug || _signingType == PhobosSigningTypeRelease) {
[dict setObject:@(0) forKey:@"is_release"];
}
[dict setObject:eventName forKey:@"type"];
[dict setObject:eventId forKey:@"type"];
[dict setObject:appParams forKey:@"app"];
[dict setObject:sdkVersion forKey:@"version"];
[dict setObject:deviceParams forKey:@"device"];
......@@ -399,190 +460,33 @@ static dispatch_queue_t _normalQueue;
return dict;
}
#pragma mark - helpers
- (void)catchNullForEvent:(NSString *)eventName attributes:(NSDictionary *)attributes {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@try {
for (NSString *key in attributes.allKeys) {
if ([attributes[key] isMemberOfClass:[NSNull class]]) {
if (self.captureNullExpection) {
self.captureNullExpection(eventName, attributes);
}
break;
}
}
} @catch (NSException *exception) {
}
});
}
#pragma mark - track event handler
+ (void)trackJsEvent:(NSString *)jsonString{
@try {
NSData *data = [jsonString dataUsingEncoding:NSUnicodeStringEncoding];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
id pa = dict[@"params"];
NSDictionary *json;
if ([pa isKindOfClass:[NSString class]]) {
NSError *jsonError;
NSData *objectData = [pa dataUsingEncoding:NSUTF8StringEncoding];
json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
[Phobos track:dict[@"type"] attributes:json];
}else{
[Phobos track:dict[@"type"]];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
+ (void)track:(NSString *)eventName{
[self track:eventName attributes:@{} sendNow:NO currentAPI:_sharedClient.serverAPI];
}
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes{
[self track:eventName attributes:attributes sendNow:NO currentAPI:_sharedClient.serverAPI];
}
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow{
[self track:eventName attributes:attributes sendNow:sendNow currentAPI:_sharedClient.serverAPI];
}
+ (void)track:(NSString *)eventName currentAPI:(NSString *)currentAPI {
[self track:eventName attributes:@{} sendNow:NO currentAPI:currentAPI];
}
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes currentAPI:(NSString *)currentAPI {
[self track:eventName attributes:attributes sendNow:NO currentAPI:currentAPI];
}
+ (void)track:(NSString *)eventName attributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow currentAPI:(NSString *)currentAPI {
NSDictionary *dataDict = [_sharedClient prepareDictionaryForEvent:eventName attributes:attributes];
@try {
NSData *JSON = [PhobosUtil encodeJSON:dataDict];
if (sendNow) {
[_sharedClient sendImmediatelyPhobosWithURL:currentAPI data:dataDict];
} else{
[_sharedClient trackPhobosWithURL:currentAPI data:dataDict];
}
}
@catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE不能为NSObject ");
}
}
#pragma mark - 事件存储、发送
/**
* 普通埋点
* @param url 接口url
* @param dataArray 埋点数据
* @param nowSend 是否需要发送当前缓存数据(app进入到后台、前台等情况)
*/
- (void)sendImmediatelyNormalData {
[self trackPhobosWithURL:nil data:nil immediate:YES];
}
- (void)trackPhobosWithURL:(NSString *)url data:(NSDictionary *)data {
[self trackPhobosWithURL:url data:data immediate:NO];
}
- (void)trackPhobosWithURL:(NSString *)url data:(NSDictionary *)data immediate:(BOOL)immediate {
dispatch_async(_normalQueue, ^{
dispatch_semaphore_wait(_normalSemaphore, DISPATCH_TIME_FOREVER);
NSMutableDictionary *dataDict = [[GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey] mutableCopy];
_normalCount++;
if (!immediate) {
dataDict = [self dataDict:dataDict setObject:data forKey:url];
[GMCache storeObjectAtDocumentPathWithkey:PhobosNormalCacheKey object:dataDict];
}
if (immediate || self.normalCount >= 50) { // 数据超过一定数量 或 进入后台等逻辑,统一进行发送普通埋点数据
[dataDict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSArray * _Nonnull obj, BOOL * _Nonnull stop) {
if (obj && obj.count > 0) {
[self sendImmediatelyPhobosWithURL:key data:obj];
}
}];
[Phobos removeAllNormalPhobosData];
}
dispatch_semaphore_signal(_normalSemaphore);
});
}
/**
* 即时发送埋点接口
* @param url : 接口url
* @param data : 使用id类型是因为直接调用实时埋点传过来的是Dict,而普通埋点过来的则是Array类型
*/
- (void)sendImmediatelyPhobosWithURL:(NSString *)url data:(id)data {
dispatch_async(_immediatelyQueue, ^{
dispatch_semaphore_wait(_immediatelySemaphore, DISPATCH_TIME_FOREVER);
__block NSMutableDictionary *dataDict = [[GMCache fetchObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey] mutableCopy];
dataDict = [self dataDict:dataDict setObject:data forKey:url];
NSInteger allCount = dataDict.allKeys.count;// 请求个数
__block finishCount = 0;// 请求完成次数,不是请求成功次数,能在最后保证未发送成功的数据,能一次保存下来,等待下次发送
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);// 因为网络请求是异步回调,需要保证数据安全
[dataDict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSArray * _Nonnull obj, BOOL * _Nonnull stop) {
[self sendDataWithAPI:key data:obj successBlock:^(NSInteger code) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
finishCount++;
if (code == 200) {// 如果数据请求成功,删除当前数据的缓存
[dataDict removeObjectForKey:key];
}
if (finishCount == allCount) {// 所有数据都请求完成,重新进行数据缓存
[GMCache storeObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey object:dataDict];
dispatch_semaphore_signal(_immediatelySemaphore);
}
dispatch_semaphore_signal(semaphore);
phobosLog(@"✈ ---------- ✈ data arrived Mars");
}];
}];
});
}
/**
* 将数据存入到字典中
* @param dataDict 需要存入的字典
* @param object 需要存入的数据
* @param key 需要将数据存入到dataDict的key所在的数据中
* @brief 保存数据到缓存层
*
* @param data 数据
*
* @since 0.0.1
*/
- (NSMutableDictionary *)dataDict:(NSMutableDictionary *)dataDict setObject:(id)object forKey:(NSString *)key {
if (!dataDict) {
dataDict = [NSMutableDictionary new];
}
// 判断数据类型,统一转换成数组
NSMutableArray *dataArray = [dataDict.allKeys containsObject:key] ? [dataDict[key] mutableCopy] : [NSMutableArray new];
NSArray *data = [object isKindOfClass:[NSArray class]] ? object :@[object];
//#ifndef POD_CONFIGURATION_APP_STORE
// [self verifyPVPhobos:dataArray data:object];
//#endif
[dataArray addObjectsFromArray:data];
// 进行数据校验
@try {
NSData *json = [PhobosUtil encodeJSON:dataArray];
[dataDict setValue:dataArray forKey:key];
} @catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE只不能为NSObject ");
- (void)save:(NSDictionary *)data
{
if (_logEnabled) {
phobosLog([NSString stringWithFormat:@"save dictionary: %@",data]);
}
return dataDict;
}
// 校验PV埋点是否有问题,默认返回NO
- (BOOL)verifyPVPhobos:(NSArray *)dataArray data:(id)data {
if ([data isKindOfClass:[NSDictionary class]]) {
NSDictionary *dataDict = (NSDictionary *)data;
if (dataDict[@"type"] && [dataDict[@"type"] isEqualToString:@"page_view"]) {
NSDictionary *pageParams = dataDict[@"params"];
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
if (dataArray) {
#ifdef POD_CONFIGURATION_APP_STORE
[dataArray addObject:data];
#else
if (data[@"type"] && [data[@"type"] isEqualToString:@"page_view"]) {
NSDictionary *pageParams = data[@"params"];
long long pageInTime = [pageParams[@"in"] longLongValue];
long long pageOutTime = [pageParams[@"out"] longLongValue];
// phobosLog(@"pageInTime------%lld",pageInTime);
// phobosLog(@"pageOutTime------%lld",pageOutTime);
if (pageInTime && pageOutTime) {
BOOL checkTimeError = NO;
__block BOOL checkTimeError;
for (int i = 0; i < dataArray.count; i++) {
NSDictionary *obj = dataArray[i];
......@@ -604,29 +508,77 @@ static dispatch_queue_t _normalQueue;
break;
}
}
}
return checkTimeError;
}
}
// 不管成功还是失败 给开发提示方便DEBUG
// phobosLog(@"%s____数据校验成功", __func__);
[dataArray addObject:data];
#endif
} else {
dataArray = [NSMutableArray arrayWithObject:data];
}
[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 从缓存中获取数据,并发送
*
* @since 0.0.1
*/
- (void)fetchDataAndSend{
NSArray *paramsArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
if (paramsArray.count>0) {
[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];
}
}
return NO;
}
// 发送埋点数据
- (void)sendDataWithAPI:(NSString *)api data:(NSArray *)dataArray successBlock:(SendDataSuccessBlock)successBlock {
/*
从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉
*/
- (void)sendArray {
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 {
if (self.phobosSendDataBlock) {
self.phobosSendDataBlock(dataArray);
}
NSData *JSON = [PhobosUtil encodeJSON:dataArray];
NSData *compressedData = [PhobosUtil compressData:JSON];
if (compressedData) {
[PhobosUtil sendData:compressedData currentAPI:api success:successBlock];
[PhobosUtil sendData:compressedData success:^(NSInteger code) {
phobosLog(@"✈ ---------- ✈ data arrived Mars");
[GMCache removeObjectAtDocumentPathWithkey:PhobosTempCacheKey];
}];
}
}
@catch (NSException *exception) {
......@@ -634,76 +586,110 @@ static dispatch_queue_t _normalQueue;
}
}
@end
@implementation Phobos (UtilTest)
/** 获取所有非立即发送埋点数量 */
+ (NSInteger)normalPhobosCount {
return Phobos.sharedClient.normalCount;
}
/** 获取所有非立即发送埋点数据 */
+ (NSDictionary *)normalPhobosData {
return [GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey];
}
/** 获取url的非立即发送埋点数量 */
+ (NSUInteger)normalPhobosCountForURL:(NSString *)url {
NSDictionary *dict = [GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey];
NSArray *urlData = dict[url];
return urlData ? urlData.count : 0;
}
+ (NSArray *)normalPhobosDataForServerAPI {
return [self normalPhobosDataForURL:Phobos.sharedClient.serverAPI];
}
/** 获取url的非立即发送埋点数据 */
+ (NSArray *)normalPhobosDataForURL:(NSString *)url {
NSDictionary *dict = [GMCache fetchObjectAtDocumentPathWithkey:PhobosNormalCacheKey];
return dict[url];
}
+ (NSArray *)immediatelyPhobosForServerAPI {
return [self immediatelyPhobosDataForURL:Phobos.sharedClient.serverAPI];
}
/** 获取所有立即发送埋点数据 */
+ (NSDictionary *)immediatelyPhobosData {
return [GMCache fetchObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey];
}
/** 获取url的立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosDataForURL:(NSString *)url {
NSDictionary *dict = [GMCache fetchObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey];
return dict[url];
/*
从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉
*/
- (void)sendArrayWithCurrentAPI:(NSString *)currentAPI {
NSString *PhobosTempCacheKeyStr = [PhobosUtil MD5String:[PhobosUtil MD5String:currentAPI]]; // 两次加密作为key值,和缓存的key值作区分
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr];
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:PhobosTempCacheKeyStr];
}];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
/**
该方法有改动,现在的逻辑是:当前方法只接受发送的请求,然后把数据转存到另一个缓存区,
让sendArray方法负责发送,数据一旦转移到缓存区,就把原有的数据干掉。
@author zhaiguojun 16-10-17 in (null)
@param array 参数
@param clean 是否立即清楚缓存
*/
- (void)sendArray:(NSArray *)array cleanCacheRightNow:(BOOL)clean {
@try {
//1.获取缓存区的数据,把新数据追加进去
+ (NSUInteger)immediatelyPhobosCountForURL:(NSString *)url {
NSDictionary *dict = [GMCache fetchObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey];
NSArray *urlData = dict[url];
return urlData ? urlData.count : 0;
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
if (dataArray) {
[dataArray addObjectsFromArray:array];
}else{
dataArray = [NSMutableArray arrayWithArray:array];
}
[GMCache storeObjectAtDocumentPathWithkey:PhobosTempCacheKey object:dataArray];
//2.把缓存区的数据发送给服务器
[self sendArray];
//3.把原有的数据删除
if (clean) {
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
}
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
/** 清除非立即发送埋点数据缓存 */
+ (void)removeAllNormalPhobosData {
Phobos.sharedClient.normalCount = 0;
[GMCache removeObjectAtDocumentPathWithkey:PhobosNormalCacheKey];
/**
上边方法新加了一个API字段
*/
- (void)sendArray:(NSArray *)array currentAPI:(NSString *)currentAPI cleanCacheRightNow:(BOOL)clean {
@try {
//1.获取缓存区的数据,把新数据追加进去
NSString *PhobosTempCacheKeyStr = [PhobosUtil MD5String:[PhobosUtil MD5String:currentAPI]]; // 两次加密作为key值,和缓存的key值作区分
NSMutableArray *dataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr];
if (dataArray) {
[dataArray addObjectsFromArray:array];
}else{
dataArray = [NSMutableArray arrayWithArray:array];
}
[GMCache storeObjectAtDocumentPathWithkey:PhobosTempCacheKeyStr object:dataArray];
//2.把缓存区的数据发送给服务器
[self sendArrayWithCurrentAPI:currentAPI];
//3.把原有的数据删除
[GMCache removeObjectAtDocumentPathWithkey:[PhobosUtil MD5String:currentAPI]];
}
@catch (NSException *exception) {
phobosLog(exception);
}
}
/** 清除立即发送埋点数据缓存 */
+ (void)removeAllImmediatelyPhobosData {
[GMCache removeObjectAtDocumentPathWithkey:PhobosImmediatelyCacheKey];
#pragma mark - helpers
- (void)catchNullForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@try {
for (NSString *key in attributes.allKeys) {
if ([attributes[key] isMemberOfClass:[NSNull class]]) {
if (self.captureNullExpection) {
self.captureNullExpection(eventId, attributes);
}
break;
}
}
} @catch (NSException *exception) {
}
});
}
- (void)setPhobosSendDataBlock:(void (^)(NSArray *))phobosSendDataBlock {
objc_setAssociatedObject(self, @selector(phobosSendDataBlock), phobosSendDataBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(NSArray *))phobosSendDataBlock {
return objc_getAssociatedObject(self, @selector(phobosSendDataBlock));
- (void)addNewApi:(NSString *)api {
for (NSString *item in self.APIArray) {
if ([api isEqualToString:item]) {
break;
}
[self.APIArray addObject:api];
}
}
@end
......@@ -18,8 +18,8 @@
#define PhobosHaveOpenApp @"PhobosHaveOpenApp" //是否打开过APP
#define PhobosBeginTime @"PhobosBeginTime" //记录APP打开|从后台启动时的时间戳
#define PhobosEndTime @"PhobosEndTime" //记录APP退出|退到后台时的时间戳
#define PhobosNormalCacheKey @"PhobosNormalCacheKey" //存放持久化埋点数据的key
#define PhobosImmediatelyCacheKey @"PhobosImmediatelyCacheKey" //存放持久化实时埋点数据的key
#define PhobosCacheKey @"PhobosCacheKey" //存放持久化埋点数据的key
#define PhobosTempCacheKey @"PhobosTempCacheKey" //临时存放待发送埋点数据的key
#define PhobosShardCount 50 //收集数据分段发送的个数
......
......@@ -75,5 +75,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, copy) NSString *isPush;
/**
获取上一个页面链路的page_name link by 7.20.0 如果有此页面有page_name则添加 , 没有添加""
*/
@property (nonatomic, copy) NSArray *referrerLink;
@end
NS_ASSUME_NONNULL_END
......@@ -127,8 +127,6 @@
//没有错误,返回正确;
if (success) {
success(200);
} else {
success(error.code);
}
}
}];
......@@ -152,8 +150,6 @@
//没有错误,返回正确;
if (success) {
success(200);
} else {
success(error.code);
}
}
}];
......
......@@ -15,6 +15,11 @@
*/
- (void)initReferer;
/**
// // 适用于链路中页面浏览事件的统计 添加 refererLink 参数
*/
- (void)initRefererLink;
/**
此方法在onPvStart时调用,如果发现已经有值了,不会给referrerId再次赋值
*/
......@@ -25,4 +30,5 @@
*/
- (void)initReferrerTabName;
@end
......@@ -45,6 +45,52 @@
}
}
// 适用于链路中页面浏览事件的统计 添加 refererLink 参数
- (void)initRefererLink {
if ([self isKindOfClass:[UIViewController class]]) {
// 分present与navigation两种情况
UIViewController *me = (UIViewController *)self;
if (me.isPush.intValue) { // 如果是推送进来的,结果页面的referrerLink 为空数组, 此处添加过滤。
objc_setAssociatedObject(self, @selector(referrerLink), @[], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return;
}
if (me.presentingViewController != nil) {
// app全局只有一个navigation,发现此时用navigation.topViewController presentViewController时,最终使用的是navigation弹出的
// 所以此处要判断,如果是navigation弹出,最后还是要定位到topViewController
if ([me.presentingViewController isKindOfClass:[UINavigationController class]]) {
UIViewController *top = ((UINavigationController *)me.presentingViewController).topViewController;
NSMutableArray *tempLink = [NSMutableArray arrayWithArray:top.referrerLink];
[tempLink addObject:top.pageName];
objc_setAssociatedObject(self, @selector(referrerLink), tempLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
NSMutableArray *tempLink = [NSMutableArray arrayWithArray:me.presentingViewController.referrerLink];
[tempLink addObject:me.presentingViewController.pageName];
objc_setAssociatedObject(self, @selector(referrerLink), tempLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
} else {
NSArray *navigationPool = ((UIViewController *)self).navigationController.viewControllers;
NSInteger refererIndex = navigationPool.count - 2;
if (refererIndex < 0 ) {
return ;
}
UIViewController *controller = navigationPool[refererIndex];
NSMutableArray *tempLink = [NSMutableArray arrayWithArray:controller.referrerLink];
[tempLink addObject:controller.pageName];
objc_setAssociatedObject(self, @selector(referrerLink), tempLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
}
- (void)setReferrerLink:(NSArray *)referrerLink {
objc_setAssociatedObject(self, @selector(referrerLink), referrerLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSArray *)referrerLink {
NSArray *referrerLink = objc_getAssociatedObject(self, @selector(referrerLink));
return referrerLink;
}
/**
此方法在onPvStart时调用,给referrerTabName赋值
*/
......
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