mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +00:00
Fix: forget the user’s consents for analytics on logout (#816)
* Fix: forget the user’s consents for analytics on logout * Fix: change how analytics consent state is stored * Fix: renaming of AnalyticsConsentState entries
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -22,12 +22,13 @@ typealias AnalyticsSettingsScreenViewModelType = StateStoreViewModel<AnalyticsSe
|
||||
class AnalyticsSettingsScreenViewModel: AnalyticsSettingsScreenViewModelType, AnalyticsSettingsScreenViewModelProtocol {
|
||||
init() {
|
||||
let strings = AnalyticsSettingsScreenStrings(termsURL: ServiceLocator.shared.settings.analyticsConfiguration.termsURL)
|
||||
let bindings = AnalyticsSettingsScreenViewStateBindings(enableAnalytics: ServiceLocator.shared.settings.enableAnalytics)
|
||||
let bindings = AnalyticsSettingsScreenViewStateBindings(enableAnalytics: ServiceLocator.shared.analytics.isEnabled)
|
||||
let state = AnalyticsSettingsScreenViewState(strings: strings, bindings: bindings)
|
||||
|
||||
super.init(initialViewState: state)
|
||||
|
||||
ServiceLocator.shared.settings.$enableAnalytics
|
||||
ServiceLocator.shared.settings.$analyticsConsentState
|
||||
.map { $0 == .optedIn }
|
||||
.weakAssign(to: \.state.bindings.enableAnalytics, on: self)
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
@@ -35,7 +36,7 @@ class AnalyticsSettingsScreenViewModel: AnalyticsSettingsScreenViewModelType, An
|
||||
override func process(viewAction: AnalyticsSettingsScreenViewAction) {
|
||||
switch viewAction {
|
||||
case .toggleAnalytics:
|
||||
if ServiceLocator.shared.settings.enableAnalytics {
|
||||
if ServiceLocator.shared.analytics.isEnabled {
|
||||
ServiceLocator.shared.analytics.optOut()
|
||||
} else {
|
||||
ServiceLocator.shared.analytics.optIn()
|
||||
|
||||
@@ -44,18 +44,22 @@ class Analytics {
|
||||
/// Whether to show the user the analytics opt in prompt.
|
||||
var shouldShowAnalyticsPrompt: Bool {
|
||||
// Only show the prompt once, and when analytics are enabled in BuildSettings.
|
||||
!ServiceLocator.shared.settings.hasSeenAnalyticsPrompt && ServiceLocator.shared.settings.analyticsConfiguration.isEnabled
|
||||
ServiceLocator.shared.settings.analyticsConsentState == .unknown && ServiceLocator.shared.settings.analyticsConfiguration.isEnabled
|
||||
}
|
||||
|
||||
var isEnabled: Bool {
|
||||
ServiceLocator.shared.settings.analyticsConsentState == .optedIn
|
||||
}
|
||||
|
||||
/// Opts in to analytics tracking with the supplied user session.
|
||||
func optIn() {
|
||||
ServiceLocator.shared.settings.enableAnalytics = true
|
||||
ServiceLocator.shared.settings.analyticsConsentState = .optedIn
|
||||
startIfEnabled()
|
||||
}
|
||||
|
||||
/// Stops analytics tracking and calls `reset` to clear any IDs and event queues.
|
||||
func optOut() {
|
||||
ServiceLocator.shared.settings.enableAnalytics = false
|
||||
ServiceLocator.shared.settings.analyticsConsentState = .optedOut
|
||||
|
||||
// The order is important here. PostHog ignores the reset if stopped.
|
||||
reset()
|
||||
@@ -66,7 +70,7 @@ class Analytics {
|
||||
|
||||
/// Starts the analytics client if the user has opted in, otherwise does nothing.
|
||||
func startIfEnabled() {
|
||||
guard ServiceLocator.shared.settings.enableAnalytics, !isRunning else { return }
|
||||
guard isEnabled, !isRunning else { return }
|
||||
|
||||
client.start()
|
||||
ServiceLocator.shared.bugReportService.start()
|
||||
@@ -87,6 +91,12 @@ class Analytics {
|
||||
MXLog.info("Reset.")
|
||||
}
|
||||
|
||||
/// Reset the consent state for analytics
|
||||
func resetConsentState() {
|
||||
MXLog.warning("Resetting consent state for analytics.")
|
||||
ServiceLocator.shared.settings.analyticsConsentState = .unknown
|
||||
}
|
||||
|
||||
/// Flushes the event queue in the analytics client, uploading all pending events.
|
||||
/// Normally events are sent in batches. Call this method when you need an event
|
||||
/// to be sent immediately.
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Copyright 2023 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum AnalyticsConsentState: String, Codable {
|
||||
case optedOut
|
||||
case optedIn
|
||||
case unknown
|
||||
}
|
||||
Reference in New Issue
Block a user