Skip to content

Commit

Permalink
task(MC-2359): Add normalized event and property names evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-clevertap committed Nov 14, 2024
1 parent 5f26cf6 commit db45c6c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CleverTapSDK/CTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
+ (double)haversineDistance:(CLLocationCoordinate2D)coordinateA coordinateB:(CLLocationCoordinate2D)coordinateB;
+ (NSNumber * _Nullable)numberFromString:(NSString * _Nullable)string;
+ (NSNumber * _Nullable)numberFromString:(NSString * _Nullable)string withLocale:(NSLocale * _Nullable)locale;
+ (NSString * _Nullable)getNormalizedName:(NSString * _Nullable)name;
+ (BOOL)areEqualNormalizedName:(NSString * _Nullable)firstName andName:(NSString * _Nullable)secondName;

@end
28 changes: 28 additions & 0 deletions CleverTapSDK/CTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,32 @@ + (NSNumber * _Nullable)numberFromString:(NSString * _Nullable)string withLocale
return nil;
}

+ (NSString * _Nullable)getNormalizedName:(NSString * _Nullable)name {
if (name) {
NSString *normalizedName = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
normalizedName = [normalizedName lowercaseStringWithLocale:englishLocale];
normalizedName = [normalizedName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
return normalizedName;
}

return nil;
}

+ (BOOL)areEqualNormalizedName:(NSString * _Nullable)firstName
andName:(NSString * _Nullable)secondName {
if (firstName == nil && secondName == nil) {
return YES;
}

if (firstName == nil || secondName == nil) {
return NO;
}

NSString *normalizedFirstName = [CTUtils getNormalizedName:firstName];
NSString *normalizedSecondName = [CTUtils getNormalizedName:secondName];

return [normalizedFirstName isEqualToString:normalizedSecondName];
}

@end
6 changes: 3 additions & 3 deletions CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "CTTriggerValue.h"
#import "CTConstants.h"
#import "CTTriggerEvaluator.h"
#import "CTUtils.h"

@implementation CTTriggersMatcher

Expand All @@ -30,9 +31,8 @@ - (BOOL)matchEventWhenTriggers:(NSArray *)whenTriggers event:(CTEventAdapter *)e
}

- (BOOL)match:(CTTriggerAdapter *)trigger event:(CTEventAdapter *)event {

BOOL eventNameMatch = [[event eventName] isEqualToString:[trigger eventName]];
BOOL profileAttrNameMatch = [event profileAttrName] != nil && [[event profileAttrName] isEqualToString:[trigger profileAttrName]];
BOOL eventNameMatch = [CTUtils areEqualNormalizedName:[event eventName] andName:[trigger eventName]];
BOOL profileAttrNameMatch = [event profileAttrName] != nil && [CTUtils areEqualNormalizedName:[event profileAttrName] andName:[trigger profileAttrName]];
if (!eventNameMatch && !profileAttrNameMatch) {
return NO;
}
Expand Down
19 changes: 19 additions & 0 deletions CleverTapSDKTests/CTUtilsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,23 @@ - (void)test_numberFromStringWithLocale {
XCTAssertNil([CTUtils numberFromString:@"12.3" withLocale:locale]);
}

- (void)testGetNormalizedName {
XCTAssertNil([CTUtils getNormalizedName:nil]);
XCTAssertEqualObjects(@"", [CTUtils getNormalizedName:@""]);
XCTAssertEqualObjects(@"event1", [CTUtils getNormalizedName:@"Event 1"]);
XCTAssertEqualObjects(@"event1", [CTUtils getNormalizedName:@"EVENT 1"]);
XCTAssertEqualObjects(@"event1", [CTUtils getNormalizedName:@"event1"]);
}

- (void)testAreEqualNormalizedNames {
XCTAssertTrue([CTUtils areEqualNormalizedName:nil andName:nil]);
XCTAssertTrue([CTUtils areEqualNormalizedName:@"" andName:@""]);
XCTAssertTrue([CTUtils areEqualNormalizedName:@"Event 1" andName:@"Event1"]);
XCTAssertTrue([CTUtils areEqualNormalizedName:@"Event 1" andName:@"event1"]);
XCTAssertTrue([CTUtils areEqualNormalizedName:@"Event 1" andName:@"EVENT 1"]);
XCTAssertFalse([CTUtils areEqualNormalizedName:@"" andName:nil]);
XCTAssertFalse([CTUtils areEqualNormalizedName:@"Event 1" andName:nil]);
XCTAssertFalse([CTUtils areEqualNormalizedName:@"Event 1" andName:@"Event 2"]);
}

@end
16 changes: 16 additions & 0 deletions CleverTapSDKTests/InApps/CTTriggersMatcherTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ - (void)testMatchEventWithoutProps {
XCTAssertFalse(matchNoProps);
}

- (void)testMatchEventWithNormalizedName {
NSArray *whenTriggers = @[
@{
@"eventName": @"event1",
@"eventProperties": @[
]
}
];

CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init];
BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"Event 1" eventProperties:@{
@"prop1": @"clevertap"
}];
XCTAssertTrue(match);
}

#pragma mark Charged Event

- (void)testMatchChargedEvent {
Expand Down

0 comments on commit db45c6c

Please sign in to comment.