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 loadFeatureFlagsOnStart configuration flag #71

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion PostHog/Classes/PHGPostHog.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ - (instancetype)initWithConfiguration:(PHGPostHogConfiguration *)configuration
}
#endif

[self reloadFeatureFlags];
if (configuration.preloadFeatureFlags) {
[self reloadFeatureFlags];
}
}
return self;
}
Expand Down
9 changes: 9 additions & 0 deletions PostHog/Classes/PHGPostHogConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ 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.
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
* 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 preloadFeatureFlags;

/**
* Dictionary indicating the options the app was launched with.
*/
Expand Down
1 change: 1 addition & 0 deletions PostHog/Classes/PHGPostHogConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ - (instancetype)init
self.maxQueueSize = 1000;
self.libraryName = @"posthog-ios";
self.libraryVersion = [PHGPostHog version];
self.preloadFeatureFlags = YES;
self.payloadFilters = @{
@"(fb\\d+://authorize#access_token=)([^ ]+)": @"$1((redacted/fb-auth-token))"
};
Expand Down
22 changes: 18 additions & 4 deletions PostHogTests/PostHogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PostHogTests: QuickSpec {
testApplication = TestApplication()
config.application = testApplication
config.captureApplicationLifecycleEvents = true
config.preloadFeatureFlags = true

UserDefaults.standard.set("test PHGQueue should be removed", forKey: "PHGQueue")
expect(UserDefaults.standard.string(forKey: "PHGQueue")).toNot(beNil())
Expand All @@ -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.preloadFeatureFlags = 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"
Expand Down Expand Up @@ -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",
])
Expand All @@ -97,22 +111,22 @@ 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
}

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())
}
Expand Down