From eff8e9ee81d34716305bd6f3148c4a93c96594e8 Mon Sep 17 00:00:00 2001 From: Nicolas Mauri Date: Fri, 21 Apr 2023 17:05:39 +0200 Subject: [PATCH] =?UTF-8?q?Fix:=20forget=20the=20user=E2=80=99s=20consents?= =?UTF-8?q?=20for=20analytics=20on=20logout=20(#816)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: forget the user’s consents for analytics on logout * Fix: change how analytics consent state is stored * Fix: renaming of AnalyticsConsentState entries --- .../Sources/Application/AppCoordinator.swift | 3 +- .../Sources/Application/AppSettings.swift | 19 ++++------- .../AnalyticsSettingsScreenViewModel.swift | 7 ++-- .../Services/Analytics/Analytics.swift | 18 +++++++--- .../Analytics/AnalyticsConsentState.swift | 23 +++++++++++++ ...nalyticsSettingsScreenViewModelTests.swift | 3 +- UnitTests/Sources/AnalyticsTests.swift | 34 ++++++++++++++----- changelog.d/pr-816.change | 1 + 8 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 ElementX/Sources/Services/Analytics/AnalyticsConsentState.swift create mode 100644 changelog.d/pr-816.change diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index ad1cdae23..670bdf5b9 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -295,7 +295,8 @@ class AppCoordinator: AppCoordinatorProtocol { tearDownUserSession() // reset analytics - ServiceLocator.shared.analytics.reset() + ServiceLocator.shared.analytics.optOut() + ServiceLocator.shared.analytics.resetConsentState() stateMachine.processEvent(.completedSigningOut(isSoft: isSoft)) } diff --git a/ElementX/Sources/Application/AppSettings.swift b/ElementX/Sources/Application/AppSettings.swift index 64d91b920..b362613fb 100644 --- a/ElementX/Sources/Application/AppSettings.swift +++ b/ElementX/Sources/Application/AppSettings.swift @@ -23,7 +23,7 @@ final class AppSettings: ObservableObject { case lastVersionLaunched case seenInvites case timelineStyle - case enableAnalytics + case analyticsConsentState case enableInAppNotifications case pusherProfileTag case shouldCollapseRoomStateEvents @@ -109,7 +109,7 @@ final class AppSettings: ObservableObject { let bugReportUISIId = "element-auto-uisi" // MARK: - Analytics - + #if DEBUG /// The configuration to use for analytics during development. Set `isEnabled` to false to disable analytics in debug builds. /// **Note:** Analytics are disabled by default for forks. If you are maintaining a fork, set custom configurations. @@ -125,16 +125,11 @@ final class AppSettings: ObservableObject { apiKey: "phc_Jzsm6DTm6V2705zeU5dcNvQDlonOR68XvX2sh1sEOHO", termsURL: URL(staticString: "https://element.io/cookie-policy")) #endif - - /// Whether the user has already been shown the PostHog analytics prompt. - var hasSeenAnalyticsPrompt: Bool { - Self.store.object(forKey: UserDefaultsKeys.enableAnalytics.rawValue) != nil - } - - /// `true` when the user has opted in to send analytics. - @UserPreference(key: UserDefaultsKeys.enableAnalytics, defaultValue: false, storageType: .userDefaults(store)) - var enableAnalytics + /// Whether the user has opted in to send analytics. + @UserPreference(key: UserDefaultsKeys.analyticsConsentState, defaultValue: AnalyticsConsentState.unknown, storageType: .userDefaults(store)) + var analyticsConsentState + // MARK: - Room Screen @UserPreference(key: UserDefaultsKeys.timelineStyle, defaultValue: TimelineStyle.bubbles, storageType: .userDefaults(store)) @@ -145,7 +140,7 @@ final class AppSettings: ObservableObject { // MARK: - Notifications - @UserPreference(key: UserDefaultsKeys.timelineStyle, defaultValue: true, storageType: .userDefaults(store)) + @UserPreference(key: UserDefaultsKeys.enableInAppNotifications, defaultValue: true, storageType: .userDefaults(store)) var enableInAppNotifications /// Tag describing which set of device specific rules a pusher executes. diff --git a/ElementX/Sources/Screens/AnalyticsSettings/AnalyticsSettingsScreenViewModel.swift b/ElementX/Sources/Screens/AnalyticsSettings/AnalyticsSettingsScreenViewModel.swift index d1a933ede..c0c3f056b 100644 --- a/ElementX/Sources/Screens/AnalyticsSettings/AnalyticsSettingsScreenViewModel.swift +++ b/ElementX/Sources/Screens/AnalyticsSettings/AnalyticsSettingsScreenViewModel.swift @@ -22,12 +22,13 @@ typealias AnalyticsSettingsScreenViewModelType = StateStoreViewModel