Skip to content

Commit

Permalink
Release 4.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jg zhu authored and jg zhu committed Dec 21, 2021
1 parent 22de274 commit 08a3e58
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 184 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.1.2"
s.version = "4.1.3"
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
11 changes: 4 additions & 7 deletions SensorsAnalyticsSDK/Core/Builder/EventObject/SABaseEventObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,8 @@ - (void)addModuleProperties:(NSDictionary *)properties {
- (void)addSuperProperties:(NSDictionary *)properties {
}

- (void)addCustomProperties:(NSDictionary *)properties error:(NSError **)error {
NSMutableDictionary *props = [SAPropertyValidator validProperties:properties validator:self error:error];
if (*error) {
return;
}
- (void)addCustomProperties:(NSDictionary *)properties {
NSMutableDictionary *props = [SAPropertyValidator validProperties:properties validator:self];

[self.properties addEntriesFromDictionary:props];

Expand Down Expand Up @@ -141,13 +138,13 @@ - (void)correctDeviceID:(NSString *)deviceID {

- (id)sensorsdata_validKey:(NSString *)key value:(id)value error:(NSError *__autoreleasing _Nullable *)error {
if (![key conformsToProtocol:@protocol(SAPropertyKeyProtocol)]) {
*error = SAPropertyError(10004, @"Property Key should by %@", [key class]);
*error = SAPropertyError(10004, @"Property Key: %@ must be a string", key);
return nil;
}

// key 校验
[(id <SAPropertyKeyProtocol>)key sensorsdata_isValidPropertyKeyWithError:error];
if (*error) {
if (*error && (*error).code != SAValidatorErrorOverflow) {
return nil;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ NS_ASSUME_NONNULL_BEGIN

/// 添加自定义属性(属性校验不通过时, error 包含错误信息)
/// @param properties 自定义属性
- (void)addCustomProperties:(NSDictionary *)properties error:(NSError ** _Nullable)error;
- (void)addCustomProperties:(NSDictionary *)properties;

/// 添加前向页面标题
/// @param referrerTitle 前向页面标题
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
//

#import <Foundation/Foundation.h>
#import "SAValidator.h"

NS_ASSUME_NONNULL_BEGIN

#define SAPropertyError(errorCode, fromat, ...) \
[NSError errorWithDomain:@"SensorsAnalyticsErrorDomain" \
code:errorCode \
userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:fromat,##__VA_ARGS__]}] \

@protocol SAPropertyKeyProtocol <NSObject>

- (void)sensorsdata_isValidPropertyKeyWithError:(NSError **)error;
Expand Down Expand Up @@ -68,8 +64,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface SAPropertyValidator : NSObject

+ (NSMutableDictionary *)validProperties:(NSDictionary *)properties error:(NSError **)error;
+ (NSMutableDictionary *)validProperties:(NSDictionary *)properties validator:(id<SAEventPropertyValidatorProtocol>)validator error:(NSError **)error;
+ (NSMutableDictionary *)validProperties:(NSDictionary *)properties;
+ (NSMutableDictionary *)validProperties:(NSDictionary *)properties validator:(id<SAEventPropertyValidatorProtocol>)validator;

@end

Expand Down
44 changes: 18 additions & 26 deletions SensorsAnalyticsSDK/Core/Builder/EventObject/SAPropertyValidator.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,19 @@
#import "SAConstants+Private.h"
#import "SACommonUtility.h"
#import "SADateFormatter.h"
#import "SAValidator.h"

static NSUInteger const kSAPropertyLengthLimitation = 8191;
#import "SensorsAnalyticsSDK+Private.h"
#import "SALog.h"

@implementation NSString (SAProperty)

- (void)sensorsdata_isValidPropertyKeyWithError:(NSError *__autoreleasing _Nullable *)error {
if (![SAValidator isValidKey: self]) {
*error = SAPropertyError(10001, @"property name[%@] is not valid", self);
}
[SAValidator validKey:self error:error];
}

- (id)sensorsdata_propertyValueWithKey:(NSString *)key error:(NSError *__autoreleasing _Nullable *)error {
NSInteger maxLength = kSAPropertyLengthLimitation;
if ([key isEqualToString:@"app_crashed_reason"]) {
maxLength = kSAPropertyLengthLimitation * 2;
}
NSUInteger length = [self lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
if (length > maxLength) {
//截取再拼接 $ 末尾,替换原数据
NSMutableString *newString = [NSMutableString stringWithString:[SACommonUtility subByteString:self byteLength:maxLength - 1]];
[newString appendString:@"$"];
return [newString copy];
if (length > kSAPropertyValueMaxLength) {
SALogWarn(@"%@'s length is longer than %ld", self, kSAPropertyValueMaxLength);
}
return self;
}
Expand All @@ -58,7 +48,7 @@ - (id)sensorsdata_propertyValueWithKey:(NSString *)key error:(NSError *__autorel
@implementation NSNumber (SAProperty)

- (id)sensorsdata_propertyValueWithKey:(NSString *)key error:(NSError *__autoreleasing _Nullable *)error {
return self;
return [self isEqualToNumber:NSDecimalNumber.notANumber] || [self isEqualToNumber:@(INFINITY)] ? nil : self;
}

@end
Expand Down Expand Up @@ -121,12 +111,12 @@ @implementation NSDictionary (SAProperty)

- (id)sensorsdata_validKey:(NSString *)key value:(id)value error:(NSError *__autoreleasing _Nullable *)error {
if (![key conformsToProtocol:@protocol(SAPropertyKeyProtocol)]) {
*error = SAPropertyError(10004, @"Property Key should by %@", [key class]);
*error = SAPropertyError(10004, @"Property Key: %@ must be a string", key);
return nil;
}

[(id <SAPropertyKeyProtocol>)key sensorsdata_isValidPropertyKeyWithError:error];
if (*error) {
if (*error && (*error).code != SAValidatorErrorOverflow) {
return nil;
}

Expand All @@ -143,22 +133,24 @@ - (id)sensorsdata_validKey:(NSString *)key value:(id)value error:(NSError *__aut

@implementation SAPropertyValidator

+ (NSMutableDictionary *)validProperties:(NSDictionary *)properties error:(NSError **)error {
return [self validProperties:properties validator:properties error:error];
+ (NSMutableDictionary *)validProperties:(NSDictionary *)properties {
return [self validProperties:properties validator:properties];
}

+ (NSMutableDictionary *)validProperties:(NSDictionary *)properties validator:(id<SAEventPropertyValidatorProtocol>)validator error:(NSError **)error {
+ (NSMutableDictionary *)validProperties:(NSDictionary *)properties validator:(id<SAEventPropertyValidatorProtocol>)validator {
if (![properties isKindOfClass:[NSDictionary class]] || ![validator conformsToProtocol:@protocol(SAEventPropertyValidatorProtocol)]) {
return nil;
}
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (id key in properties) {
id value = [validator sensorsdata_validKey:key value:properties[key] error:error];
if (*error) {
return nil;
NSError *error = nil;
id value = [validator sensorsdata_validKey:key value:properties[key] error:&error];
if (error) {
SALogError(@"%@",error.localizedDescription);
}
if (value) {
result[key] = value;
}

result[key] = value;
}
return result;
}
Expand Down
30 changes: 7 additions & 23 deletions SensorsAnalyticsSDK/Core/Builder/EventObject/SATrackEventObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,20 @@
#import "SAPresetProperty.h"
#import "SAValidator.h"
#import "SALog.h"
#import "SensorsAnalyticsSDK+Private.h"

@implementation SATrackEventObject

- (instancetype)initWithEventId:(NSString *)eventId {
self = [super init];
if (self) {
self.eventId = eventId;
self.eventId = eventId && ![eventId isKindOfClass:[NSString class]] ? [NSString stringWithFormat:@"%@", eventId] : eventId;
}
return self;
}

- (void)validateEventWithError:(NSError **)error {
if (self.eventId && ![self.eventId isKindOfClass:NSString.class]) {
*error = SAPropertyError(20000, @"Event name must be NSString. got: %@ %@", [self.eventId class], self.eventId);
return;
}
if (self.eventId == nil || [self.eventId length] == 0) {
*error = SAPropertyError(20001, @"Event name should not be empty or nil");
return;
}
if (![SAValidator isValidKey:self.eventId]) {
*error = SAPropertyError(20002, @"Event name[%@] not valid", self.eventId);
return;
}
[SAValidator validKey:self.eventId error:error];
}

#pragma makr - SAEventBuildStrategy
Expand All @@ -75,11 +65,8 @@ - (void)addSuperProperties:(NSDictionary *)properties {
}
}

- (void)addCustomProperties:(NSDictionary *)properties error:(NSError **)error {
[super addCustomProperties:properties error:error];
if (*error) {
return;
}
- (void)addCustomProperties:(NSDictionary *)properties {
[super addCustomProperties:properties];

// 如果传入自定义属性中的 $lib_method 为 String 类型,需要进行修正处理
id libMethod = self.properties[kSAEventPresetPropertyLibMethod];
Expand Down Expand Up @@ -157,11 +144,8 @@ - (instancetype)initWithEventId:(NSString *)eventId {
return self;
}

- (void)addCustomProperties:(NSDictionary *)properties error:(NSError **)error {
[super addCustomProperties:properties error:error];
if (*error) {
return;
}
- (void)addCustomProperties:(NSDictionary *)properties {
[super addCustomProperties:properties];
self.properties[kSAEventPresetPropertyLibMethod] = kSALibMethodAuto;
self.lib.method = kSALibMethodAuto;

Expand Down
67 changes: 48 additions & 19 deletions SensorsAnalyticsSDK/Core/Builder/SAIdentifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "SAFileStore.h"
#import "SAValidator.h"
#import "SALog.h"
#import "SensorsAnalyticsSDK+Private.h"

#if TARGET_OS_IOS
#import "SAKeyChainItemWrapper.h"
Expand Down Expand Up @@ -72,7 +73,16 @@ - (instancetype)initWithQueue:(dispatch_queue_t)queue loginIDKey:(NSString *)log
if (self) {
_queue = queue;

if ((![loginIDKey isEqualToString:kSAIdentitiesLoginId] && [self isPresetKey:loginIDKey]) || ![SAValidator isValidKey:loginIDKey]) {
NSError *error = nil;
[SAValidator validKey:loginIDKey error:&error];
if (error) {
SALogError(@"%@",error.localizedDescription);
if (error.code != SAValidatorErrorOverflow) {
loginIDKey = kSAIdentitiesLoginId;
}
}

if (![loginIDKey isEqualToString:kSAIdentitiesLoginId] && [self isPresetKey:loginIDKey]) {
SALogError(@"LoginIDKey [ %@ ] is invalid", loginIDKey);
loginIDKey = kSAIdentitiesLoginId;
}
Expand All @@ -92,16 +102,19 @@ - (instancetype)initWithQueue:(dispatch_queue_t)queue loginIDKey:(NSString *)log
#pragma mark - Public Methods

- (BOOL)identify:(NSString *)anonymousId {
if (![SAValidator isValidString:anonymousId]) {
SALogError(@"%@ anonymousId:%@ is invalid parameter for identify", self, anonymousId);
if (![anonymousId isKindOfClass:[NSString class]]) {
SALogError(@"AnonymousId must be string");
return NO;
}

if ([anonymousId length] > 255) {
SALogError(@"%@ anonymousId:%@ is beyond the maximum length 255", self, anonymousId);
if (anonymousId.length == 0) {
SALogError(@"AnonymousId is empty");
return NO;
}

if ([anonymousId length] > kSAPropertyValueMaxLength) {
SALogWarn(@"AnonymousId: %@'s length is longer than %ld", anonymousId, kSAPropertyValueMaxLength);
}

if ([anonymousId isEqualToString:self.anonymousId]) {
return NO;
}
Expand Down Expand Up @@ -136,13 +149,20 @@ - (void)updateAnonymousId:(NSString *)anonymousId {
}

- (BOOL)isValidLoginId:(NSString *)loginId {
if (![SAValidator isValidString:loginId]) {
SALogError(@"%@ loginId:%@ is invalid parameter for login", self, loginId);
if (![loginId isKindOfClass:[NSString class]]) {
SALogError(@"LoginId must be string");
return NO;
}
if (loginId.length == 0) {
SALogError(@"LoginId is empty");
return NO;
}

if ([loginId length] > kSAPropertyValueMaxLength) {
SALogWarn(@"LoginId: %@'s length is longer than %ld", loginId, kSAPropertyValueMaxLength);
}

if ([loginId length] > 255) {
SALogError(@"%@ loginId:%@ is beyond the maximum length 255", self, loginId);
if ([loginId isEqualToString:self.loginId]) {
return NO;
}

Expand Down Expand Up @@ -376,33 +396,42 @@ - (BOOL)isPresetKey:(NSString *)key {

- (BOOL)isValidIdentity:(NSString *)key value:(NSString *)value {
if (![key isKindOfClass:NSString.class]) {
SALogError(@"Key must be String");
SALogError(@"Key [%@] must be string", key);
return NO;
}
if (key.length <= 0) {
SALogError(@"Key is empty or null");
SALogError(@"Key is empty");
return NO;
}
if (![SAValidator isValidKey:key] || [self isPresetKey:key]) {
NSError *error = nil;
[SAValidator validKey:key error:&error];
if (error) {
SALogError(@"%@",error.localizedDescription);
}
if (error && error.code != SAValidatorErrorOverflow) {
return NO;
}
if ([self isPresetKey:key]) {
SALogError(@"Key [ %@ ] is invalid", key);
return NO;
}
if ([key isEqualToString:self.loginIDKey]) {
SALogError(@"Key [ %@ ] is invalid", key);
return NO;
}
if (![value isKindOfClass:NSString.class]) {
SALogError(@"Value must be String");
if (!value) {
SALogError(@"bind or unbind value should not be nil");
return NO;
}
if (key.length <= 0) {
SALogError(@"Value is empty or null");
if (![value isKindOfClass:[NSString class]]) {
SALogError(@"bind or unbind value should be string");
return NO;
}
if (value.length > 255) {
SALogError(@"Value [ %@ ] is beyond the maximum length 255", value);
if (value.length == 0) {
SALogError(@"bind or unbind value should not be empty");
return NO;
}
[value sensorsdata_propertyValueWithKey:key error:nil];
return YES;
}

Expand Down
17 changes: 2 additions & 15 deletions SensorsAnalyticsSDK/Core/Builder/SASuperProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,7 @@ - (instancetype)init {
}

- (void)registerSuperProperties:(NSDictionary *)propertyDict {
NSError *error = nil;
NSDictionary *validProperty = [SAPropertyValidator validProperties:[propertyDict copy] error:&error];
if (error) {
SALogError(@"%@", error.localizedDescription);
SALogError(@"%@ failed to register super properties.", self);
[SAModuleManager.sharedInstance showDebugModeWarning:error.localizedDescription];
return;
}
NSDictionary *validProperty = [SAPropertyValidator validProperties:[propertyDict copy]];
[self unregisterSameLetterSuperProperties:validProperty];
// 注意这里的顺序,发生冲突时是以 propertyDict 为准,所以它是后加入的
NSMutableDictionary *tmp = [NSMutableDictionary dictionaryWithDictionary:self.superProperties];
Expand Down Expand Up @@ -122,13 +115,7 @@ - (NSDictionary *)acquireDynamicSuperProperties {
return [self.dynamicSuperPropertiesLock readWithBlock:^id _Nonnull{
if (self.dynamicSuperProperties) {
NSDictionary *dynamicProperties = self.dynamicSuperProperties();
NSError *error;
NSDictionary *validProperties = [SAPropertyValidator validProperties:dynamicProperties error:&error];
if (error) {
SALogError(@"%@", error.localizedDescription);
[SAModuleManager.sharedInstance showDebugModeWarning:error.localizedDescription];
return nil;
}
NSDictionary *validProperties = [SAPropertyValidator validProperties:dynamicProperties];
[self unregisterSameLetterSuperProperties:validProperties];
return validProperties;
}
Expand Down
1 change: 1 addition & 0 deletions SensorsAnalyticsSDK/Core/SAConfigOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ @interface SAConfigOptions ()<NSCopying>
@property (nonatomic, assign) BOOL enableDeeplink;
@property (nonatomic, assign) BOOL enableAutoTrack;


@end

@implementation SAConfigOptions
Expand Down
Loading

0 comments on commit 08a3e58

Please sign in to comment.