Skip to content

Commit

Permalink
Release 4.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
王洋洋 committed Jan 12, 2023
1 parent ea4b3b7 commit f5f34c0
Show file tree
Hide file tree
Showing 34 changed files with 238 additions and 168 deletions.
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SensorsAnalyticsSDK"
s.version = "4.5.1"
s.version = "4.5.2"
s.summary = "The official iOS SDK of Sensors Analytics."
s.homepage = "http://www.sensorsdata.cn"
s.source = { :git => 'https://github.com/sensorsdata/sa-sdk-ios.git', :tag => "v#{s.version}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ NS_ASSUME_NONNULL_BEGIN
/// 是否为 H5 打通事件
@property (nonatomic, assign) BOOL hybridH5;

/// is instant event or not, default is NO
@property (nonatomic, assign) BOOL isInstantEvent;

- (instancetype)initWithH5Event:(NSDictionary *)event;

/// 校验事件名称
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ - (instancetype)init {

_ignoreRemoteConfig = NO;
_hybridH5 = NO;
_isInstantEvent = NO;
}
return self;
}
Expand Down Expand Up @@ -90,6 +91,10 @@ - (instancetype)initWithH5Event:(NSDictionary *)event {

[properties removeObjectsForKeys:@[@"_nocache", @"server_url", kSAAppVisualProperties, kSAEventProject, kSAEventToken, kSAEventCommonOptionalPropertyTime]];
_properties = properties;
NSNumber *isInstantEvent = event[kSAInstantEventKey];
if ([isInstantEvent isKindOfClass:[NSNumber class]]) {
_isInstantEvent = [isInstantEvent boolValue];
}
}
return self;
}
Expand Down
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/Core/Builder/SAIDFAHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ + (NSString *)idfa {
return nil;
}

NSUUID *uuid = ((NSUUID * (*)(id, SEL))[idfaManager methodForSelector:advertisingIdentifierSelector])(idfaManager, advertisingIdentifierSelector);;
NSUUID *uuid = ((NSUUID * (*)(id, SEL))[idfaManager methodForSelector:advertisingIdentifierSelector])(idfaManager, advertisingIdentifierSelector);
NSString *idfa = [uuid UUIDString];
// 在 iOS 10.0 以后,当用户开启限制广告跟踪,advertisingIdentifier 的值将是全零
// 00000000-0000-0000-0000-000000000000
Expand Down
26 changes: 1 addition & 25 deletions SensorsAnalyticsSDK/Core/Builder/SAIdentifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ - (BOOL)identify:(NSString *)anonymousId {
}

[self updateAnonymousId:anonymousId];
[self bindIdentity:kSAIdentitiesAnonymousId value:anonymousId];
return YES;
}

