Skip to content

Commit

Permalink
NR-364665: setUserId tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
cdillard-NewRelic committed Feb 5, 2025
1 parent 0a30ba8 commit 26bbdce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
13 changes: 11 additions & 2 deletions Agent/Analytics/NRMASAM.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ - (BOOL) setAttribute:(NSString*)name value:(id)value {

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

if (name == kNRMA_Attrib_userId && !value ) {
// We allow userId to be nil, so skip value validation in that case to prevent errant log.
BOOL validValue = NO;
if ([name isEqualToString: kNRMA_Attrib_userId] && !value ) {
validValue = YES;
}
else {
validValue = [attributeValidator valueValidator:value];
}


if ([name isEqualToString: kNRMA_Attrib_userId] && !value ) {
NRLOG_AGENT_VERBOSE(@"Successfully set userId to nil");
return YES;
}
Expand Down
19 changes: 10 additions & 9 deletions Agent/Public/NewRelic.m
Original file line number Diff line number Diff line change
Expand Up @@ -631,21 +631,22 @@ + (BOOL) incrementAttribute:(NSString*)name

+ (BOOL) setUserId:(NSString* _Nullable)userId {
NSString *previousUserId = [[NewRelicAgentInternal sharedInstance] getUserId];

BOOL newSession = false;
// If the client passes a new userId that is non NULL.
if (userId != NULL) {
// 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.

// If userId has been set before and is different from the current userId, then we need to start a new session.
if ([previousUserId isEqualToString: userId] == NO) {
// end session and harvest.
newSession = true;
}
// If the client passes a new NULL userId.
else {
if (previousUserId != NULL) {
// end session and harvest.
newSession = true;
}
// Do nothing if passed userId is null and saved userId (for this app session (since app launch)) is null.
if (userId == nil) {
// end session and harvest.
newSession = true;
}

NRLOG_AGENT_VERBOSE(@"setUserId: %@ and previousUserId: %@ and will start newSession=%d", userId, previousUserId, newSession);

// Update in memory userId.
[NewRelicAgentInternal sharedInstance].userId = userId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ - (void) testSetUserId {

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

XCTAssertFalse([analytics setUserId:nil], @"bad input produced a incorrect result");

// Setting userId to nil is allowed.
XCTAssertTrue([analytics setUserId:nil], @"bad input produced a incorrect result");

XCTAssertFalse([analytics setUserId:@""], @"bad input produced a incorrect result");
}
@end

0 comments on commit 26bbdce

Please sign in to comment.