Skip to content

Commit 26bbdce

Browse files
NR-364665: setUserId tweaks
1 parent 0a30ba8 commit 26bbdce

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

Agent/Analytics/NRMASAM.mm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,18 @@ - (BOOL) setAttribute:(NSString*)name value:(id)value {
8989

9090
- (BOOL) checkAttribute:(NSString*)name value:(id)value {
9191
BOOL validAttribute = [attributeValidator nameValidator:name];
92-
BOOL validValue = [attributeValidator valueValidator:value];
9392

94-
if (name == kNRMA_Attrib_userId && !value ) {
93+
// We allow userId to be nil, so skip value validation in that case to prevent errant log.
94+
BOOL validValue = NO;
95+
if ([name isEqualToString: kNRMA_Attrib_userId] && !value ) {
96+
validValue = YES;
97+
}
98+
else {
99+
validValue = [attributeValidator valueValidator:value];
100+
}
101+
102+
103+
if ([name isEqualToString: kNRMA_Attrib_userId] && !value ) {
95104
NRLOG_AGENT_VERBOSE(@"Successfully set userId to nil");
96105
return YES;
97106
}

Agent/Public/NewRelic.m

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -631,21 +631,22 @@ + (BOOL) incrementAttribute:(NSString*)name
631631

632632
+ (BOOL) setUserId:(NSString* _Nullable)userId {
633633
NSString *previousUserId = [[NewRelicAgentInternal sharedInstance] getUserId];
634+
634635
BOOL newSession = false;
635-
// If the client passes a new userId that is non NULL.
636-
if (userId != NULL) {
637-
// A new userId has been set where the previously set one (during this app session (since app launch)) was not NULL or the previous set one was NULL, we start a new session.
636+
637+
// If userId has been set before and is different from the current userId, then we need to start a new session.
638+
if ([previousUserId isEqualToString: userId] == NO) {
639+
// end session and harvest.
638640
newSession = true;
639641
}
640-
// If the client passes a new NULL userId.
641-
else {
642-
if (previousUserId != NULL) {
643-
// end session and harvest.
644-
newSession = true;
645-
}
646642
// Do nothing if passed userId is null and saved userId (for this app session (since app launch)) is null.
643+
if (userId == nil) {
644+
// end session and harvest.
645+
newSession = true;
647646
}
648647

648+
NRLOG_AGENT_VERBOSE(@"setUserId: %@ and previousUserId: %@ and will start newSession=%d", userId, previousUserId, newSession);
649+
649650
// Update in memory userId.
650651
[NewRelicAgentInternal sharedInstance].userId = userId;
651652

Tests/Unit-Tests/NewRelicAgentTests/API-Tests/NRMAAPIHelperTests.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ - (void) testSetUserId {
3434

3535
XCTAssert([analytics setUserId:@"AUniqueId1"], @"Good input produced incorrect result");
3636

37-
XCTAssertFalse([analytics setUserId:nil], @"bad input produced a incorrect result");
38-
37+
// Setting userId to nil is allowed.
38+
XCTAssertTrue([analytics setUserId:nil], @"bad input produced a incorrect result");
39+
3940
XCTAssertFalse([analytics setUserId:@""], @"bad input produced a incorrect result");
4041
}
4142
@end

0 commit comments

Comments
 (0)