Expand All @@ -131,10 +130,6 @@ - (void)archiveAnonymousId:(NSString *)anonymousId {
- (void)resetAnonymousId {
NSString *anonymousId = [SAIdentifier hardwareID];
[self updateAnonymousId:anonymousId];
// 只有 identities 包含 $identity_anonymous_id 时需要更新内容
if (self.identities[kSAIdentitiesAnonymousId]) {
[self bindIdentity:kSAIdentitiesAnonymousId value:anonymousId];
}
}

- (void)updateAnonymousId:(NSString *)anonymousId {
Expand Down Expand Up @@ -409,7 +404,7 @@ - (NSDictionary *)mergeH5Identities:(NSDictionary *)identities eventType:(SAEven

NSMutableDictionary *newIdentities = [NSMutableDictionary dictionaryWithDictionary:identities];
// 移除 H5 事件 identities 中的保留 ID,不允许 H5 绑定保留 ID
[newIdentities removeObjectsForKeys:@[kSAIdentitiesUniqueID, kSAIdentitiesUUID, kSAIdentitiesAnonymousId]];
[newIdentities removeObjectsForKeys:@[kSAIdentitiesUniqueID, kSAIdentitiesUUID]];
[newIdentities addEntriesFromDictionary:self.identities];

// 当 identities 不存在( 2.0 版本)或 identities 中包含自定义 login_id (3.0 版本)时
Expand Down Expand Up @@ -589,24 +584,6 @@ - (NSDictionary *)unarchiveIdentitiesWithKey:(NSString *)loginIDKey {
NSDictionary *cache = [self decodeIdentities];
NSMutableDictionary *identities = [NSMutableDictionary dictionaryWithDictionary:cache];

// 当 cache 不存在时表示从 v2.0 升级到 v3.0 版本,兼容 v2.0 的 anonymous_id
// 存在从 v3.0 降级至 v2.0 且修改了 anonymous_id 又升级回 v3.0 的情况
// 所以当 identities[kSAIdentitiesAnonymousId] 存在时,需要使用本地的 anonymous_id 更新其内容
if (!cache || identities[kSAIdentitiesAnonymousId]) {

NSString *anonymousId;
#if TARGET_OS_IOS
// 读取 KeyChain 中保存的 anonymousId,
// 当前逻辑时 self.anonymousId 还未设置,因此需要手动读取 keychain 数据
anonymousId = [SAKeyChainItemWrapper saUdid];
#endif
if (!anonymousId) {
// 读取本地文件中保存的 anonymouId
anonymousId = [[SAStoreManager sharedInstance] objectForKey:kSAEventDistinctId];
}
identities[kSAIdentitiesAnonymousId] = anonymousId;
}

// SDK 取 IDFV 或 uuid 为设备唯一标识,已知情况下未发现获取不到 IDFV 的情况
if (!identities[kSAIdentitiesUniqueID] && !identities[kSAIdentitiesUUID] ) {
NSString *key = kSAIdentitiesUUID;
Expand Down Expand Up @@ -666,7 +643,6 @@ - (NSDictionary *)unarchiveIdentitiesWithKey:(NSString *)loginIDKey {
NSMutableDictionary *newIdentities = [NSMutableDictionary dictionary];
newIdentities[kSAIdentitiesUniqueID] = identities[kSAIdentitiesUniqueID];
newIdentities[kSAIdentitiesUUID] = identities[kSAIdentitiesUUID];
newIdentities[kSAIdentitiesAnonymousId] = identities[kSAIdentitiesAnonymousId];
identities = newIdentities;
}
// 当 v2.0 版本状态为未登录状态时,直接清空本地保存的 loginIDKey 文件内容
Expand Down
3 changes: 3 additions & 0 deletions SensorsAnalyticsSDK/Core/Flow/SAFlowData.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ typedef void(^SAFlowDataCompletion)(SAFlowData *output);
/// ID-Mapping 相关
@property (nonatomic, strong, nullable) SAIdentifier *identifier;

/// mark event is instant or not
@property (nonatomic,assign) BOOL isInstantEvent;

#pragma mark - store

/// 单条数据记录
Expand Down
10 changes: 10 additions & 0 deletions SensorsAnalyticsSDK/Core/Flow/SAFlowData.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#import "SAFlowData.h"
#import "SAIdentifier.h"
#import "SAConstants+Private.h"

static NSString * const kSAFlowDataEventObject = @"event_object";
static NSString * const kSAFlowDataIdentifier = @"identifier";
Expand Down Expand Up @@ -109,6 +110,7 @@ - (void)setRecordIDs:(NSArray<NSString *> *)recordIDs {

- (void)setEventObject:(SABaseEventObject *)eventObject {
[self setParamWithKey:kSAFlowDataEventObject value:eventObject];
self.isInstantEvent = eventObject.isInstantEvent;
}

- (SABaseEventObject *)eventObject {
Expand Down Expand Up @@ -163,4 +165,12 @@ - (NSInteger)repeatCount {
return [self.param[kSAFlowDataRepeatCount] integerValue];
}

- (void)setIsInstantEvent:(BOOL)isInstantEvent {
[self setParamWithKey:kSAInstantEventKey value:[NSNumber numberWithBool:isInstantEvent]];
}

-(BOOL)isInstantEvent {
return [self.param[kSAInstantEventKey] boolValue];
}

@end
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/Core/Flow/SAFlowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern NSString * const kSAFlushFlowId;
- (SAFlowObject *)flowForID:(NSString *)flowID;

- (void)startWithFlowID:(NSString *)flowID input:(SAFlowData *)input completion:(nullable SAFlowDataCompletion)completion;
- (void)startWithFlow:(SAFlowObject *)flow input:(SAFlowData *)input completion:(SAFlowDataCompletion)completion;
- (void)startWithFlow:(SAFlowObject *)flow input:(SAFlowData *)input completion:(nullable SAFlowDataCompletion)completion;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (void)processWithInput:(SAFlowData *)input completion:(SAFlowDataCompletion)co
// 判断本地数据库中未上传的数量
if (!input.eventObject.isSignUp &&
[self.eventStore recordCountWithStatus:SAEventRecordStatusNone] <= input.configOptions.flushBulkSize &&
input.configOptions.debugMode == SensorsAnalyticsDebugOff) {
input.configOptions.debugMode == SensorsAnalyticsDebugOff && !input.isInstantEvent) {
input.state = SAFlowStateStop;
}
return completion(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (void)processWithInput:(SAFlowData *)input completion:(SAFlowDataCompletion)co

// 查询数据
NSInteger queryCount = input.configOptions.debugMode != SensorsAnalyticsDebugOff ? 1 : 50;
NSArray<SAEventRecord *> *records = [self.eventStore selectRecords:queryCount];
NSArray<SAEventRecord *> *records = [self.eventStore selectRecords:queryCount isInstantEvent:input.isInstantEvent];
if (records.count == 0) {
input.state = SAFlowStateStop;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ - (void)processWithInput:(SAFlowData *)input completion:(SAFlowDataCompletion)co
SALogDebug(@"\n【track event】:\n%@", event);
}

input.record = [[SAEventRecord alloc] initWithEvent:event type:@"POST"];
SAEventRecord *record = [[SAEventRecord alloc] initWithEvent:event type:@"POST"];
record.isInstantEvent = input.eventObject.isInstantEvent;
input.record = record;
completion(input);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ @interface SAConfigOptions ()

@implementation SAFlushHTTPBodyInterceptor

- (void)processWithInput:(SAFlowData *)input completion:(SAFlowDataCompletion)completion {
NSParameterAssert(input.configOptions);
NSParameterAssert(input.records.count > 0);

input.HTTPBody = [self buildBodyWithInput:input];
completion(input);
}

// 2. 完成 HTTP 请求拼接
- (NSData *)buildBodyWithJSONString:(NSString *)jsonString isEncrypted:(BOOL)isEncrypted {
- (NSData *)buildBodyWithInput:(SAFlowData *)input {
BOOL isEncrypted = input.configOptions.enableEncrypt && input.records.firstObject.isEncrypted;
NSString *jsonString = input.json;
int gzip = 1; // gzip = 9 表示加密编码
if (isEncrypted) {
// 加密数据已{经做过 gzip 压缩和 base64 处理了,就不需要再处理。
Expand All @@ -50,16 +60,10 @@ - (NSData *)buildBodyWithJSONString:(NSString *)jsonString isEncrypted:(BOOL)isE
int hashCode = [jsonString sensorsdata_hashCode];
jsonString = [jsonString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
NSString *bodyString = [NSString stringWithFormat:@"crc=%d&gzip=%d&data_list=%@", hashCode, gzip, jsonString];
if (input.isInstantEvent) {
bodyString = [bodyString stringByAppendingString:@"&instant_event=true"];
}
return [bodyString dataUsingEncoding:NSUTF8StringEncoding];
}

- (void)processWithInput:(SAFlowData *)input completion:(SAFlowDataCompletion)completion {
NSParameterAssert(input.configOptions);
NSParameterAssert(input.records.count > 0);

BOOL isEncrypted = input.configOptions.enableEncrypt && input.records.firstObject.isEncrypted;
input.HTTPBody = [self buildBodyWithJSONString:input.json isEncrypted:isEncrypted];
completion(input);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ - (void)processWithInput:(SAFlowData *)input completion:(SAFlowDataCompletion)co
SAFlowData *inputData = [[SAFlowData alloc] init];
inputData.cookie = input.cookie;
inputData.repeatCount = input.repeatCount + 1;
inputData.isInstantEvent = input.isInstantEvent;
// 当前已处于 serialQueue,不必再切队列
[SAFlowManager.sharedInstance startWithFlowID:kSAFlushFlowId input:inputData completion:^(SAFlowData * _Nonnull output) {
completion(output);
Expand Down
3 changes: 3 additions & 0 deletions SensorsAnalyticsSDK/Core/SAConfigOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ NS_ASSUME_NONNULL_BEGIN
/// 是否禁用采集 deviceId
@property (nonatomic, assign) BOOL disableDeviceId;

/// set instant events
@property (nonatomic, copy) NSArray<NSString *> *instantEvents;

- (void)registerStorePlugin:(id<SAStorePlugin>)plugin;

/**
Expand Down
7 changes: 7 additions & 0 deletions SensorsAnalyticsSDK/Core/SAConfigOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ - (id)copyWithZone:(nullable NSZone *)zone {
options.eventSessionTimeout = self.eventSessionTimeout;
options.disableDeviceId = self.disableDeviceId;
options.propertyPlugins = self.propertyPlugins;
options.instantEvents = self.instantEvents;

#if TARGET_OS_IOS
// 支持 https 自签证书
Expand Down Expand Up @@ -231,6 +232,12 @@ - (void)setEventSessionTimeout:(NSInteger)eventSessionTimeout {
}
}

- (void)setInstantEvents:(NSArray<NSString *> *)instantEvents {
if ([instantEvents isKindOfClass:[NSArray class]]) {
_instantEvents = instantEvents;
}
}

- (void)registerStorePlugin:(id<SAStorePlugin>)plugin {
[self.storePlugins addObject:plugin];
}
Expand Down
3 changes: 3 additions & 0 deletions SensorsAnalyticsSDK/Core/SAConstants+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,6 @@ extern NSString * const kSAWebVisualEventName;
extern NSString * const kSAAppVisualProperties;
/// 内嵌 H5 可视化全埋点 Web 自定义属性配置
extern NSString * const kSAWebVisualProperties;

/// is instant event
extern NSString * const kSAInstantEventKey;
4 changes: 4 additions & 0 deletions SensorsAnalyticsSDK/Core/SAConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,7 @@ void sensorsdata_dispatch_safe_sync(dispatch_queue_t queue,DISPATCH_NOESCAPE dis
SALimitKey const SALimitKeyIDFA = @"SALimitKeyIDFA";
SALimitKey const SALimitKeyIDFV = @"SALimitKeyIDFV";
SALimitKey const SALimitKeyCarrier = @"SALimitKeyCarrier";


/// is instant event
NSString * const kSAInstantEventKey = @"is_instant_event";
Loading

0 comments on commit f5f34c0

Please sign in to comment.