Skip to content

Commit

Permalink
feat: add set dynamic generalProps generator (#308)
Browse files Browse the repository at this point in the history
* feat: add register dynamic generalProps block

* style: code format

* refactor: all event classes inherit from GrowingBaseEvent

* fix: delete conversionVariable and visitorAttributes Event

* style: code format

* refactor: static functions about generalProps

* feat: all event types take with generalProps

* fix: attributes type change to [String: Any]

* fix: buildDynamicGeneralProps logic

* style: code format

* fix: rename registerDynamicGeneralPropsBlock when coding with swift language

* style: code format

* ci: update ci

* fix: rename registerDynamicGeneralPropsBlock to setDynamicGeneralPropsGenerator

* ci: update ci

* ci: update ci

* fix: event attributes set to nil when attributes count == 0

* style: code format

* fix: generalProps in multi-threads

* style: code format

* style: delete unused codes

* ci: update ci

---------

Co-authored-by: GIOSDK <sdk-integration@growingio.com>
  • Loading branch information
YoloMao and GIOSDK committed Apr 30, 2024
1 parent a52fefa commit bec7290
Show file tree
Hide file tree
Showing 43 changed files with 551 additions and 569 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ jobs:
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Install sonar-scanner and build-wrapper
uses: sonarsource/sonarcloud-github-c-cpp@v2
- name: Install sonar-scanner
uses: yolomao/sonarcloud-github-c-cpp@main

- name: Cache cocoapods
uses: actions/cache@v4
Expand Down
288 changes: 245 additions & 43 deletions Example/GrowingAnalyticsTests/A0GrowingAnalyticsTest.m

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions Example/GrowingAnalyticsTests/Helper/ManualTrackHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

+ (BOOL)loginUserAttributesEventCheck:(NSDictionary *)event;

+ (BOOL)conversionVariablesEventCheck:(NSDictionary *)event;

+ (BOOL)visitorAttributesEventCheck:(NSDictionary *)event;

+ (BOOL)appCloseEventCheck:(NSDictionary *)event;

+ (BOOL)pageEventCheck:(NSDictionary *)event;
Expand Down
16 changes: 0 additions & 16 deletions Example/GrowingAnalyticsTests/Helper/ManualTrackHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ + (BOOL)loginUserAttributesEventCheck:(NSDictionary *)event {
return [self protocolCheck:event protocol:protocol] && [self emptyPropertyCheck:event];
}

+ (BOOL)conversionVariablesEventCheck:(NSDictionary *)event {
if (event.count == 0) {
return NO;
}
NSArray *protocol = [self.context arrayByAddingObjectsFromArray:@[@"attributes"]];
return [self protocolCheck:event protocol:protocol] && [self emptyPropertyCheck:event];
}

+ (BOOL)visitorAttributesEventCheck:(NSDictionary *)event {
if (event.count == 0) {
return NO;
}
NSArray *protocol = [self.context arrayByAddingObjectsFromArray:@[@"attributes"]];
return [self protocolCheck:event protocol:protocol] && [self emptyPropertyCheck:event];
}

+ (BOOL)appCloseEventCheck:(NSDictionary *)event {
if (event.count == 0) {
return NO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#import <KIF/KIF.h>
#import <WebKit/WebKit.h>
#import "GrowingAutotracker.h"
#import "GrowingTrackerCore/Event/GrowingConversionVariableEvent.h"
#import "GrowingTrackerCore/Event/GrowingLoginUserAttributesEvent.h"
#import "GrowingTrackerCore/Event/GrowingTrackEventType.h"
#import "GrowingTrackerCore/Event/Tools/GrowingPersistenceDataProvider.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageEvent.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingViewElementEvent.h"
#import "GrowingTrackerCore/Event/GrowingAppCloseEvent.h"
#import "GrowingTrackerCore/Event/GrowingConversionVariableEvent.h"
#import "GrowingTrackerCore/Event/GrowingCustomEvent.h"
#import "GrowingTrackerCore/Event/GrowingLoginUserAttributesEvent.h"
#import "GrowingTrackerCore/Event/GrowingVisitEvent.h"
#import "GrowingTrackerCore/Event/GrowingVisitorAttributesEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridCustomEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridEventType.h"
#import "Modules/Hybrid/Events/GrowingHybridPageEvent.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
#import "GrowingTrackerCore/Event/Autotrack/GrowingAutotrackEventType.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageEvent.h"
#import "GrowingTrackerCore/Event/GrowingAppCloseEvent.h"
#import "GrowingTrackerCore/Event/GrowingConversionVariableEvent.h"
#import "GrowingTrackerCore/Event/GrowingCustomEvent.h"
#import "GrowingTrackerCore/Event/GrowingEventManager.h"
#import "GrowingTrackerCore/Event/GrowingLoginUserAttributesEvent.h"
#import "GrowingTrackerCore/Event/GrowingVisitEvent.h"
#import "GrowingTrackerCore/Event/GrowingVisitorAttributesEvent.h"
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"
#import "GrowingTrackerCore/Manager/GrowingSession.h"
#import "GrowingTrackerCore/Utils/GrowingDeviceInfo.h"
Expand Down Expand Up @@ -150,42 +148,6 @@ - (void)testGrowingLoginUserAttributesEvent {
XCTAssertTrue([ManualTrackHelper contextOptionalPropertyCheck:dic]);
}

- (void)testGrowingConversionVariableEvent {
GrowingBaseBuilder *builder = GrowingConversionVariableEvent.builder.setAttributes(@{@"key": @"value"});
[GrowingEventManager.sharedInstance postEventBuilder:builder];

NSArray<GrowingBaseEvent *> *events = [MockEventQueue.sharedQueue eventsFor:GrowingEventTypeConversionVariables];
XCTAssertEqual(events.count, 1);

GrowingConversionVariableEvent *event = (GrowingConversionVariableEvent *)events.firstObject;
XCTAssertEqualObjects(event.eventType, GrowingEventTypeConversionVariables);
XCTAssertEqualObjects(event.attributes[@"key"], @"value");

NSDictionary *dic = event.toDictionary;
XCTAssertEqualObjects(dic[@"eventType"], GrowingEventTypeConversionVariables);
XCTAssertEqualObjects(dic[@"attributes"][@"key"], @"value");
XCTAssertTrue([ManualTrackHelper conversionVariablesEventCheck:dic]);
XCTAssertTrue([ManualTrackHelper contextOptionalPropertyCheck:dic]);
}

- (void)testGrowingVisitorAttributesEvent {
GrowingBaseBuilder *builder = GrowingVisitorAttributesEvent.builder.setAttributes(@{@"key": @"value"});
[GrowingEventManager.sharedInstance postEventBuilder:builder];

NSArray<GrowingBaseEvent *> *events = [MockEventQueue.sharedQueue eventsFor:GrowingEventTypeVisitorAttributes];
XCTAssertEqual(events.count, 1);

GrowingVisitorAttributesEvent *event = (GrowingVisitorAttributesEvent *)events.firstObject;
XCTAssertEqualObjects(event.eventType, GrowingEventTypeVisitorAttributes);
XCTAssertEqualObjects(event.attributes[@"key"], @"value");

NSDictionary *dic = event.toDictionary;
XCTAssertEqualObjects(dic[@"eventType"], GrowingEventTypeVisitorAttributes);
XCTAssertEqualObjects(dic[@"attributes"][@"key"], @"value");
XCTAssertTrue([ManualTrackHelper visitorAttributesEventCheck:dic]);
XCTAssertTrue([ManualTrackHelper contextOptionalPropertyCheck:dic]);
}

- (void)testGrowingAppCloseEvent {
GrowingBaseBuilder *builder = GrowingAppCloseEvent.builder;
[GrowingEventManager.sharedInstance postEventBuilder:builder];
Expand Down
36 changes: 29 additions & 7 deletions GrowingAutotracker/GrowingAutotracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ NS_ASSUME_NONNULL_BEGIN

/// 以登录用户的身份定义用户属性变量,用于用户信息相关分析。
/// @param attributes 用户属性信息
- (void)setLoginUserAttributes:(NSDictionary<NSString *, NSString *> *)attributes;
- (void)setLoginUserAttributes:(NSDictionary<NSString *, id> *)attributes;

/// 同步获取设备id,又称为匿名用户id,SDK 自动生成用来定义唯一设备。
- (NSString *)getDeviceId;
Expand All @@ -93,7 +93,7 @@ NS_ASSUME_NONNULL_BEGIN
/// 发送一个自定义事件
/// @param eventName 自定义事件名称
/// @param attributes 事件发生时所伴随的维度信息
- (void)trackCustomEvent:(NSString *)eventName withAttributes:(NSDictionary<NSString *, NSString *> *)attributes;
- (void)trackCustomEvent:(NSString *)eventName withAttributes:(NSDictionary<NSString *, id> *)attributes;

/// 初始化事件计时器
/// @param eventName 自定义事件名称
Expand All @@ -115,7 +115,7 @@ NS_ASSUME_NONNULL_BEGIN
/// 停止事件计时器,并发送一个自定义事件
/// @param timerId 计时器唯一标识
/// @param attributes 事件发生时所伴随的维度信息
- (void)trackTimerEnd:(NSString *)timerId withAttributes:(NSDictionary<NSString *, NSString *> *)attributes;
- (void)trackTimerEnd:(NSString *)timerId withAttributes:(NSDictionary<NSString *, id> *)attributes;

/// 删除事件计时器
/// @param timerId 计时器唯一标识
Expand All @@ -126,14 +126,19 @@ NS_ASSUME_NONNULL_BEGIN

/// 设置埋点通用属性
/// @param props 事件通用属性,相同字段的新值将覆盖旧值
- (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props;
+ (void)setGeneralProps:(NSDictionary<NSString *, id> *)props;

/// 清除指定字段的埋点通用属性
/// @param keys 通用属性指定字段
- (void)removeGeneralProps:(NSArray<NSString *> *)keys;
+ (void)removeGeneralProps:(NSArray<NSString *> *)keys;

/// 清除所有埋点通用属性
- (void)clearGeneralProps;
+ (void)clearGeneralProps;

/// 设置埋点动态通用属性
/// @param generator 动态通用属性,其优先级大于通用属性
+ (void)setDynamicGeneralPropsGenerator:(NSDictionary<NSString *, id> * (^_Nullable)(void))generator
NS_SWIFT_NAME(setDynamicGeneralProps(_:));

///-------------------------------
#pragma mark Autotrack Event
Expand All @@ -151,7 +156,7 @@ NS_ASSUME_NONNULL_BEGIN
/// @param attributes 事件发生时所伴随的维度信息
- (void)autotrackPage:(UIViewController *)controller
alias:(NSString *)alias
attributes:(NSDictionary<NSString *, NSString *> *)attributes
attributes:(NSDictionary<NSString *, id> *)attributes
NS_EXTENSION_UNAVAILABLE("AutotrackPage is not supported for iOS extensions.");

///-------------------------------
Expand All @@ -160,6 +165,23 @@ NS_ASSUME_NONNULL_BEGIN

- (instancetype)init NS_UNAVAILABLE;

///-------------------------------
#pragma mark Deprecated
///-------------------------------

/// 设置埋点通用属性
/// @param props 事件通用属性,相同字段的新值将覆盖旧值
- (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props
DEPRECATED_MSG_ATTRIBUTE("Use +[GrowingAutotracker setGeneralProps:] instead.");

/// 清除指定字段的埋点通用属性
/// @param keys 通用属性指定字段
- (void)removeGeneralProps:(NSArray<NSString *> *)keys
DEPRECATED_MSG_ATTRIBUTE("Use +[GrowingAutotracker removeGeneralProps:] instead.");

/// 清除所有埋点通用属性
- (void)clearGeneralProps DEPRECATED_MSG_ATTRIBUTE("Use +[GrowingAutotracker clearGeneralProps] instead.");

@end

// 该属性setter方法均使用 objc_setAssociatedObject实现
Expand Down
19 changes: 18 additions & 1 deletion GrowingAutotracker/GrowingAutotracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#import "GrowingAutotracker/GrowingAutotracker.h"
#import "GrowingAutotrackerCore/GrowingRealAutotracker.h"
#import "GrowingTrackerCore/Event/GrowingGeneralProps.h"
#import "GrowingTrackerCore/Manager/GrowingSession.h"

static GrowingAutotracker *sharedInstance = nil;
Expand All @@ -41,7 +42,7 @@ + (void)startWithConfiguration:(GrowingTrackConfiguration *)configuration launch
}

if (!configuration.accountId.length) {
@throw [NSException exceptionWithName:@"初始化异常" reason:@"AccountId不能为空" userInfo:nil];
@throw [NSException exceptionWithName:@"初始化异常" reason:@"accountId不能为空" userInfo:nil];
}

if (!configuration.dataSourceId.length) {
Expand Down Expand Up @@ -71,6 +72,22 @@ + (instancetype)sharedInstance {
return sharedInstance;
}

+ (void)setGeneralProps:(NSDictionary<NSString *, id> *)props {
[[GrowingGeneralProps sharedInstance] setGeneralProps:props];
}

+ (void)removeGeneralProps:(NSArray<NSString *> *)keys {
[[GrowingGeneralProps sharedInstance] removeGeneralProps:keys];
}

+ (void)clearGeneralProps {
[[GrowingGeneralProps sharedInstance] clearGeneralProps];
}

+ (void)setDynamicGeneralPropsGenerator:(NSDictionary<NSString *, id> * (^_Nullable)(void))generator {
[[GrowingGeneralProps sharedInstance] setDynamicGeneralPropsGenerator:generator];
}

@end

#pragma clang diagnostic pop
34 changes: 28 additions & 6 deletions GrowingTracker/GrowingTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ NS_ASSUME_NONNULL_BEGIN

/// 以登录用户的身份定义用户属性变量,用于用户信息相关分析。
/// @param attributes 用户属性信息
- (void)setLoginUserAttributes:(NSDictionary<NSString *, NSString *> *)attributes;
- (void)setLoginUserAttributes:(NSDictionary<NSString *, id> *)attributes;

/// 同步获取设备id,又称为匿名用户id,SDK 自动生成用来定义唯一设备。
- (NSString *)getDeviceId;
Expand All @@ -86,7 +86,7 @@ NS_ASSUME_NONNULL_BEGIN
/// 发送一个自定义事件
/// @param eventName 自定义事件名称
/// @param attributes 事件发生时所伴随的维度信息
- (void)trackCustomEvent:(NSString *)eventName withAttributes:(NSDictionary<NSString *, NSString *> *)attributes;
- (void)trackCustomEvent:(NSString *)eventName withAttributes:(NSDictionary<NSString *, id> *)attributes;

/// 初始化事件计时器
/// @param eventName 自定义事件名称
Expand All @@ -108,7 +108,7 @@ NS_ASSUME_NONNULL_BEGIN
/// 停止事件计时器,并发送一个自定义事件
/// @param timerId 计时器唯一标识
/// @param attributes 事件发生时所伴随的维度信息
- (void)trackTimerEnd:(NSString *)timerId withAttributes:(NSDictionary<NSString *, NSString *> *)attributes;
- (void)trackTimerEnd:(NSString *)timerId withAttributes:(NSDictionary<NSString *, id> *)attributes;

/// 删除事件计时器
/// @param timerId 计时器唯一标识
Expand All @@ -119,21 +119,43 @@ NS_ASSUME_NONNULL_BEGIN

/// 设置埋点通用属性
/// @param props 事件通用属性,相同字段的新值将覆盖旧值
- (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props;
+ (void)setGeneralProps:(NSDictionary<NSString *, id> *)props;

/// 清除指定字段的埋点通用属性
/// @param keys 通用属性指定字段
- (void)removeGeneralProps:(NSArray<NSString *> *)keys;
+ (void)removeGeneralProps:(NSArray<NSString *> *)keys;

/// 清除所有埋点通用属性
- (void)clearGeneralProps;
+ (void)clearGeneralProps;

/// 设置埋点动态通用属性
/// @param generator 动态通用属性,其优先级大于通用属性
+ (void)setDynamicGeneralPropsGenerator:(NSDictionary<NSString *, id> * (^_Nullable)(void))generator
NS_SWIFT_NAME(setDynamicGeneralProps(_:));

///-------------------------------
#pragma mark Unavailable
///-------------------------------

- (instancetype)init NS_UNAVAILABLE;

///-------------------------------
#pragma mark Deprecated
///-------------------------------

/// 设置埋点通用属性
/// @param props 事件通用属性,相同字段的新值将覆盖旧值
- (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props
DEPRECATED_MSG_ATTRIBUTE("Use +[GrowingTracker setGeneralProps:] instead.");

/// 清除指定字段的埋点通用属性
/// @param keys 通用属性指定字段
- (void)removeGeneralProps:(NSArray<NSString *> *)keys
DEPRECATED_MSG_ATTRIBUTE("Use +[GrowingTracker removeGeneralProps:] instead.");

/// 清除所有埋点通用属性
- (void)clearGeneralProps DEPRECATED_MSG_ATTRIBUTE("Use +[GrowingTracker clearGeneralProps] instead.");

@end

NS_ASSUME_NONNULL_END
21 changes: 18 additions & 3 deletions GrowingTracker/GrowingTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
// limitations under the License.

#import "GrowingTracker/GrowingTracker.h"
#import "GrowingTrackerCore/Event/GrowingGeneralProps.h"
#import "GrowingTrackerCore/GrowingRealTracker.h"
#import "GrowingTrackerCore/Manager/GrowingSession.h"
#import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogMacros.h"
#import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h"

static GrowingTracker *sharedInstance = nil;

Expand All @@ -43,7 +42,7 @@ + (void)startWithConfiguration:(GrowingTrackConfiguration *)configuration launch
}

if (!configuration.accountId.length) {
@throw [NSException exceptionWithName:@"初始化异常" reason:@"AccountId不能为空" userInfo:nil];
@throw [NSException exceptionWithName:@"初始化异常" reason:@"accountId不能为空" userInfo:nil];
}

if (!configuration.dataSourceId.length) {
Expand Down Expand Up @@ -73,6 +72,22 @@ + (instancetype)sharedInstance {
return sharedInstance;
}

+ (void)setGeneralProps:(NSDictionary<NSString *, id> *)props {
[[GrowingGeneralProps sharedInstance] setGeneralProps:props];
}

+ (void)removeGeneralProps:(NSArray<NSString *> *)keys {
[[GrowingGeneralProps sharedInstance] removeGeneralProps:keys];
}

+ (void)clearGeneralProps {
[[GrowingGeneralProps sharedInstance] clearGeneralProps];
}

+ (void)setDynamicGeneralPropsGenerator:(NSDictionary<NSString *, id> * (^_Nullable)(void))generator {
[[GrowingGeneralProps sharedInstance] setDynamicGeneralPropsGenerator:generator];
}

@end

#pragma clang diagnostic pop
6 changes: 3 additions & 3 deletions GrowingTrackerCore/Event/Autotrack/GrowingPageEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "GrowingBaseEvent.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingAutotrackEventType.h"
#import "GrowingTrackerCore/Event/GrowingBaseAttributesEvent.h"

NS_ASSUME_NONNULL_BEGIN

@class GrowingPageBuilder;

@interface GrowingPageEvent : GrowingBaseAttributesEvent
@interface GrowingPageEvent : GrowingBaseEvent

@property (nonatomic, copy, readonly) NSString *path;
@property (nonatomic, copy, readonly) NSString *orientation;
Expand All @@ -35,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN

@end

@interface GrowingPageBuilder : GrowingBaseAttributesBuilder
@interface GrowingPageBuilder : GrowingBaseBuilder

@property (nonatomic, copy, readonly) NSString *path;
@property (nonatomic, copy, readonly) NSString *orientation;
Expand Down

0 comments on commit bec7290

Please sign in to comment.