Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
GMPhobos
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gengmeiios
GMPhobos
Commits
848180b3
Commit
848180b3
authored
Apr 29, 2021
by
艾娇平
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、移除旧的埋点库
2、ios14适配,增加idfa授权请求 3、添加通用参数ua
parent
74b89c03
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
71 additions
and
1017 deletions
+71
-1017
NewPhobos.h
GMPhobos/Classes/NewPhobos.h
+2
-4
NewPhobos.m
GMPhobos/Classes/NewPhobos.m
+2
-1
OldPhobos.h
GMPhobos/Classes/OldPhobos.h
+0
-186
OldPhobos.m
GMPhobos/Classes/OldPhobos.m
+0
-713
Phobos.h
GMPhobos/Classes/Phobos.h
+2
-0
Phobos.m
GMPhobos/Classes/Phobos.m
+15
-70
PhobosConfig.h
GMPhobos/Classes/PhobosConfig.h
+0
-2
PhobosDevice.h
GMPhobos/Classes/PhobosDevice.h
+7
-3
PhobosDevice.m
GMPhobos/Classes/PhobosDevice.m
+43
-38
No files found.
GMPhobos/Classes/NewPhobos.h
View file @
848180b3
...
...
@@ -35,9 +35,6 @@ NS_ASSUME_NONNULL_BEGIN
+
(
instancetype
)
sharedClient
;
/** 用于Phobos库从old更新到new使用,在全量之后删除相关逻辑 */
@property
(
nonatomic
,
assign
)
BOOL
isGray
;
@property
(
nonatomic
,
assign
)
BOOL
isGraySendNow
;
//控制实时上报
// Phobos在处理业务端传递来的参数时会检查是否某个value为空,如果为空会调用这个block以通知业务层,业务层可以上报这个异常,以助解决问题
...
...
@@ -121,7 +118,8 @@ NS_ASSUME_NONNULL_BEGIN
从主项目获取当前显示的controller
*/
@property
(
nonatomic
,
copy
)
UIViewController
*
(
^
getTopController
)
(
void
);
/// ua 为了应对苹果14.5之后不允许再直接读取idfa采取的归因方案--ip+ua
@property
(
nonatomic
,
copy
)
NSString
*
wk_ua
;
#pragma mark - 事件采集
/** 点击事件采集 */
...
...
GMPhobos/Classes/NewPhobos.m
View file @
848180b3
...
...
@@ -336,6 +336,7 @@ static NewPhobos *_sharedClient;
[
deviceParams
setValue
:
PhobosSafeString
(
_networkStatus
)
forKey
:
@"net_type"
];
[
deviceParams
setValue
:
PhobosSafeString
([
PhobosUtil
platform
])
forKey
:
@"model"
];
[
deviceParams
setValue
:
PhobosSafeString
([
UIDevice
currentDevice
].
systemVersion
)
forKey
:
@"sys_version"
];
[
deviceParams
setValue
:
PhobosSafeString
(
_wk_ua
)
forKey
:
@"ua"
];
NSMutableDictionary
*
appParams
=
[
NSMutableDictionary
new
];
[
appParams
setValue
:
PhobosSafeString
(
_greyType
)
forKey
:
@"grey_type"
];
...
...
@@ -343,7 +344,7 @@ static NewPhobos *_sharedClient;
[
appParams
setValue
:
PhobosSafeString
(
_appVersion
)
forKey
:
@"version"
];
[
appParams
setValue
:
PhobosSafeString
(
_channelId
)
forKey
:
@"channel"
];
[
appParams
setValue
:
PhobosSafeString
(
_userType
)
forKey
:
@"user_type"
];
[
appParams
setValue
:
@
(
_isGray
)
forKey
:
@"is_gray"
];
[
appParams
setValue
:
@
(
YES
)
forKey
:
@"is_gray"
];
[
appParams
setValue
:
PhobosSafeString
(
_currentCityId
)
forKey
:
@"current_city_id"
];
[
appParams
setValue
:
@
(
_serialId
++
)
forKey
:
@"serial_id"
];
...
...
GMPhobos/Classes/OldPhobos.h
deleted
100644 → 0
View file @
74b89c03
//
// Phobos.h
// GengmeiDoctor
// Data Statistic Client For Mars
// Created by Thierry on 16/1/26.
// Copyright © 2016年 wanmeizhensuo. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PhobosPVProtocol.h"
#import <CoreLocation/CLLocation.h>
#import "PhobosConfig.h"
NS_ASSUME_NONNULL_BEGIN
@interface
OldPhobos
:
NSObject
/**
* @brief 开启Phobos统计,默认以BATCH方式发送log.
*
* @param appName 通常由数据端与客户端一起确认的区分不同app的名字
* @param channelId 发布渠道
*
* @return Phobos实例
*
* @since 0.0.1
*/
+
(
OldPhobos
*
)
clientWithAppName
:(
NSString
*
)
appName
channelId
:(
NSString
*
)
channelId
;
+
(
instancetype
)
sharedClient
;
+
(
void
)
setSharedClient
:(
OldPhobos
*
)
client
;
#pragma mark - SDK配置
// Phobos在处理业务端传递来的参数时会检查是否某个value为空,如果为空会调用这个block以通知业务层,业务层可以上报这个异常,以助解决问题
@property
(
nonatomic
,
copy
)
void
(
^
captureNullExpection
)
(
NSString
*
eventId
,
NSDictionary
*
info
);
/**
网络状态 wifi=1, mobile=0, 不连通=-1
*/
@property
(
nonatomic
,
copy
)
NSString
*
netStatus
;
/**
//没有网络连接
public static final String NETWORN_NONE = "none";
//wifi连接
public static final String NETWORN_WIFI = "wifi";
//手机网络数据连接类型
public static final String NETWORN_2G = "2G";
public static final String NETWORN_3G = "3G";
public static final String NETWORN_4G = "4G";
public static final String NETWORN_MOBILE = "other";
*/
@property
(
nonatomic
,
copy
)
NSString
*
networkStatus
;
/**
* @brief 设置是否打印sdk的log信息,默认不开启
*
* @since 0.0.1
*/
@property
(
assign
,
nonatomic
)
BOOL
logEnabled
;
/**
* @brief 设置当前登录用户的ID,如果没有默认为@""
*
*
* @since 0.0.2
*/
@property
(
strong
,
nonatomic
)
NSString
*
userId
;
/*!
* @author zhaiguojun, 16-05-31
*
* @brief 用户当前的城市id
*
*
* @since 0.2.7
*/
@property
(
strong
,
nonatomic
)
NSString
*
currentCityId
;
@property
(
strong
,
nonatomic
)
CLLocation
*
gps
;
/**
* 记录用户类型
*/
@property
(
strong
,
nonatomic
)
NSMutableDictionary
*
userType
;
/**
数据接收的服务器API
*/
@property
(
copy
,
nonatomic
)
NSString
*
serverAPI
;
/**
当前APP请求接口的 APIHOST(GMServerDomains.apiHost 主要用于flutter AppDelegate 中初始化需要传值
*/
@property
(
nonatomic
,
copy
)
NSString
*
apiHost
;
/**
当前APP请求接口的 cookie(主要用于flutter) 获取到cookie 的时候穿过来 或者cookie 有变化的时候传过来
*/
@property
(
nonatomic
,
copy
)
NSString
*
cookie
;
/**
灰度组, since 7.7.65
*/
@property
(
nonatomic
,
copy
)
NSString
*
greyType
;
/**
包的类型:APPSTORE、RELEASE、DEBUG
*/
@property
(
nonatomic
,
assign
)
PhobosSigningType
signingType
;
/**
从主项目获取当前显示的controller
*/
@property
(
nonatomic
,
copy
)
UIViewController
*
(
^
getTopController
)
(
void
);
#pragma mark - 事件采集
/**
* @brief 自定义事件,数量统计.
*
* @param eventId 事件Id
* @attributes 参数
* @sendNow 是否实时发送,默认为NO
*
* @since 0.0.1
*/
+
(
void
)
track
:(
NSString
*
)
eventId
attributes
:(
NSDictionary
*
)
attributes
;
+
(
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
*
* @brief H5调用的埋点方法
*
* @param jsonString h5传过来的参数
*
* @since 0.0.1
*/
+
(
void
)
trackJsEvent
:(
NSString
*
)
jsonString
;
/**
* @brief PV事件开始。当controller viewWillAppear时调用
*/
-
(
void
)
onPVStart
:(
UIResponder
<
PhobosPVProtocol
>
*
)
page
;
/**
* @brief PV事件结束。当controller viewWillDisAppear时调用
*/
-
(
void
)
onPVEnd
:(
UIResponder
<
PhobosPVProtocol
>
*
)
page
;
/**
* 为隐私弹窗触发on_app_session_over埋点使用
*/
-
(
void
)
handleAppInBackgound
;
/**
* @author 翟国钧, 16-03-08 11:03:45
*
* @brief 有些事件需要模拟pv事件,统一用该方法处理,in out 时间相同.点击一次即触发
*
* @param pageName 控件所在VC的pageName
* @param bid 业务id
* @param referer 上个月面的pagename
*
* @since 5.9.1
*/
-
(
void
)
simulativePV
:(
NSString
*
)
pageName
businessId
:(
NSString
*
)
bid
referer
:(
NSString
*
)
referer
;
@end
NS_ASSUME_NONNULL_END
GMPhobos/Classes/OldPhobos.m
deleted
100644 → 0
View file @
74b89c03
//
// Phobos.m
// GengmeiDoctor
//
// Created by Thierry on 16/1/26.
// Copyright © 2016年 wanmeizhensuo. All rights reserved.
//
#import "OldPhobos.h"
#import <AdSupport/AdSupport.h>
#import "UIResponder+PhobosPV.h"
#import "PhobosUtil.h"
#import "PhobosCustomVisibleController.h"
#import "PhobosDevice.h"
#import <GMCache/GMCache.h>
#import <mach/mach_time.h>
static
OldPhobos
*
sharedClient
=
nil
;
@interface
OldPhobos
()
@property
(
strong
,
nonatomic
)
UIViewController
*
visibleController
;
@property
(
strong
,
nonatomic
)
NSDateFormatter
*
dateFormatter
;
@property
(
strong
,
nonatomic
)
NSString
*
appName
;
@property
(
strong
,
nonatomic
)
NSString
*
channelId
;
@property
(
strong
,
nonatomic
)
NSString
*
appVersion
;
@property
(
strong
,
nonatomic
)
NSString
*
sessionId
;
/* 每一条埋点数据的物理ID,自增,生命周期和sessionId相同。特别注意:在sessionOver的时候,要把他置为0 */
@property
(
assign
,
nonatomic
)
NSInteger
serialId
;
// 用来记录除serverAPI以外的API
@property
(
strong
,
nonatomic
)
NSMutableArray
*
APIArray
;
@end
@implementation
OldPhobos
+
(
OldPhobos
*
)
clientWithAppName
:(
NSString
*
)
appName
channelId
:(
NSString
*
)
channelId
{
return
[[
self
alloc
]
initWithAppName
:
appName
channelId
:
channelId
];
}
+
(
OldPhobos
*
)
sharedClient
{
return
sharedClient
;
}
+
(
void
)
setSharedClient
:
(
OldPhobos
*
)
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
]
initWithCapacity
:
0
];
_appVersion
=
[
PhobosUtil
getAppVersion
];
_APIArray
=
[
NSMutableArray
array
];
_signingType
=
PhobosSigningTypeUndefined
;
[
self
setupNotification
];
[
self
handleSessionStart
];
phobosLog
(
@"starts to orbit"
);
}
return
self
;
}
-
(
void
)
dealloc
{
if
(
self
)
{
[[
NSNotificationCenter
defaultCenter
]
removeObserver
:
self
];
}
}
-
(
void
)
setUserType
:
(
NSMutableDictionary
*
)
userType
{
if
(
userType
==
nil
&&
userType
.
count
==
0
)
{
return
;
}
NSArray
*
newKeys
=
userType
.
allKeys
;
NSArray
*
oldKeys
=
_userType
.
allKeys
;
for
(
NSString
*
newKey
in
newKeys
)
{
if
([
oldKeys
containsObject
:
newKey
])
{
NSString
*
newValue
=
[[
userType
objectForKey
:
newKey
]
stringValue
];
NSString
*
oldValue
=
[[
_userType
objectForKey
:
newKey
]
stringValue
];
if
(
!
[
newValue
isEqualToString
:
oldValue
])
{
[
_userType
setObject
:[
userType
objectForKey
:
newKey
]
forKey
:
newKey
];
}
}
else
{
[
_userType
setObject
:[
userType
objectForKey
:
newKey
]
forKey
:
newKey
];
}
}
}
/**
在APP启动、从后台到前台的时候需要记录device_opened埋点
*/
-
(
void
)
handleEventDeviceOpened
{
/** 每次打开APP埋点 **/
// [deviceParams setValue:PhobosSafeString([PhobosDevice getIDFA]) forKey:@"idfa"];
// [deviceParams setValue:PhobosSafeString([PhobosDevice getIDFV]) forKey:@"idfv"];
// [deviceParams setValue:PhobosSafeString([PhobosDevice getDEVICEID]) forKey:@"device_id"];
NSDictionary
*
dict
=
@{
@"build_cpu_abi"
:
PhobosSafeString
([
PhobosUtil
currentDeviceCPUType
]),
@"cpu_count"
:
PhobosSafeString
([
PhobosUtil
currentDeviceCPUCount
]),
@"mac_address"
:
PhobosSafeString
([
PhobosUtil
getMacAddress
]),
@"phone_operator"
:
PhobosSafeString
([
PhobosUtil
getTelephonyInfo
]),
@"total_memory"
:
PhobosSafeString
([
PhobosUtil
getTotalMemorySize
]),
@"run_time"
:
PhobosSafeString
([
PhobosUtil
deviceRunTime
]),
@"uuid"
:
PhobosSafeString
([
PhobosDevice
getDEVICEID
]),
@"uqid"
:
PhobosSafeString
([
PhobosDevice
getUQID
]),
@"build_version_release"
:
PhobosSafeString
([[
UIDevice
currentDevice
]
systemVersion
]),
};
[
OldPhobos
track
:
@"device_opened"
attributes
:
dict
sendNow
:
YES
];
}
-
(
UIViewController
*
)
visibleController
{
if
(
self
.
getTopController
)
{
id
target
=
self
.
getTopController
();
if
([
target
conformsToProtocol
:
NSProtocolFromString
(
@"PhobosCustomVisibleController"
)])
{
target
=
[
target
performSelector
:
@selector
(
phobosVisibleController
)];
}
return
target
;
}
return
nil
;
}
#pragma mark - notification handler
/**
* @brief 设置对APP的通知监听
*
* @since 0.0.1
*/
-
(
void
)
setupNotification
{
[[
NSNotificationCenter
defaultCenter
]
addObserver
:
self
selector
:
@selector
(
handleAppFinishLaunch
)
name
:
UIApplicationDidFinishLaunchingNotification
object
:
nil
];
[[
NSNotificationCenter
defaultCenter
]
addObserver
:
self
selector
:
@selector
(
handleAppInForeground
)
name
:
UIApplicationWillEnterForegroundNotification
object
:
nil
];
[[
NSNotificationCenter
defaultCenter
]
addObserver
:
self
selector
:
@selector
(
handleAppInBackgound
)
name
:
UIApplicationDidEnterBackgroundNotification
object
:
nil
];
}
/**
* @brief 每次打开APP或返回前台,即Session开始的时候的处理
* @notification didFinishLaunch和willEnterForeground的时候都需要记录
*
* @since 0.0.1
*/
-
(
void
)
handleSessionStart
{
_serialId
=
0
;
_sessionId
=
[[
NSUUID
UUID
]
UUIDString
];
[
GMCache
storeObjectAtDocumentPathWithkey
:
PhobosBeginTime
object
:[
PhobosUtil
currentTime
]];
}
/**
* @brief 应用打开时的处理
*
* @since 0.0.1
*/
-
(
void
)
handleAppFinishLaunch
{
phobosLog
(
@"handleAppFinishLaunch"
);
[
self
handleSessionStart
];
[
self
handleEventDeviceOpened
];
}
/**
* @brief 应用进入前台的处理
*
* @since 0.0.1
*/
-
(
void
)
handleAppInForeground
{
phobosLog
(
@"handleAppInForeground"
);
[
self
handleSessionStart
];
[
self
handleEventDeviceOpened
];
[
self
fetchDataAndSend
];
[
self
handlePVEventAppInForeground
];
}
/**
* @brief 应用进入后台的处理
*
* @since 0.0.1
*/
-
(
void
)
handleAppInBackgound
{
phobosLog
(
@"handleAppInBackgound"
);
[
self
handlePVEventAppInBackgound
];
[
self
handleSessionOver
];
[
self
fetchDataAndSend
];
}
/**
* @brief 会话结束时候的处理
*
* @since 0.0.1
*/
-
(
void
)
handleSessionOver
{
//进入后台的同时,把记录时间同步到服务端,把已使用时间清空
double
beginTime
=
[[
GMCache
fetchObjectAtDocumentPathWithkey
:
PhobosBeginTime
]
doubleValue
];
if
(
beginTime
==
0
)
{
return
;
}
NSDate
*
date
=
[
NSDate
date
];
double
endTime
=
[
date
timeIntervalSince1970
];
NSString
*
usedTime
=
[
NSString
stringWithFormat
:
@"%ld"
,(
long
)(
endTime
-
beginTime
)];
NSDictionary
*
dict
=
@{
@"duration"
:
usedTime
,
@"build_cpu_abi"
:
[
PhobosUtil
currentDeviceCPUType
],
@"cpu_count"
:
[
PhobosUtil
currentDeviceCPUCount
],
@"mac_address"
:
[
PhobosUtil
getMacAddress
],
@"phone_operator"
:
[
PhobosUtil
getTelephonyInfo
],
@"total_memory"
:
[
PhobosUtil
getTotalMemorySize
],
@"run_time"
:
[
PhobosUtil
deviceRunTime
],
@"uuid"
:
[
PhobosDevice
getDEVICEID
],
@"uqid"
:
PhobosSafeString
([
PhobosDevice
getUQID
]),
@"build_version_release"
:
[[
UIDevice
currentDevice
]
systemVersion
],
};
[
OldPhobos
track
:
@"on_app_session_over"
attributes
:
dict
];
[
GMCache
removeObjectAtDocumentPathWithkey
:
PhobosBeginTime
];
//当前session结束之后,把id置为0
_serialId
=
0
;
}
/**
APP从后台到前台的时候,重新初始化pagename等信息
@author zhaiguojun 16-10-11 in (null)
*/
-
(
void
)
handlePVEventAppInForeground
{
if
(
self
.
visibleController
!=
nil
)
{
[
self
onPVStart
:
self
.
visibleController
];
}
}
/**
APP进到后台的时候,把当前pageview时间结束
@author zhaiguojun 16-10-11 in (null)
*/
-
(
void
)
handlePVEventAppInBackgound
{
if
(
self
.
visibleController
!=
nil
)
{
[
self
onPVEnd
:
self
.
visibleController
];
}
}
#pragma mark - track event handler
+
(
void
)
track
:
(
NSString
*
)
eventId
{
[
OldPhobos
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
{
[
OldPhobos
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
];
[
OldPhobos
track
:
dict
[
@"type"
]
attributes
:
json
];
}
else
{
[
OldPhobos
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
];
page
.
inTimeMillis
=
[
PhobosUtil
currentMMTime
];
// 业务层更新
if
(
page
.
updatePVStartBlock
)
{
page
.
updatePVStartBlock
();
}
}
-
(
void
)
onPVEnd
:
(
UIResponder
<
PhobosPVProtocol
>
*
)
page
{
if
(
!
[
PhobosUtil
isNonEmpty
:
page
.
pageName
]
||
!
page
.
needLogPV
)
{
if
(
!
page
.
isFirstEntry
)
{
page
.
isFirstEntry
=
YES
;
}
return
;
}
// 业务层更新
if
(
page
.
updatePVEndBlock
)
{
page
.
updatePVEndBlock
();
}
NSMutableDictionary
*
dict
=
[[
NSMutableDictionary
alloc
]
init
];
@try
{
[
dict
setObject
:
PhobosSafeString
([
PhobosUtil
currentTime
])
forKey
:
@"out"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
inTime
)
forKey
:
@"in"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
pageName
)
forKey
:
@"page_name"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
businessId
)
forKey
:
@"business_id"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
referer
)
forKey
:
@"referrer"
];
[
dict
setObject
:
page
.
referrerLink
.
count
?
page
.
referrerLink
:@[]
forKey
:
@"referrer_link"
];
[
dict
setObject
:
@
(
0
)
forKey
:
@"fake"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
referrerId
)
forKey
:
@"referrer_id"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
extraParam
)
forKey
:
@"extra_param"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
referrerTabName
)
forKey
:
@"referrer_tab_name"
];
[
dict
setObject
:
@
(
page
.
isPush
?
1
:
0
)
forKey
:
@"is_push"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
messageId
)
forKey
:
@"message_id"
];
[
dict
setObject
:
PhobosSafeString
(
page
.
inTimeMillis
)
forKey
:
@"in_time_millis"
];
[
dict
setObject
:
PhobosSafeString
([
PhobosUtil
currentMMTime
])
forKey
:
@"out_time_millis"
];
//首次进来为1,再次进入的时候都为0
[
dict
setObject
:
page
.
isFirstEntry
?
@
(
0
):
@
(
1
)
forKey
:
@"is_first"
];
if
(
!
page
.
isFirstEntry
)
{
page
.
isFirstEntry
=
YES
;
}
if
(
page
.
inTime
.
length
>
0
)
{
// 页面显示时间为空时不记录页面pv事件
[
OldPhobos
track
:
@"page_view"
attributes
:
dict
];
}
}
@catch
(
NSException
*
exception
)
{
phobosLog
(
exception
);
}
}
-
(
void
)
simulativePV
:
(
NSString
*
)
pageName
businessId
:
(
NSString
*
)
bid
referer
:
(
NSString
*
)
referer
{
NSMutableDictionary
*
dict
=
[[
NSMutableDictionary
alloc
]
init
];
@try
{
// fake(模拟)的事件,所以in与out一样,就是这么规定的
[
dict
setObject
:[
PhobosUtil
currentTime
]
forKey
:
@"in"
];
[
dict
setObject
:[
PhobosUtil
currentTime
]
forKey
:
@"out"
];
[
dict
setObject
:
pageName
?:
@""
forKey
:
@"page_name"
];
[
dict
setObject
:
bid
?:
@""
forKey
:
@"business_id"
];
[
dict
setObject
:
referer
?:
@""
forKey
:
@"referrer"
];
[
dict
setObject
:
@
(
1
)
forKey
:
@"fake"
];
[
OldPhobos
track
:
@"page_view"
attributes
:
dict
];
}
@catch
(
NSException
*
exception
)
{
phobosLog
(
exception
);
}
}
#pragma mark - 事件存储、发送
/**
* @brief 将埋点时间封装成词典数据
*
* @since 0.0.1
*/
-
(
NSDictionary
*
)
prepareDictionaryForEvent
:
(
NSString
*
)
eventId
attributes
:
(
NSDictionary
*
)
attributes
{
NSArray
*
referrerLink
=
sharedClient
.
visibleController
.
referrerLink
;
// 对于埋点没有referrer_link的情况,在这里进行统一添加
if
(
!
[
attributes
.
allKeys
containsObject
:
@"referrer_link"
])
{
NSMutableDictionary
*
attributesParams
=
[
NSMutableDictionary
dictionaryWithDictionary
:
attributes
];
if
([
referrerLink
isKindOfClass
:[
NSArray
class
]]
&&
referrerLink
.
count
)
{
[
attributesParams
setValue
:
referrerLink
forKey
:
@"referrer_link"
];
}
else
{
[
attributesParams
setValue
:@[]
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
:
PhobosSafeString
([
PhobosDevice
getIDFA
])
forKey
:
@"idfa"
];
[
deviceParams
setValue
:
PhobosSafeString
([
PhobosDevice
getIDFV
])
forKey
:
@"idfv"
];
[
deviceParams
setValue
:
PhobosSafeString
([
PhobosDevice
getDEVICEID
])
forKey
:
@"device_id"
];
[
deviceParams
setValue
:
PhobosSafeString
([
PhobosDevice
getUQID
])
forKey
:
@"uqid"
];
[
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
:
PhobosSafeString
(
_netStatus
)
forKey
:
@"is_WiFi"
];
[
deviceParams
setValue
:[
PhobosUtil
getIPAddress
:
YES
]
forKey
:
@"ip"
];
[
deviceParams
setValue
:
PhobosSafeString
(
_networkStatus
)
forKey
:
@"net_type"
];
[
deviceParams
setValue
:
PhobosSafeString
([
PhobosUtil
platform
])
forKey
:
@"model"
];
[
deviceParams
setValue
:
PhobosSafeString
([
UIDevice
currentDevice
].
systemVersion
)
forKey
:
@"sys_version"
];
NSMutableDictionary
*
appParams
=
[
NSMutableDictionary
new
];
[
appParams
setValue
:
PhobosSafeString
(
_greyType
)
forKey
:
@"grey_type"
];
[
appParams
setValue
:
PhobosSafeString
(
_appName
)
forKey
:
@"name"
];
[
appParams
setValue
:
PhobosSafeString
(
_appVersion
)
forKey
:
@"version"
];
[
appParams
setValue
:
PhobosSafeString
(
_channelId
)
forKey
:
@"channel"
];
[
appParams
setValue
:
PhobosSafeString
(
_userType
)
forKey
:
@"user_type"
];
[
appParams
setValue
:
PhobosSafeString
(
_currentCityId
)
forKey
:
@"current_city_id"
];
[
appParams
setValue
:
@
(
_serialId
++
)
forKey
:
@"serial_id"
];
if
(
_signingType
==
PhobosSigningTypeDebug
||
_signingType
==
PhobosSigningTypeRelease
)
{
[
dict
setValue
:
@
(
0
)
forKey
:
@"is_release"
];
}
NSString
*
nano_time
=
[
NSString
stringWithFormat
:
@"%lld"
,[[
NSProcessInfo
processInfo
]
systemUptime
]];
[
dict
setValue
:
eventId
forKey
:
@"type"
];
[
dict
setValue
:
appParams
forKey
:
@"app"
];
[
dict
setValue
:
sdkVersion
forKey
:
@"version"
];
[
dict
setValue
:
deviceParams
forKey
:
@"device"
];
[
dict
setValue
:
PhobosSafeString
(
_userId
)
forKey
:
@"user_id"
];
[
dict
setValue
:
PhobosSafeString
(
currentTime
)
forKey
:
@"create_at"
];
// 1584513842 当前时间(秒)
[
dict
setValue
:
PhobosSafeString
(
nano_time
)
forKey
:
@"nano_time"
];
// 1657008897 系统启动后时长(秒)
[
dict
setValue
:
@
(
mach_absolute_time
())
forKey
:
@"absolute_time"
];
// 148237263697318 系统启动后的 CPU 嘀嗒数(纳秒)
[
dict
setValue
:
PhobosSafeString
([
PhobosUtil
currentMMTime
])
forKey
:
@"create_at_millis"
];
// 1584513842753 当前时间戳(毫秒)
[
dict
setValue
:
attributes
forKey
:
@"params"
];
[
dict
setValue
:
PhobosSafeString
(
_sessionId
)
forKey
:
@"app_session_id"
];
}
@catch
(
NSException
*
exception
)
{
phobosLog
(
exception
);
}
return
dict
;
}
/**
* @brief 保存数据到缓存层
*
* @param data 数据
*
* @since 0.0.1
*/
-
(
void
)
save
:
(
NSDictionary
*
)
data
{
if
(
_logEnabled
)
{
phobosLog
([
NSString
stringWithFormat
:
@"save dictionary: %@"
,
data
]);
}
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
)
{
__block
BOOL
checkTimeError
;
for
(
int
i
=
0
;
i
<
dataArray
.
count
;
i
++
)
{
NSDictionary
*
obj
=
dataArray
[
i
];
if
(
obj
[
@"type"
]
&&
[
obj
[
@"type"
]
isEqualToString
:
@"page_view"
])
{
NSDictionary
*
params
=
obj
[
@"params"
];
long
long
objInTime
=
[
params
[
@"in"
]
longLongValue
];
long
long
objOutTime
=
[
params
[
@"out"
]
longLongValue
];
if
(
(
fabsl
(
objInTime
-
pageInTime
))
<
1
||
(
fabsl
(
objOutTime
-
pageOutTime
))
<
1
)
{
phobosLog
(
@"两个埋点inTime 时间差------%f"
,
(
fabsl
(
objInTime
-
pageInTime
)));
phobosLog
(
@"两个埋点outTime 时间差------%f"
,
(
fabsl
(
objOutTime
-
pageOutTime
)));
checkTimeError
=
YES
;
phobosLog
(
@"%s____数据校验失败,PV埋点可能重复"
,
__func__
);
/*
dispatch_async(dispatch_get_main_queue(), ^{
NSString *stringTitle = [NSString stringWithFormat:@"%@ PV埋点可能重复", data];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:stringTitle message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
});*/
break
;
}
}
}
}
}
// 不管成功还是失败 给开发提示方便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
];
}
}
}
/*
从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉
*/
-
(
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
{
NSData
*
JSON
=
[
PhobosUtil
encodeJSON
:
dataArray
];
NSData
*
compressedData
=
[
PhobosUtil
compressData
:
JSON
];
if
(
compressedData
)
{
[
PhobosUtil
sendData
:
compressedData
success
:
^
(
NSInteger
code
)
{
if
(
code
==
200
)
{
phobosLog
(
@"✈ ---------- ✈ data arrived Mars"
);
[
GMCache
removeObjectAtDocumentPathWithkey
:
PhobosTempCacheKey
];
}
}];
}
}
@catch
(
NSException
*
exception
)
{
phobosLog
(
exception
);
}
}
/*
从缓存区获取数据,发给服务器,请求成功的时候,把缓存区的数据删除掉
*/
-
(
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
)
{
if
(
code
==
200
)
{
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.获取缓存区的数据,把新数据追加进去
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
);
}
}
/**
上边方法新加了一个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
);
}
}
#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
)
addNewApi
:
(
NSString
*
)
api
{
for
(
NSString
*
item
in
self
.
APIArray
)
{
if
([
api
isEqualToString
:
item
])
{
break
;
}
[
self
.
APIArray
addObject
:
api
];
}
}
@end
GMPhobos/Classes/Phobos.h
View file @
848180b3
...
...
@@ -116,6 +116,8 @@ NS_ASSUME_NONNULL_BEGIN
从主项目获取当前显示的controller
*/
@property
(
nonatomic
,
copy
)
UIViewController
*
(
^
getTopController
)
(
void
);
/// ua 为了应对苹果14.5之后不允许再直接读取idfa采取的归因方案--ip+ua
@property
(
nonatomic
,
copy
)
NSString
*
wk_ua
;
#pragma mark - 事件采集
...
...
GMPhobos/Classes/Phobos.m
View file @
848180b3
...
...
@@ -6,7 +6,6 @@
//
#import "Phobos.h"
#import "OldPhobos.h"
#import "NewPhobos.h"
static
Phobos
*
sharedClient
=
nil
;
...
...
@@ -14,7 +13,6 @@ static BOOL isGray = YES;
@interface
Phobos
()
@property
(
nonatomic
,
strong
)
OldPhobos
*
oldPhobos
;
@property
(
nonatomic
,
strong
)
NewPhobos
*
rePhobos
;
...
...
@@ -46,22 +44,9 @@ static BOOL isGray = YES;
-
(
instancetype
)
initWithAppName
:
(
NSString
*
)
appName
channelId
:
(
NSString
*
)
channelId
{
if
(
self
=
[
super
init
])
{
//判断 PhobosGray 是否为空值
if
([[
NSUserDefaults
standardUserDefaults
]
objectForKey
:
PhobosGray
]
==
nil
)
{
isGray
=
YES
;
}
else
{
isGray
=
[[[
NSUserDefaults
standardUserDefaults
]
objectForKey
:
PhobosGray
]
boolValue
];
}
// isGray在config接口获取,会保存在NSUserDefaults的PhobosGray中,当次不使用,在下一次冷启动通过该灰度,初始化埋点库
if
(
isGray
)
{
_rePhobos
=
[
NewPhobos
clientWithAppName
:
appName
channelId
:
channelId
];
sharedClient
=
_rePhobos
;
_rePhobos
.
isGray
=
YES
;
}
else
{
_oldPhobos
=
[
OldPhobos
clientWithAppName
:
appName
channelId
:
channelId
];
[
OldPhobos
setSharedClient
:
_oldPhobos
];
sharedClient
=
_oldPhobos
;
}
_rePhobos
=
[
NewPhobos
clientWithAppName
:
appName
channelId
:
channelId
];
sharedClient
=
_rePhobos
;
}
return
self
;
}
...
...
@@ -83,164 +68,124 @@ static BOOL isGray = YES;
}
+
(
void
)
track
:
(
NSString
*
)
eventId
attributes
:
(
NSDictionary
*
)
attributes
{
if
(
isGray
)
{
// 新埋点库支持实时上报数据,取灰度字段去控制
NewPhobos
*
phobos
=
[
NewPhobos
sharedClient
];
[
NewPhobos
track
:
eventId
attributes
:
attributes
sendNow
:
phobos
.
isGraySendNow
];
}
else
{
[
OldPhobos
track
:
eventId
attributes
:
attributes
];
}
// 新埋点库支持实时上报数据,取灰度字段去控制
NewPhobos
*
phobos
=
[
NewPhobos
sharedClient
];
[
NewPhobos
track
:
eventId
attributes
:
attributes
sendNow
:
phobos
.
isGraySendNow
];
}
+
(
void
)
track
:
(
NSString
*
)
eventId
attributes
:
(
NSDictionary
*
)
attributes
sendNow
:
(
BOOL
)
sendNow
{
if
(
isGray
)
{
[
NewPhobos
track
:
eventId
attributes
:
attributes
sendNow
:
sendNow
];
}
else
{
[
OldPhobos
track
:
eventId
attributes
:
attributes
sendNow
:
sendNow
];
}
[
NewPhobos
track
:
eventId
attributes
:
attributes
sendNow
:
sendNow
];
}
+
(
void
)
track
:
(
NSString
*
)
eventId
{
if
(
isGray
)
{
[
NewPhobos
track
:
eventId
];
}
else
{
[
OldPhobos
track
:
eventId
];
}
[
NewPhobos
track
:
eventId
];
}
+
(
void
)
track
:
(
NSString
*
)
eventId
attributes
:
(
NSDictionary
*
)
attributes
currentAPI
:
(
NSString
*
)
currentAPI
{
if
(
isGray
)
{
[
NewPhobos
track
:
eventId
attributes
:
attributes
currentAPI
:
currentAPI
];
}
else
{
[
OldPhobos
track
:
eventId
attributes
:
attributes
currentAPI
:
currentAPI
];
}
[
NewPhobos
track
:
eventId
attributes
:
attributes
currentAPI
:
currentAPI
];
}
+
(
void
)
track
:
(
NSString
*
)
eventId
attributes
:
(
NSDictionary
*
)
attributes
sendNow
:
(
BOOL
)
sendNow
currentAPI
:
(
NSString
*
)
currentAPI
{
if
(
isGray
)
{
[
NewPhobos
track
:
eventId
attributes
:
attributes
sendNow
:
sendNow
currentAPI
:
currentAPI
];
}
else
{
[
OldPhobos
track
:
eventId
attributes
:
attributes
sendNow
:
sendNow
currentAPI
:
currentAPI
];
}
[
NewPhobos
track
:
eventId
attributes
:
attributes
sendNow
:
sendNow
currentAPI
:
currentAPI
];
}
+
(
void
)
track
:
(
NSString
*
)
eventId
currentAPI
:
(
NSString
*
)
currentAPI
{
if
(
isGray
)
{
[
NewPhobos
track
:
eventId
currentAPI
:
currentAPI
];
}
else
{
[
OldPhobos
track
:
eventId
currentAPI
:
currentAPI
];
}
[
NewPhobos
track
:
eventId
currentAPI
:
currentAPI
];
}
/**
* @brief PV事件开始。当controller viewWillAppear时调用
*/
-
(
void
)
onPVStart
:
(
UIResponder
<
PhobosPVProtocol
>
*
)
page
{
[
_rePhobos
onPVStart
:
page
];
[
_oldPhobos
onPVStart
:
page
];
}
/**
* @brief PV事件结束。当controller viewWillDisAppear时调用
*/
-
(
void
)
onPVEnd
:
(
UIResponder
<
PhobosPVProtocol
>
*
)
page
{
[
_rePhobos
onPVEnd
:
page
];
[
_oldPhobos
onPVEnd
:
page
];
}
/**
* 为隐私弹窗触发on_app_session_over埋点使用
*/
-
(
void
)
handleAppInBackgound
{
if
(
isGray
)
{
[
_rePhobos
handleAppInBackgound
];
}
else
{
[
_oldPhobos
handleAppInBackgound
];
}
[
_rePhobos
handleAppInBackgound
];
}
-
(
void
)
setCaptureNullExpection
:
(
void
(
^
)(
NSString
*
_Nonnull
,
NSDictionary
*
_Nonnull
))
captureNullExpection
{
_captureNullExpection
=
captureNullExpection
;
_rePhobos
.
captureNullExpection
=
captureNullExpection
;
_oldPhobos
.
captureNullExpection
=
captureNullExpection
;
}
-
(
void
)
setNetStatus
:
(
NSString
*
)
netStatus
{
_netStatus
=
netStatus
;
_rePhobos
.
netStatus
=
netStatus
;
_oldPhobos
.
netStatus
=
netStatus
;
}
-
(
void
)
setNetworkStatus
:
(
NSString
*
)
networkStatus
{
_networkStatus
=
networkStatus
;
_rePhobos
.
networkStatus
=
networkStatus
;
_oldPhobos
.
networkStatus
=
networkStatus
;
}
-
(
void
)
setLogEnabled
:
(
BOOL
)
logEnabled
{
_logEnabled
=
logEnabled
;
_rePhobos
.
logEnabled
=
logEnabled
;
_oldPhobos
.
logEnabled
=
logEnabled
;
}
-
(
void
)
setUserId
:
(
NSString
*
)
userId
{
_userId
=
userId
;
_rePhobos
.
userId
=
userId
;
_oldPhobos
.
userId
=
userId
;
}
-
(
void
)
setCurrentCityId
:
(
NSString
*
)
currentCityId
{
_currentCityId
=
currentCityId
;
_rePhobos
.
currentCityId
=
currentCityId
;
_oldPhobos
.
currentCityId
=
currentCityId
;
}
-
(
void
)
setGps
:
(
CLLocation
*
)
gps
{
_gps
=
gps
;
_rePhobos
.
gps
=
gps
;
_oldPhobos
.
gps
=
gps
;
}
-
(
void
)
setUserType
:
(
NSMutableDictionary
*
)
userType
{
_userType
=
userType
;
_rePhobos
.
userType
=
userType
;
_oldPhobos
.
userType
=
userType
;
}
-
(
void
)
setApiHost
:
(
NSString
*
)
apiHost
{
_apiHost
=
apiHost
;
_rePhobos
.
apiHost
=
apiHost
;
_oldPhobos
.
apiHost
=
apiHost
;
}
-
(
void
)
setServerAPI
:
(
NSString
*
)
serverAPI
{
_serverAPI
=
serverAPI
;
_rePhobos
.
serverAPI
=
serverAPI
;
_oldPhobos
.
serverAPI
=
serverAPI
;
}
-
(
void
)
setCookie
:
(
NSString
*
)
cookie
{
_cookie
=
cookie
;
_rePhobos
.
cookie
=
cookie
;
_oldPhobos
.
cookie
=
cookie
;
}
-
(
void
)
setGreyType
:
(
NSString
*
)
greyType
{
_greyType
=
greyType
;
_rePhobos
.
greyType
=
greyType
;
_oldPhobos
.
greyType
=
greyType
;
}
-
(
void
)
setSigningType
:
(
PhobosSigningType
)
signingType
{
_signingType
=
signingType
;
_rePhobos
.
signingType
=
signingType
;
_oldPhobos
.
signingType
=
signingType
;
}
-
(
void
)
setGetTopController
:
(
UIViewController
*
_Nonnull
(
^
)(
void
))
getTopController
{
_getTopController
=
getTopController
;
_rePhobos
.
getTopController
=
getTopController
;
_oldPhobos
.
getTopController
=
getTopController
;
}
-
(
void
)
setWk_ua
:
(
NSString
*
)
wk_ua
{
_wk_ua
=
wk_ua
;
_rePhobos
.
wk_ua
=
wk_ua
;
}
@end
...
...
GMPhobos/Classes/PhobosConfig.h
View file @
848180b3
...
...
@@ -24,8 +24,6 @@ static NSString *sdkVersion = @"2.0.5";
#define phobosLog(...)
#endif
#define PhobosGray @"PhobosGray"
#define PhobosBeginTime @"PhobosBeginTime" //记录APP打开|从后台启动时的时间戳
#define PhobosEndTime @"PhobosEndTime" //记录APP退出|退到后台时的时间戳
#define PhobosCacheKey @"PhobosCacheKey" //old 存放持久化埋点数据的key
...
...
GMPhobos/Classes/PhobosDevice.h
View file @
848180b3
...
...
@@ -6,14 +6,14 @@
//
#import <Foundation/Foundation.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/ASIdentifierManager.h>
NS_ASSUME_NONNULL_BEGIN
extern
NSString
*
const
KprivatePolicyNotification
;
typedef
void
(
^
PhobosGetIDFACompleteBlock
)(
NS
Dictionary
*
dict
);
typedef
void
(
^
PhobosGetIDFACompleteBlock
)(
NS
String
*
idfa
,
NSInteger
authoriza
);
@interface
PhobosDevice
:
NSObject
...
...
@@ -35,6 +35,10 @@ typedef void (^PhobosGetIDFACompleteBlock)(NSDictionary *dict);
/// 用户是否点击了隐私协议 点击同意之后才会获取 device_id
+
(
BOOL
)
showPrivatePolicy
;
/// 请求获取IDFA授权 (ios14 异步回调, ios14之前直接回调)
/// @param completeBlock 回调执行结果
+
(
void
)
requestTrackingAuthorizaWithCompletionHandler
:(
PhobosGetIDFACompleteBlock
)
completeBlock
;
@end
NS_ASSUME_NONNULL_END
GMPhobos/Classes/PhobosDevice.m
View file @
848180b3
...
...
@@ -10,7 +10,6 @@
#import <SAMKeychain/SAMKeychain.h>
#import <GMCache/GMCache.h>
#import "PhobosUtil.h"
//#import <AppTrackingTransparency/AppTrackingTransparency.h>
NSString
*
const
KprivatePolicyNotification
=
@"gm_popup_private_policy_notification"
;
//钥匙串账户
...
...
@@ -29,7 +28,7 @@ static dispatch_queue_t queue;
#pragma mark - getIDFA
/
/
同步获取IDFA 直接从钥匙串中获取 适配ATTrackingManagerAuthorization可使用下面方法
/
*
同步获取IDFA 直接从钥匙串中获取 适配ATTrackingManagerAuthorization可使用下面方法
+ (NSString *)getIDFA {
if (![PhobosDevice showPrivatePolicy]) {
return defaultIDFA;
...
...
@@ -47,58 +46,66 @@ static dispatch_queue_t queue;
keyChainIDFA = defaultIDFA;
}
return keyChainIDFA;
}
}
*/
/** iOS 14 适配 ATTrackingManagerAuthorizationStatus
/** iOS 14 适配 ATTrackingManagerAuthorizationStatus
*/
//同步获取IDFA 直接从钥匙串中获取
+
(
NSString
*
)
getIDFA
{
if
(
!
[
PhobosDevice
showPrivatePolicy
])
{
return
defaultIDFA
;
}
//直接从钥匙串中获取
NSString
*
keyChainIDFA
=
[
self
getKeyChainService
:
IDFA
account
:
keyChainAccount
];
//判断获取的钥匙串是否为空
if
([
self
checkIdfaIsNull
:
keyChainIDFA
])
{
if (@available(iOS 14, *)) {
// 如果是iOS14 未从钥匙串中获取到idfa直接按默认的
keyChainIDFA = defaultIDFA;
} else {
keyChainIDFA = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString;
if ([self checkIdfaIsNull:keyChainIDFA]) {
[self saveKeyChainWithValue:keyChainIDFA service:IDFA account:keyChainAccount];
}
}
keyChainIDFA
=
defaultIDFA
;
}
return
keyChainIDFA
;
}
//同步获取IDFA
+ (void)getIDFAString:(PhobosGetIDFACompleteBlock)completeBlock {
/// 请求获取IDFA授权
/// @param completeBlock 回调执行结果
+
(
void
)
requestTrackingAuthorizaWithCompletionHandler
:
(
PhobosGetIDFACompleteBlock
)
completeBlock
{
// iOS14及以上版本需要先请求权限
if
(
@available
(
iOS
14
,
*
))
{
__block NSString *idfaString = @"";
[
ATTrackingManager
requestTrackingAuthorizationWithCompletionHandler
:
^
(
ATTrackingManagerAuthorizationStatus
status
)
{
BOOL isAuthor = NO;
if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
isAuthor = YES;
idfaString = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString;
}
dispatch_async
(
dispatch_get_main_queue
(),
^
{
NSString
*
idfaString
;
if
(
status
==
ATTrackingManagerAuthorizationStatusAuthorized
)
{
idfaString
=
[[
ASIdentifierManager
sharedManager
]
advertisingIdentifier
].
UUIDString
;
if
(
!
[
self
checkIdfaIsNull
:
idfaString
])
{
[
self
saveKeyChainWithValue
:
idfaString
service
:
IDFA
account
:
keyChainAccount
];
}
}
else
{
// NSLog(@"请在设置-隐私-跟踪中允许App请求跟踪");
}
if
(
completeBlock
)
{
completeBlock
(
idfaString
,
status
);
}
});
}];
}
else
{
// iOS14以下版本依然使用原方式访问IDFA
// 判断在设置-隐私里用户是否打开了广告跟踪
NSInteger
status
=
0
;
NSString
*
idfaString
;
if
([[
ASIdentifierManager
sharedManager
]
isAdvertisingTrackingEnabled
])
{
idfaString
=
[[[
ASIdentifierManager
sharedManager
]
advertisingIdentifier
]
UUIDString
];
if
(
!
[
self
checkIdfaIsNull
:
idfaString
])
{
// 标示 用户已授权打开
status
=
3
;
[
self
saveKeyChainWithValue
:
idfaString
service
:
IDFA
account
:
keyChainAccount
];
}
completeBlock(@{@"idfa": idfaString, @"is_author": @(isAuthor)});
}];
} else {
// 使用原方式访问IDFA
NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
BOOL isAuthor = NO;
if (![self checkIdfaIsNull:idfaString]) {
isAuthor = YES;
[self saveKeyChainWithValue:idfaString service:IDFA account:keyChainAccount];
}
else
{
// 表示用户拒绝打开
status
=
2
;
// NSLog(@"请在设置-隐私-广告中打开广告跟踪功能");
}
if
(
completeBlock
)
{
completeBlock
(
idfaString
,
status
);
}
completeBlock(@{@"idfa": idfaString, @"is_author": @(isAuthor)});
}
}
*/
#pragma mark - 对IDFA判空处理
//检查idfa 是否为空
...
...
@@ -106,7 +113,6 @@ static dispatch_queue_t queue;
return
!
idfa
.
length
||
[
idfa
isEqualToString
:
defaultIDFA
];
}
#pragma mark - getIDFV
+
(
NSString
*
)
getIDFV
{
if
(
!
[
PhobosDevice
showPrivatePolicy
])
{
...
...
@@ -115,7 +121,6 @@ static dispatch_queue_t queue;
return
[[[
UIDevice
currentDevice
]
identifierForVendor
]
UUIDString
];
}
#pragma mark - getDEVICEID
+
(
NSString
*
)
getDEVICEID
{
if
(
!
[
PhobosDevice
showPrivatePolicy
])
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment