From e8a13d52e1bffd1c8824df7652dd2cd68df845e4 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> Date: Thu, 8 Feb 2024 14:12:34 +0100 Subject: [PATCH] fix: App opened event respects the life cycle config (#102) --- CHANGELOG.md | 4 ++++ PostHog/PostHogSDK.swift | 4 ++++ PostHogExample/AppDelegate.swift | 1 + PostHogTests/PostHogSDKTest.swift | 40 ++++++++++++++++++++++++------- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d49657abb..a362adeba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/PostHog/PostHogSDK.swift b/PostHog/PostHogSDK.swift index df4b86848..4d827c649 100644 --- a/PostHog/PostHogSDK.swift +++ b/PostHog/PostHogSDK.swift @@ -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 diff --git a/PostHogExample/AppDelegate.swift b/PostHogExample/AppDelegate.swift index ac097e0b2..88b338e31 100644 --- a/PostHogExample/AppDelegate.swift +++ b/PostHogExample/AppDelegate.swift @@ -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 diff --git a/PostHogTests/PostHogSDKTest.swift b/PostHogTests/PostHogSDKTest.swift index 3ce596a4f..bb0cbab01 100644 --- a/PostHogTests/PostHogSDKTest.swift +++ b/PostHogTests/PostHogSDKTest.swift @@ -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 { @@ -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) } @@ -284,7 +286,7 @@ class PostHogSDKTest: QuickSpec { } it("capture AppBackgrounded") { - let sut = self.getSut() + let sut = self.getSut(captureApplicationLifecycleEvents: true) sut.handleAppDidEnterBackground() @@ -300,7 +302,7 @@ class PostHogSDKTest: QuickSpec { } it("capture AppInstalled") { - let sut = self.getSut() + let sut = self.getSut(captureApplicationLifecycleEvents: true) sut.handleAppDidFinishLaunching() @@ -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") @@ -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() @@ -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() @@ -378,7 +380,7 @@ class PostHogSDKTest: QuickSpec { } it("capture captureAppOpened") { - let sut = self.getSut() + let sut = self.getSut(captureApplicationLifecycleEvents: true) sut.handleAppDidBecomeActive() @@ -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() @@ -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() @@ -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 } @@ -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 }