Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global state management #905

Merged
merged 8 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Analytics.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
9D8CE5A023EE014E00197D0C /* Analytics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EADEB85B1DECD080005322DA /* Analytics.framework */; };
A31958EF2385AC3A00A47EFA /* SerializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A31958EE2385AC3A00A47EFA /* SerializationTests.m */; };
A31CAFC224635F8400443BA4 /* AutoScreenReportingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AF0E8457CCFC077382BF449 /* AutoScreenReportingTest.swift */; };
A34CC6DD249AAEBB00C5E368 /* SEGState.m in Sources */ = {isa = PBXBuildFile; fileRef = A34CC6DB249AAEBA00C5E368 /* SEGState.m */; };
A34CC6DE249AAEBB00C5E368 /* SEGState.h in Headers */ = {isa = PBXBuildFile; fileRef = A34CC6DC249AAEBB00C5E368 /* SEGState.h */; settings = {ATTRIBUTES = (Private, ); }; };
A352176023AD5825005B07F6 /* SEGMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = A352175F23AD5825005B07F6 /* SEGMacros.h */; settings = {ATTRIBUTES = (Private, ); }; };
E4ACAD51DB827114D8626C74 /* Pods_AnalyticsTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5238C0454BF56A36A536C5C1 /* Pods_AnalyticsTests.framework */; };
EA88A5981DED7608009FB66A /* SEGSerializableValue.h in Headers */ = {isa = PBXBuildFile; fileRef = EA88A5971DED7608009FB66A /* SEGSerializableValue.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -128,6 +130,8 @@
6EEC1C702017EA370089C478 /* EndToEndTests.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = EndToEndTests.swift; sourceTree = "<group>"; tabWidth = 4; };
9D8CE5A723EE014E00197D0C /* AnalyticsTestsTVOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AnalyticsTestsTVOS.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A31958EE2385AC3A00A47EFA /* SerializationTests.m */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = SerializationTests.m; sourceTree = "<group>"; tabWidth = 4; };
A34CC6DB249AAEBA00C5E368 /* SEGState.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SEGState.m; sourceTree = "<group>"; };
A34CC6DC249AAEBB00C5E368 /* SEGState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SEGState.h; sourceTree = "<group>"; };
A352175F23AD5825005B07F6 /* SEGMacros.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SEGMacros.h; sourceTree = "<group>"; };
B0D1E9E4405FD8386DCCD150 /* Pods-AnalyticsTestsTVOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnalyticsTestsTVOS.release.xcconfig"; path = "Target Support Files/Pods-AnalyticsTestsTVOS/Pods-AnalyticsTestsTVOS.release.xcconfig"; sourceTree = "<group>"; };
EA88A5971DED7608009FB66A /* SEGSerializableValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SEGSerializableValue.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -361,6 +365,8 @@
EADEB88C1DECD12B005322DA /* Internal */ = {
isa = PBXGroup;
children = (
A34CC6DC249AAEBB00C5E368 /* SEGState.h */,
A34CC6DB249AAEBA00C5E368 /* SEGState.m */,
EADEB88D1DECD12B005322DA /* NSData+SEGGZIP.h */,
EADEB88E1DECD12B005322DA /* NSData+SEGGZIP.m */,
EADEB8781DECD12B005322DA /* SEGAES256Crypto.h */,
Expand Down Expand Up @@ -411,6 +417,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
A34CC6DE249AAEBB00C5E368 /* SEGState.h in Headers */,
EADEB8DC1DECD12B005322DA /* SEGAnalytics.h in Headers */,
EADEB8B81DECD12B005322DA /* SEGIntegrationFactory.h in Headers */,
EADEB8CB1DECD12B005322DA /* SEGSegmentIntegration.h in Headers */,
Expand Down Expand Up @@ -718,6 +725,7 @@
EADEB8D51DECD12B005322DA /* SEGUtils.m in Sources */,
EAA5427E1EB42B8C00945DA7 /* SEGReachability.m in Sources */,
EADEB8B41DECD12B005322DA /* SEGGroupPayload.m in Sources */,
A34CC6DD249AAEBB00C5E368 /* SEGState.m in Sources */,
EADEB8D71DECD12B005322DA /* UIViewController+SEGScreen.m in Sources */,
EADEB8D11DECD12B005322DA /* SEGStoreKitTracker.m in Sources */,
);
Expand Down
1 change: 0 additions & 1 deletion Analytics/Classes/SEGAnalytics.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ NS_SWIFT_NAME(Analytics)
/** Returns the configuration used to create the analytics client. */
- (SEGAnalyticsConfiguration *)configuration;


@end

NS_ASSUME_NONNULL_END
29 changes: 26 additions & 3 deletions Analytics/Classes/SEGAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "SEGMiddleware.h"
#import "SEGContext.h"
#import "SEGIntegrationsManager.h"
#import "SEGState.h"
#import "SEGUtils.h"

static SEGAnalytics *__sharedInstance = nil;
Expand Down Expand Up @@ -86,6 +87,9 @@ - (instancetype)initWithConfiguration:(SEGAnalyticsConfiguration *)configuration
}
}
#endif

