Skip to content

Commit

Permalink
Fix Session UserID not consistent among tracker instances (close #630)
Browse files Browse the repository at this point in the history
PR: #631
  • Loading branch information
AlexBenny authored Aug 13, 2021
1 parent 2cf806e commit 8210486
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Snowplow/Internal/SPTrackerConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ extern NSString * const kSPApplicationBuild;

// --- Session Context

extern NSString * const kSPInstallationUserId;

extern NSString * const kSPSessionUserId;
extern NSString * const kSPSessionId;
extern NSString * const kSPSessionPreviousId;
Expand Down
2 changes: 2 additions & 0 deletions Snowplow/Internal/SPTrackerConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ @implementation SPTrackerConstants

// --- Session Context

NSString * const kSPInstallationUserId = @"SPInstallationUserId";

NSString * const kSPSessionUserId = @"userId";
NSString * const kSPSessionId = @"sessionId";
NSString * const kSPSessionPreviousId = @"previousSessionId";
Expand Down
31 changes: 22 additions & 9 deletions Snowplow/Internal/Session/SPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ - (instancetype)initWithForegroundTimeout:(NSInteger)foregroundTimeout andBackgr

NSDictionary * storedSessionDict = [self getSessionFromFile];
if (storedSessionDict) {
_userId = [storedSessionDict valueForKey:kSPSessionUserId];
_userId = [storedSessionDict valueForKey:kSPSessionUserId] ?: [SPUtilities getUUIDString];
_currentSessionId = [storedSessionDict valueForKey:kSPSessionId];
_sessionIndex = [[storedSessionDict valueForKey:kSPSessionIndex] intValue];
} else {
Expand All @@ -101,6 +101,16 @@ - (instancetype)initWithForegroundTimeout:(NSInteger)foregroundTimeout andBackgr
_sessionIndex = 0;
}

// Get or Set the Session UserID
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *storedUserId = [userDefaults stringForKey:kSPInstallationUserId];
if (storedUserId) {
_userId = storedUserId;
} else if (_userId) {
[userDefaults setObject:_userId forKey:kSPInstallationUserId];
}

// Start session check
self.lastSessionCheck = [SPUtilities getTimestamp];
[self startChecker];

Expand Down Expand Up @@ -162,16 +172,20 @@ - (void) setBackgroundTimeout:(NSInteger)backgroundTimeout {
}

- (NSDictionary *) getSessionDictWithEventId:(NSString *)eventId {
NSMutableDictionary *result;
if (!_isSessionCheckerEnabled) {
return [_sessionDict copy];
}
@synchronized (self) {
if ([self shouldUpdateSession]) {
[self updateSessionWithEventId:eventId];
result = [_sessionDict mutableCopy];
} else {
@synchronized (self) {
if ([self shouldUpdateSession]) {
[self updateSessionWithEventId:eventId];
}
self.lastSessionCheck = [SPUtilities getTimestamp];
result = [_sessionDict mutableCopy];
}
self.lastSessionCheck = [SPUtilities getTimestamp];
return [_sessionDict copy];
}
[result setObject:_userId forKey:kSPSessionUserId];
return [result copy];
}

- (NSInteger) getForegroundTimeout {
Expand Down Expand Up @@ -275,7 +289,6 @@ - (void)updateSessionWithEventId:(NSString *)eventId {
if (_firstEventId) {
[newSessionDict setObject:_firstEventId forKey:kSPSessionFirstEventId];
}
[newSessionDict setObject:_userId forKey:kSPSessionUserId];
[newSessionDict setObject:_currentSessionId forKey:kSPSessionId];
[newSessionDict setObject:(_previousSessionId != nil ? _previousSessionId : [NSNull null]) forKey:kSPSessionPreviousId];
[newSessionDict setObject:[NSNumber numberWithInt:(int)_sessionIndex] forKey:kSPSessionIndex];
Expand Down

0 comments on commit 8210486

Please sign in to comment.