diff --git a/CleverTapSDK/CTUtils.h b/CleverTapSDK/CTUtils.h index 26a1b247..741b3d1e 100644 --- a/CleverTapSDK/CTUtils.h +++ b/CleverTapSDK/CTUtils.h @@ -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 diff --git a/CleverTapSDK/CTUtils.m b/CleverTapSDK/CTUtils.m index 14f49047..cdb01e32 100644 --- a/CleverTapSDK/CTUtils.m +++ b/CleverTapSDK/CTUtils.m @@ -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 diff --git a/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m b/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m index b6366b13..7f68301a 100644 --- a/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m +++ b/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m @@ -11,6 +11,7 @@ #import "CTTriggerValue.h" #import "CTConstants.h" #import "CTTriggerEvaluator.h" +#import "CTUtils.h" @implementation CTTriggersMatcher @@ -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; } diff --git a/CleverTapSDKTests/CTUtilsTest.m b/CleverTapSDKTests/CTUtilsTest.m index 16f12e0c..71fe4103 100644 --- a/CleverTapSDKTests/CTUtilsTest.m +++ b/CleverTapSDKTests/CTUtilsTest.m @@ -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 diff --git a/CleverTapSDKTests/InApps/CTTriggersMatcherTest.m b/CleverTapSDKTests/InApps/CTTriggersMatcherTest.m index df26c716..d7d2f5da 100644 --- a/CleverTapSDKTests/InApps/CTTriggersMatcherTest.m +++ b/CleverTapSDKTests/InApps/CTTriggersMatcherTest.m @@ -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 {