Skip to content

Commit

Permalink
feat: add register dynamic generalProps block
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Apr 17, 2024
1 parent 3ce88fc commit 76e66ea
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Example/GrowingAnalyticsTests/A0GrowingAnalyticsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,25 @@ - (void)test22SetGeneralProps {
XCTAssertEqualObjects(event.eventName, @"eventName");
XCTAssertEqual(event.attributes.count, 0);
}

{
// set dynamic generalProps
[MockEventQueue.sharedQueue cleanQueue];

[[GrowingAutotracker sharedInstance] setGeneralProps:@{@"key" : @"value", @"key2" : @"value2"}];
[[GrowingAutotracker sharedInstance] registerDynamicGeneralPropsBlock:^NSDictionary<NSString *,NSString *> * _Nonnull{
return @{@"key": @"valueChange", @"key3": @(1)};
}];
[[GrowingAutotracker sharedInstance] trackCustomEvent:@"eventName"];
NSArray<GrowingBaseEvent *> *events = [MockEventQueue.sharedQueue eventsFor:GrowingEventTypeCustom];
XCTAssertEqual(events.count, 1);

GrowingCustomEvent *event = (GrowingCustomEvent *)events.lastObject;
XCTAssertEqualObjects(event.eventName, @"eventName");
XCTAssertEqualObjects(event.attributes[@"key"], @"valueChange");
XCTAssertEqualObjects(event.attributes[@"key2"], @"value2");
XCTAssertEqualObjects(event.attributes[@"key3"], @"1");
}
}

@end
Expand Down
4 changes: 4 additions & 0 deletions GrowingAutotracker/GrowingAutotracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ NS_ASSUME_NONNULL_BEGIN
/// @param props 事件通用属性,相同字段的新值将覆盖旧值
- (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props;

/// 设置埋点动态通用属性
/// @param dynamicGeneralPropsBlock 动态通用属性,其优先级大于通用属性
- (void)registerDynamicGeneralPropsBlock:(NSDictionary<NSString *, NSString *> *(^_Nullable)(void))dynamicGeneralPropsBlock;

/// 清除指定字段的埋点通用属性
/// @param keys 通用属性指定字段
- (void)removeGeneralProps:(NSArray<NSString *> *)keys;
Expand Down
4 changes: 4 additions & 0 deletions GrowingTracker/GrowingTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ NS_ASSUME_NONNULL_BEGIN
/// @param props 事件通用属性,相同字段的新值将覆盖旧值
- (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props;

/// 设置埋点动态通用属性
/// @param dynamicGeneralPropsBlock 动态通用属性,其优先级大于通用属性
- (void)registerDynamicGeneralPropsBlock:(NSDictionary<NSString *, NSString *> *(^_Nullable)(void))dynamicGeneralPropsBlock;

/// 清除指定字段的埋点通用属性
/// @param keys 通用属性指定字段
- (void)removeGeneralProps:(NSArray<NSString *> *)keys;
Expand Down
1 change: 1 addition & 0 deletions GrowingTrackerCore/Event/GrowingEventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@

- (NSDictionary<NSString *, NSString *> *_Nonnull)generalProps;
- (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *_Nonnull)props;
- (void)registerDynamicGeneralPropsBlock:(NSDictionary<NSString *, NSString *> *_Nonnull(^_Nullable)(void))dynamicGeneralPropsBlock;
- (void)removeGeneralProps:(NSArray<NSString *> *_Nonnull)keys;
- (void)clearGeneralProps;

Expand Down
4 changes: 4 additions & 0 deletions GrowingTrackerCore/Event/GrowingEventManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ - (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props {
[self.gProps setGeneralProps:props];
}

- (void)registerDynamicGeneralPropsBlock:(NSDictionary<NSString *, NSString *> *(^_Nullable)(void))dynamicGeneralPropsBlock {
[self.gProps registerDynamicGeneralPropsBlock:dynamicGeneralPropsBlock];
}

- (void)removeGeneralProps:(NSArray<NSString *> *)keys {
[self.gProps removeGeneralProps:keys];
}
Expand Down
2 changes: 2 additions & 0 deletions GrowingTrackerCore/Event/GrowingGeneralProps.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props;

- (void)registerDynamicGeneralPropsBlock:(NSDictionary<NSString *, NSString *> *(^_Nullable)(void))dynamicGeneralPropsBlock;

- (void)removeGeneralProps:(NSArray<NSString *> *)keys;

- (void)clearGeneralProps;
Expand Down
32 changes: 31 additions & 1 deletion GrowingTrackerCore/Event/GrowingGeneralProps.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@interface GrowingGeneralProps ()

@property (nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *internalProps;
@property (nonatomic, copy) NSDictionary<NSString *, NSString *> *(^dynamicPropsBlock)(void);

@end

Expand All @@ -31,6 +32,10 @@ - (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props {
[self.internalProps addEntriesFromDictionary:props];
}

- (void)registerDynamicGeneralPropsBlock:(NSDictionary<NSString *, NSString *> *(^_Nullable)(void))dynamicGeneralPropsBlock {
self.dynamicPropsBlock = dynamicGeneralPropsBlock;
}

- (void)removeGeneralProps:(NSArray<NSString *> *)keys {
[self.internalProps removeObjectsForKeys:keys];
}
Expand All @@ -39,10 +44,35 @@ - (void)clearGeneralProps {
[self.internalProps removeAllObjects];
}

- (NSDictionary<NSString *, NSString *> *)validProperties:(NSDictionary *)properties {
if (![properties isKindOfClass:[NSDictionary class]]) {
return nil;
}

NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (NSString *key in properties.allKeys) {
id value = properties[key];
if ([value isKindOfClass:[NSString class]]) {
result[key] = value;
} else if ([value isKindOfClass:[NSNumber class]]) {
result[key] = ((NSNumber *)value).stringValue;
}
}
return [result copy];
}

#pragma mark - Setter && Getter

- (NSDictionary<NSString *, NSString *> *)props {
return self.internalProps.copy;
// dynamic general properties > general properties
NSMutableDictionary *finalProps = self.internalProps.mutableCopy;
if (self.dynamicPropsBlock) {
NSDictionary *dynamicProps = self.dynamicPropsBlock();
if (dynamicProps && [dynamicProps isKindOfClass:[NSDictionary class]]) {
[finalProps addEntriesFromDictionary:dynamicProps];
}
}
return [self validProperties:finalProps];
}

- (NSMutableDictionary<NSString *, NSString *> *)internalProps {
Expand Down
6 changes: 6 additions & 0 deletions GrowingTrackerCore/GrowingRealTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ - (void)setGeneralProps:(NSDictionary<NSString *, NSString *> *)props {
}];
}

- (void)registerDynamicGeneralPropsBlock:(NSDictionary<NSString *, NSString *> *(^_Nullable)(void))dynamicGeneralPropsBlock {
[GrowingDispatchManager dispatchInGrowingThread:^{
[[GrowingEventManager sharedInstance] registerDynamicGeneralPropsBlock:dynamicGeneralPropsBlock];
}];
}

- (void)removeGeneralProps:(NSArray<NSString *> *)keys {
if ([GrowingArgumentChecker isIllegalKeys:keys]) {
return;
Expand Down

0 comments on commit 76e66ea

Please sign in to comment.