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

fix: App opened event respects the life cycle config #102

Merged
merged 3 commits into from
Feb 8, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Next

## 3.1.1 - 2024-02-08

- `Application Opened` respects the `captureApplicationLifecycleEvents` config. [#102](https://github.com/PostHog/posthog-ios/pull/102)

## 3.1.0 - 2024-02-07

- Add session tracking [#100](https://github.com/PostHog/posthog-ios/pull/100)
Expand Down
4 changes: 4 additions & 0 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
}

private func captureAppOpened() {
if !config.captureApplicationLifecycleEvents {
return
}

var props: [String: Any] = [:]
props["from_background"] = appFromBackground

Expand Down
1 change: 1 addition & 0 deletions PostHogExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
)
// the ScreenViews for SwiftUI does not work, the names are not useful
config.captureScreenViews = false
config.captureApplicationLifecycleEvents = false
config.flushAt = 1
config.flushIntervalSeconds = 10

Expand Down
40 changes: 31 additions & 9 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Quick
class PostHogSDKTest: QuickSpec {
func getSut(preloadFeatureFlags: Bool = false,
sendFeatureFlagEvent: Bool = false,
captureApplicationLifecycleEvents: Bool = false,
flushAt: Int = 1,
optOut: Bool = false) -> PostHogSDK
{
Expand All @@ -23,6 +24,7 @@ class PostHogSDKTest: QuickSpec {
config.sendFeatureFlagEvent = sendFeatureFlagEvent
config.disableReachabilityForTesting = true
config.disableQueueTimerForTesting = true
config.captureApplicationLifecycleEvents = captureApplicationLifecycleEvents
config.optOut = optOut
return PostHogSDK.with(config)
}
Expand Down Expand Up @@ -284,7 +286,7 @@ class PostHogSDKTest: QuickSpec {
}

it("capture AppBackgrounded") {
let sut = self.getSut()
let sut = self.getSut(captureApplicationLifecycleEvents: true)

sut.handleAppDidEnterBackground()

Expand All @@ -300,7 +302,7 @@ class PostHogSDKTest: QuickSpec {
}

it("capture AppInstalled") {
let sut = self.getSut()
let sut = self.getSut(captureApplicationLifecycleEvents: true)

sut.handleAppDidFinishLaunching()

Expand All @@ -318,7 +320,7 @@ class PostHogSDKTest: QuickSpec {
}

it("capture AppUpdated") {
let sut = self.getSut()
let sut = self.getSut(captureApplicationLifecycleEvents: true)

let userDefaults = UserDefaults.standard
userDefaults.setValue("1.0.0", forKey: "PHGVersionKey")
Expand All @@ -343,7 +345,7 @@ class PostHogSDKTest: QuickSpec {
}

it("capture AppOpenedFromBackground from_background should be false") {
let sut = self.getSut()
let sut = self.getSut(captureApplicationLifecycleEvents: true)

sut.handleAppDidBecomeActive()

Expand All @@ -360,7 +362,7 @@ class PostHogSDKTest: QuickSpec {
}

it("capture AppOpenedFromBackground from_background should be true") {
let sut = self.getSut(flushAt: 2)
let sut = self.getSut(captureApplicationLifecycleEvents: true, flushAt: 2)

sut.handleAppDidBecomeActive()
sut.handleAppDidBecomeActive()
Expand All @@ -378,7 +380,7 @@ class PostHogSDKTest: QuickSpec {
}

it("capture captureAppOpened") {
let sut = self.getSut()
let sut = self.getSut(captureApplicationLifecycleEvents: true)

sut.handleAppDidBecomeActive()

Expand All @@ -396,6 +398,26 @@ class PostHogSDKTest: QuickSpec {
sut.close()
}

it("does not capture life cycle events") {
let sut = self.getSut()

sut.handleAppDidFinishLaunching()
sut.handleAppDidBecomeActive()
sut.handleAppDidEnterBackground()

sut.screen("test")

let events = getBatchedEvents(server)

expect(events.count) == 1

let event = events.first!
expect(event.event) == "$screen"

sut.reset()
sut.close()
}

it("reloadFeatureFlags adds groups if any") {
let sut = self.getSut()

Expand Down Expand Up @@ -527,7 +549,7 @@ class PostHogSDKTest: QuickSpec {
}

it("sets sessionId on app start") {
let sut = self.getSut()
let sut = self.getSut(captureApplicationLifecycleEvents: true)

sut.handleAppDidBecomeActive()

Expand Down Expand Up @@ -571,7 +593,7 @@ class PostHogSDKTest: QuickSpec {
}

it("rotates to a new sessionId only after > 30 mins in the background") {
let sut = self.getSut(flushAt: 5)
let sut = self.getSut(captureApplicationLifecycleEvents: true, flushAt: 5)
let mockNow = MockDate()
sut.now = { mockNow.date }

Expand Down Expand Up @@ -608,7 +630,7 @@ class PostHogSDKTest: QuickSpec {
}

it("clears sessionId for background events after 30 mins in background") {
let sut = self.getSut(flushAt: 2)
let sut = self.getSut(captureApplicationLifecycleEvents: true, flushAt: 2)
let mockNow = MockDate()
sut.now = { mockNow.date }

Expand Down