From 8bb0408b0d137dea1924bfd048d4e83b44b639f3 Mon Sep 17 00:00:00 2001 From: Mario Radonic Date: Tue, 3 Oct 2023 11:57:25 +0200 Subject: [PATCH 1/3] Add loadFeatureFlagsOnStart config. --- PostHog/Classes/PHGPostHog.m | 4 +++- PostHog/Classes/PHGPostHogConfiguration.h | 7 +++++++ PostHog/Classes/PHGPostHogConfiguration.m | 1 + PostHogTests/PostHogTests.swift | 22 ++++++++++++++++++---- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/PostHog/Classes/PHGPostHog.m b/PostHog/Classes/PHGPostHog.m index 3a380d075..f4c49cbc9 100644 --- a/PostHog/Classes/PHGPostHog.m +++ b/PostHog/Classes/PHGPostHog.m @@ -86,7 +86,9 @@ - (instancetype)initWithConfiguration:(PHGPostHogConfiguration *)configuration } #endif - [self reloadFeatureFlags]; + if (configuration.loadFeatureFlagsOnStart) { + [self reloadFeatureFlags]; + } } return self; } diff --git a/PostHog/Classes/PHGPostHogConfiguration.h b/PostHog/Classes/PHGPostHogConfiguration.h index 49089d577..c649df36c 100644 --- a/PostHog/Classes/PHGPostHogConfiguration.h +++ b/PostHog/Classes/PHGPostHogConfiguration.h @@ -123,6 +123,13 @@ typedef NSMutableURLRequest *_Nonnull (^PHGRequestFactory)(NSURL *_Nonnull); */ @property (nonatomic, assign) BOOL shouldSendDeviceID; +/** + * Whether the posthog client should load feature flags when initialized. + * Changing the value of this property after initializing the client will have no effect. + * The default value is `YES`. + */ +@property (nonatomic, assign) BOOL loadFeatureFlagsOnStart; + /** * Dictionary indicating the options the app was launched with. */ diff --git a/PostHog/Classes/PHGPostHogConfiguration.m b/PostHog/Classes/PHGPostHogConfiguration.m index c85108399..d5111a34d 100644 --- a/PostHog/Classes/PHGPostHogConfiguration.m +++ b/PostHog/Classes/PHGPostHogConfiguration.m @@ -57,6 +57,7 @@ - (instancetype)init self.maxQueueSize = 1000; self.libraryName = @"posthog-ios"; self.libraryVersion = [PHGPostHog version]; + self.loadFeatureFlagsOnStart = YES; self.payloadFilters = @{ @"(fb\\d+://authorize#access_token=)([^ ]+)": @"$1((redacted/fb-auth-token))" }; diff --git a/PostHogTests/PostHogTests.swift b/PostHogTests/PostHogTests.swift index d0bc904fc..826c0cab7 100644 --- a/PostHogTests/PostHogTests.swift +++ b/PostHogTests/PostHogTests.swift @@ -15,6 +15,7 @@ class PostHogTests: QuickSpec { testApplication = TestApplication() config.application = testApplication config.captureApplicationLifecycleEvents = true + config.loadFeatureFlagsOnStart = true UserDefaults.standard.set("test PHGQueue should be removed", forKey: "PHGQueue") expect(UserDefaults.standard.string(forKey: "PHGQueue")).toNot(beNil()) @@ -40,6 +41,19 @@ class PostHogTests: QuickSpec { expect(posthog.getAnonymousId()).toNot(beNil()) } + it("loads feature flags on init") { + expect(testMiddleware.lastContext?.eventType) == .reloadFeatureFlags + } + + it("doesn't load feature flags on init when configured") { + config.loadFeatureFlagsOnStart = false + testMiddleware = TestMiddleware() + config.middlewares = [testMiddleware] + + posthog = PHGPostHog(configuration: config) + expect(testMiddleware.lastContext).to(beNil()) + } + it("initialized correctly with api host") { let config = PHGPostHogConfiguration(apiKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE", host: "https://testapp.posthog.test") config.libraryName = "posthog-ios-test" @@ -83,7 +97,7 @@ class PostHogTests: QuickSpec { it("fires Application Opened for UIApplicationDidFinishLaunching") { testMiddleware.swallowEvent = true - NotificationCenter.default.post(name: UIApplication.didFinishLaunchingNotification, object: testApplication, userInfo: [ + NotificationCenter.default.post(name: NSNotification.Name.UIApplicationDidFinishLaunching, object: testApplication, userInfo: [ UIApplication.LaunchOptionsKey.sourceApplication: "testApp", UIApplication.LaunchOptionsKey.url: "test://test", ]) @@ -97,7 +111,7 @@ class PostHogTests: QuickSpec { it("fires Application Opened during UIApplicationWillEnterForeground") { testMiddleware.swallowEvent = true - NotificationCenter.default.post(name: UIApplication.willEnterForegroundNotification, object: testApplication) + NotificationCenter.default.post(name: NSNotification.Name.UIApplicationWillEnterForeground, object: testApplication) let event = testMiddleware.lastContext?.payload as? PHGCapturePayload expect(event?.event) == "Application Opened" expect(event?.properties?["from_background"] as? Bool) == true @@ -105,14 +119,14 @@ class PostHogTests: QuickSpec { it("fires Application Backgrounded during UIApplicationDidEnterBackground") { testMiddleware.swallowEvent = true - NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: testApplication) + NotificationCenter.default.post(name: NSNotification.Name.UIApplicationDidEnterBackground, object: testApplication) let event = testMiddleware.lastContext?.payload as? PHGCapturePayload expect(event?.event) == "Application Backgrounded" } it("flushes when UIApplicationDidEnterBackground is fired") { posthog.capture("test") - NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: testApplication) + NotificationCenter.default.post(name: NSNotification.Name.UIApplicationDidEnterBackground, object: testApplication) expect(testApplication.backgroundTasks.count).toEventually(equal(1)) expect(testApplication.backgroundTasks[0].isEnded).toEventually(beFalse()) } From a3c9f9b223f39fa9f3999e87bc221deaae3cbb05 Mon Sep 17 00:00:00 2001 From: Mario Radonic Date: Tue, 3 Oct 2023 15:05:22 +0200 Subject: [PATCH 2/3] PR adjustments. --- PostHog/Classes/PHGPostHog.m | 2 +- PostHog/Classes/PHGPostHogConfiguration.h | 4 +++- PostHog/Classes/PHGPostHogConfiguration.m | 2 +- PostHogTests/PostHogTests.swift | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/PostHog/Classes/PHGPostHog.m b/PostHog/Classes/PHGPostHog.m index f4c49cbc9..210b097dc 100644 --- a/PostHog/Classes/PHGPostHog.m +++ b/PostHog/Classes/PHGPostHog.m @@ -86,7 +86,7 @@ - (instancetype)initWithConfiguration:(PHGPostHogConfiguration *)configuration } #endif - if (configuration.loadFeatureFlagsOnStart) { + if (configuration.preloadFeatureFlags) { [self reloadFeatureFlags]; } } diff --git a/PostHog/Classes/PHGPostHogConfiguration.h b/PostHog/Classes/PHGPostHogConfiguration.h index c649df36c..fa0a29f2b 100644 --- a/PostHog/Classes/PHGPostHogConfiguration.h +++ b/PostHog/Classes/PHGPostHogConfiguration.h @@ -126,9 +126,11 @@ typedef NSMutableURLRequest *_Nonnull (^PHGRequestFactory)(NSURL *_Nonnull); /** * Whether the posthog client should load feature flags when initialized. * Changing the value of this property after initializing the client will have no effect. + * If set to `YES` (default), `PHGPosthog` will automatically call `reloadFeatureFlags` during initialization. + * If set to `NO`, you can manually call `reloadFeatureFlags` when needed. * The default value is `YES`. */ -@property (nonatomic, assign) BOOL loadFeatureFlagsOnStart; +@property (nonatomic, assign) BOOL preloadFeatureFlags; /** * Dictionary indicating the options the app was launched with. diff --git a/PostHog/Classes/PHGPostHogConfiguration.m b/PostHog/Classes/PHGPostHogConfiguration.m index d5111a34d..ab1a3ae4e 100644 --- a/PostHog/Classes/PHGPostHogConfiguration.m +++ b/PostHog/Classes/PHGPostHogConfiguration.m @@ -57,7 +57,7 @@ - (instancetype)init self.maxQueueSize = 1000; self.libraryName = @"posthog-ios"; self.libraryVersion = [PHGPostHog version]; - self.loadFeatureFlagsOnStart = YES; + self.preloadFeatureFlags = YES; self.payloadFilters = @{ @"(fb\\d+://authorize#access_token=)([^ ]+)": @"$1((redacted/fb-auth-token))" }; diff --git a/PostHogTests/PostHogTests.swift b/PostHogTests/PostHogTests.swift index 826c0cab7..75ddecd6e 100644 --- a/PostHogTests/PostHogTests.swift +++ b/PostHogTests/PostHogTests.swift @@ -15,7 +15,7 @@ class PostHogTests: QuickSpec { testApplication = TestApplication() config.application = testApplication config.captureApplicationLifecycleEvents = true - config.loadFeatureFlagsOnStart = true + config.preloadFeatureFlags = true UserDefaults.standard.set("test PHGQueue should be removed", forKey: "PHGQueue") expect(UserDefaults.standard.string(forKey: "PHGQueue")).toNot(beNil()) @@ -46,7 +46,7 @@ class PostHogTests: QuickSpec { } it("doesn't load feature flags on init when configured") { - config.loadFeatureFlagsOnStart = false + config.preloadFeatureFlags = false testMiddleware = TestMiddleware() config.middlewares = [testMiddleware] From 863022510be95f482a29a89852096fc350f1a156 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Mon, 9 Oct 2023 11:01:02 +0200 Subject: [PATCH 3/3] add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47f272a0f..9010b2ace 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Next +- Add loadFeatureFlagsOnStart configuration flag [#71](https://github.com/PostHog/posthog-ios/pull/71) + ## 2.0.5 - 2023-10-06 - Update device type [#63](https://github.com/PostHog/posthog-ios/pull/63)