[SEGState sharedInstance].configuration = configuration;
[[SEGState sharedInstance].context updateStaticContext];
}
return self;
}
Expand Down Expand Up @@ -176,6 +180,8 @@ - (void)_applicationWillEnterForeground
@"version" : currentVersion ?: @"",
@"build" : currentBuild ?: @"",
}];

[[SEGState sharedInstance].context updateStaticContext];
}

- (void)_applicationDidEnterBackground
Expand Down Expand Up @@ -221,6 +227,7 @@ - (void)identify:(NSString *)userId traits:(NSDictionary *)traits options:(NSDic
newTraits[@"anonymousId"] = anonId;
if (userId != nil) {
newTraits[@"userId"] = userId;
[SEGState sharedInstance].userInfo.userId = userId;
}

[self run:SEGEventTypeIdentify payload:
Expand Down Expand Up @@ -343,6 +350,7 @@ - (void)registeredForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
NSParameterAssert(deviceToken != nil);
SEGRemoteNotificationPayload *payload = [[SEGRemoteNotificationPayload alloc] init];
payload.deviceToken = deviceToken;
[SEGState sharedInstance].context.deviceToken = deviceTokenToString(deviceToken);
[self run:SEGEventTypeRegisteredForRemoteNotifications payload:payload];
}

Expand All @@ -365,9 +373,14 @@ - (void)continueUserActivity:(NSUserActivity *)activity
}

if ([activity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSString *urlString = activity.webpageURL.absoluteString;
[SEGState sharedInstance].context.referrer = @{
@"url" : urlString,
};

NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithCapacity:activity.userInfo.count + 2];
[properties addEntriesFromDictionary:activity.userInfo];
properties[@"url"] = activity.webpageURL.absoluteString;
properties[@"url"] = urlString;
properties[@"title"] = activity.title ?: @"";
properties = [SEGUtils traverseJSON:properties
andReplaceWithFilters:self.configuration.payloadFilters];
Expand All @@ -386,10 +399,15 @@ - (void)openURL:(NSURL *)url options:(NSDictionary *)options
if (!self.configuration.trackDeepLinks) {
return;
}

NSString *urlString = url.absoluteString;
[SEGState sharedInstance].context.referrer = @{
@"url" : urlString,
};

NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithCapacity:options.count + 2];
[properties addEntriesFromDictionary:options];
properties[@"url"] = url.absoluteString;
properties[@"url"] = urlString;
properties = [SEGUtils traverseJSON:properties
andReplaceWithFilters:self.configuration.payloadFilters];
[self track:@"Deep Link Opened" properties:[properties copy]];
Expand Down Expand Up @@ -417,7 +435,7 @@ - (void)disable

- (NSString *)getAnonymousId
{
return [self.integrationsManager getAnonymousId];
return [SEGState sharedInstance].userInfo.anonymousId;
}

- (NSDictionary *)bundledIntegrations
Expand Down Expand Up @@ -458,10 +476,15 @@ - (void)run:(SEGEventType)eventType payload:(SEGPayload *)payload
} else {
payload.timestamp = iso8601FormattedString([NSDate date]);
}

SEGContext *context = [[[SEGContext alloc] initWithAnalytics:self] modify:^(id<SEGMutableContext> _Nonnull ctx) {
ctx.eventType = eventType;
ctx.payload = payload;
ctx.payload.messageId = GenerateUUIDString();
ctx.anonymousId = [SEGState sharedInstance].userInfo.anonymousId;
ctx.userId = [SEGState sharedInstance].userInfo.userId;
}];

// Could probably do more things with callback later, but we don't use it yet.
[self.runner run:context callback:nil];
}
Expand Down
1 change: 1 addition & 0 deletions Analytics/Classes/SEGPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ NS_SWIFT_NAME(Payload)
@property (nonatomic, readonly) JSON_DICT context;
@property (nonatomic, readonly) JSON_DICT integrations;
@property (nonatomic, strong) NSString *timestamp;
@property (nonatomic, strong) NSString *messageId;

- (instancetype)initWithContext:(JSON_DICT)context integrations:(JSON_DICT)integrations;

Expand Down
11 changes: 9 additions & 2 deletions Analytics/Classes/SEGPayload.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#import "SEGPayload.h"

#import "SEGState.h"

@implementation SEGPayload

- (instancetype)initWithContext:(NSDictionary *)context integrations:(NSDictionary *)integrations
{
if (self = [super init]) {
_context = [context copy];
// combine existing state with user supplied context.
NSDictionary *internalContext = [SEGState sharedInstance].context.payload;

NSMutableDictionary *combinedContext = [[NSMutableDictionary alloc] init];
[combinedContext addEntriesFromDictionary:internalContext];
[combinedContext addEntriesFromDictionary:context];

_context = [combinedContext copy];
_integrations = [integrations copy];
}
return self;
Expand Down
Loading