Skip to content

Commit b9b4831

Browse files
marandanetoeakurnikov
authored andcommitted
fix: App opened event respects the life cycle config (PostHog#102)
1 parent 1164d00 commit b9b4831

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Next
22

3+
## 3.1.1 - 2024-02-08
4+
5+
- `Application Opened` respects the `captureApplicationLifecycleEvents` config. [#102](https://github.com/PostHog/posthog-ios/pull/102)
6+
37
## 3.1.0 - 2024-02-07
48

59
- Add session tracking [#100](https://github.com/PostHog/posthog-ios/pull/100)

PostHog/PostHogSDK.swift

+4
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
800800
}
801801

802802
private func captureAppOpened() {
803+
if !config.captureApplicationLifecycleEvents {
804+
return
805+
}
806+
803807
var props: [String: Any] = [:]
804808
props["from_background"] = appFromBackground
805809

PostHogExample/AppDelegate.swift

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
1616
)
1717
// the ScreenViews for SwiftUI does not work, the names are not useful
1818
config.captureScreenViews = false
19+
config.captureApplicationLifecycleEvents = false
1920
config.flushAt = 1
2021
config.flushIntervalSeconds = 10
2122

PostHogTests/PostHogSDKTest.swift

+31-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Quick
1414
class PostHogSDKTest: QuickSpec {
1515
func getSut(preloadFeatureFlags: Bool = false,
1616
sendFeatureFlagEvent: Bool = false,
17+
captureApplicationLifecycleEvents: Bool = false,
1718
flushAt: Int = 1,
1819
optOut: Bool = false) -> PostHogSDK
1920
{
@@ -23,6 +24,7 @@ class PostHogSDKTest: QuickSpec {
2324
config.sendFeatureFlagEvent = sendFeatureFlagEvent
2425
config.disableReachabilityForTesting = true
2526
config.disableQueueTimerForTesting = true
27+
config.captureApplicationLifecycleEvents = captureApplicationLifecycleEvents
2628
config.optOut = optOut
2729
return PostHogSDK.with(config)
2830
}
@@ -284,7 +286,7 @@ class PostHogSDKTest: QuickSpec {
284286
}
285287

286288
it("capture AppBackgrounded") {
287-
let sut = self.getSut()
289+
let sut = self.getSut(captureApplicationLifecycleEvents: true)
288290

289291
sut.handleAppDidEnterBackground()
290292

@@ -300,7 +302,7 @@ class PostHogSDKTest: QuickSpec {
300302
}
301303

302304
it("capture AppInstalled") {
303-
let sut = self.getSut()
305+
let sut = self.getSut(captureApplicationLifecycleEvents: true)
304306

305307
sut.handleAppDidFinishLaunching()
306308

@@ -318,7 +320,7 @@ class PostHogSDKTest: QuickSpec {
318320
}
319321

320322
it("capture AppUpdated") {
321-
let sut = self.getSut()
323+
let sut = self.getSut(captureApplicationLifecycleEvents: true)
322324

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

345347
it("capture AppOpenedFromBackground from_background should be false") {
346-
let sut = self.getSut()
348+
let sut = self.getSut(captureApplicationLifecycleEvents: true)
347349

348350
sut.handleAppDidBecomeActive()
349351

@@ -360,7 +362,7 @@ class PostHogSDKTest: QuickSpec {
360362
}
361363

362364
it("capture AppOpenedFromBackground from_background should be true") {
363-
let sut = self.getSut(flushAt: 2)
365+
let sut = self.getSut(captureApplicationLifecycleEvents: true, flushAt: 2)
364366

365367
sut.handleAppDidBecomeActive()
366368
sut.handleAppDidBecomeActive()
@@ -378,7 +380,7 @@ class PostHogSDKTest: QuickSpec {
378380
}
379381

380382
it("capture captureAppOpened") {
381-
let sut = self.getSut()
383+
let sut = self.getSut(captureApplicationLifecycleEvents: true)
382384

383385
sut.handleAppDidBecomeActive()
384386

@@ -396,6 +398,26 @@ class PostHogSDKTest: QuickSpec {
396398
sut.close()
397399
}
398400

401+
it("does not capture life cycle events") {
402+
let sut = self.getSut()
403+
404+
sut.handleAppDidFinishLaunching()
405+
sut.handleAppDidBecomeActive()
406+
sut.handleAppDidEnterBackground()
407+
408+
sut.screen("test")
409+
410+
let events = getBatchedEvents(server)
411+
412+
expect(events.count) == 1
413+
414+
let event = events.first!
415+
expect(event.event) == "$screen"
416+
417+
sut.reset()
418+
sut.close()
419+
}
420+
399421
it("reloadFeatureFlags adds groups if any") {
400422
let sut = self.getSut()
401423

@@ -527,7 +549,7 @@ class PostHogSDKTest: QuickSpec {
527549
}
528550

529551
it("sets sessionId on app start") {
530-
let sut = self.getSut()
552+
let sut = self.getSut(captureApplicationLifecycleEvents: true)
531553

532554
sut.handleAppDidBecomeActive()
533555

@@ -571,7 +593,7 @@ class PostHogSDKTest: QuickSpec {
571593
}
572594

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

@@ -608,7 +630,7 @@ class PostHogSDKTest: QuickSpec {
608630
}
609631

610632
it("clears sessionId for background events after 30 mins in background") {
611-
let sut = self.getSut(flushAt: 2)
633+
let sut = self.getSut(captureApplicationLifecycleEvents: true, flushAt: 2)
612634
let mockNow = MockDate()
613635
sut.now = { mockNow.date }
614636

0 commit comments

Comments
 (0)