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:
Nicolas Mauri
2023-04-21 17:05:39 +02:00
committed by GitHub
parent 078799c1e2
commit b432bd6fe4
8 changed files with 78 additions and 30 deletions
@@ -295,7 +295,8 @@ class AppCoordinator: AppCoordinatorProtocol {
tearDownUserSession() tearDownUserSession()
// reset analytics // reset analytics
ServiceLocator.shared.analytics.reset() ServiceLocator.shared.analytics.optOut()
ServiceLocator.shared.analytics.resetConsentState()
stateMachine.processEvent(.completedSigningOut(isSoft: isSoft)) stateMachine.processEvent(.completedSigningOut(isSoft: isSoft))
} }
+5 -10
View File
@@ -23,7 +23,7 @@ final class AppSettings: ObservableObject {
case lastVersionLaunched case lastVersionLaunched
case seenInvites case seenInvites
case timelineStyle case timelineStyle
case enableAnalytics case analyticsConsentState
case enableInAppNotifications case enableInAppNotifications
case pusherProfileTag case pusherProfileTag
case shouldCollapseRoomStateEvents case shouldCollapseRoomStateEvents
@@ -126,14 +126,9 @@ final class AppSettings: ObservableObject {
termsURL: URL(staticString: "https://element.io/cookie-policy")) termsURL: URL(staticString: "https://element.io/cookie-policy"))
#endif #endif
/// Whether the user has already been shown the PostHog analytics prompt. /// Whether the user has opted in to send analytics.
var hasSeenAnalyticsPrompt: Bool { @UserPreference(key: UserDefaultsKeys.analyticsConsentState, defaultValue: AnalyticsConsentState.unknown, storageType: .userDefaults(store))
Self.store.object(forKey: UserDefaultsKeys.enableAnalytics.rawValue) != nil var analyticsConsentState
}
/// `true` when the user has opted in to send analytics.
@UserPreference(key: UserDefaultsKeys.enableAnalytics, defaultValue: false, storageType: .userDefaults(store))
var enableAnalytics
// MARK: - Room Screen // MARK: - Room Screen
@@ -145,7 +140,7 @@ final class AppSettings: ObservableObject {
// MARK: - Notifications // MARK: - Notifications
@UserPreference(key: UserDefaultsKeys.timelineStyle, defaultValue: true, storageType: .userDefaults(store)) @UserPreference(key: UserDefaultsKeys.enableInAppNotifications, defaultValue: true, storageType: .userDefaults(store))
var enableInAppNotifications var enableInAppNotifications
/// Tag describing which set of device specific rules a pusher executes. /// Tag describing which set of device specific rules a pusher executes.
@@ -22,12 +22,13 @@ typealias AnalyticsSettingsScreenViewModelType = StateStoreViewModel<AnalyticsSe
class AnalyticsSettingsScreenViewModel: AnalyticsSettingsScreenViewModelType, AnalyticsSettingsScreenViewModelProtocol { class AnalyticsSettingsScreenViewModel: AnalyticsSettingsScreenViewModelType, AnalyticsSettingsScreenViewModelProtocol {
init() { init() {
let strings = AnalyticsSettingsScreenStrings(termsURL: ServiceLocator.shared.settings.analyticsConfiguration.termsURL) 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) let state = AnalyticsSettingsScreenViewState(strings: strings, bindings: bindings)
super.init(initialViewState: state) super.init(initialViewState: state)
ServiceLocator.shared.settings.$enableAnalytics ServiceLocator.shared.settings.$analyticsConsentState
.map { $0 == .optedIn }
.weakAssign(to: \.state.bindings.enableAnalytics, on: self) .weakAssign(to: \.state.bindings.enableAnalytics, on: self)
.store(in: &cancellables) .store(in: &cancellables)
} }
@@ -35,7 +36,7 @@ class AnalyticsSettingsScreenViewModel: AnalyticsSettingsScreenViewModelType, An
override func process(viewAction: AnalyticsSettingsScreenViewAction) { override func process(viewAction: AnalyticsSettingsScreenViewAction) {
switch viewAction { switch viewAction {
case .toggleAnalytics: case .toggleAnalytics:
if ServiceLocator.shared.settings.enableAnalytics { if ServiceLocator.shared.analytics.isEnabled {
ServiceLocator.shared.analytics.optOut() ServiceLocator.shared.analytics.optOut()
} else { } else {
ServiceLocator.shared.analytics.optIn() ServiceLocator.shared.analytics.optIn()
@@ -44,18 +44,22 @@ class Analytics {
/// Whether to show the user the analytics opt in prompt. /// Whether to show the user the analytics opt in prompt.
var shouldShowAnalyticsPrompt: Bool { var shouldShowAnalyticsPrompt: Bool {
// Only show the prompt once, and when analytics are enabled in BuildSettings. // 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. /// Opts in to analytics tracking with the supplied user session.
func optIn() { func optIn() {
ServiceLocator.shared.settings.enableAnalytics = true ServiceLocator.shared.settings.analyticsConsentState = .optedIn
startIfEnabled() startIfEnabled()
} }
/// Stops analytics tracking and calls `reset` to clear any IDs and event queues. /// Stops analytics tracking and calls `reset` to clear any IDs and event queues.
func optOut() { func optOut() {
ServiceLocator.shared.settings.enableAnalytics = false ServiceLocator.shared.settings.analyticsConsentState = .optedOut
// The order is important here. PostHog ignores the reset if stopped. // The order is important here. PostHog ignores the reset if stopped.
reset() reset()
@@ -66,7 +70,7 @@ class Analytics {
/// Starts the analytics client if the user has opted in, otherwise does nothing. /// Starts the analytics client if the user has opted in, otherwise does nothing.
func startIfEnabled() { func startIfEnabled() {
guard ServiceLocator.shared.settings.enableAnalytics, !isRunning else { return } guard isEnabled, !isRunning else { return }
client.start() client.start()
ServiceLocator.shared.bugReportService.start() ServiceLocator.shared.bugReportService.start()
@@ -87,6 +91,12 @@ class Analytics {
MXLog.info("Reset.") 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. /// 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 /// Normally events are sent in batches. Call this method when you need an event
/// to be sent immediately. /// 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
}
@@ -42,12 +42,13 @@ class AnalyticsSettingsScreenViewModelTests: XCTestCase {
} }
func testOptIn() { func testOptIn() {
applicationSettings.analyticsConsentState = .optedOut
context.send(viewAction: .toggleAnalytics) context.send(viewAction: .toggleAnalytics)
XCTAssertTrue(context.enableAnalytics) XCTAssertTrue(context.enableAnalytics)
} }
func testOptOut() { func testOptOut() {
applicationSettings.enableAnalytics = true applicationSettings.analyticsConsentState = .optedIn
context.send(viewAction: .toggleAnalytics) context.send(viewAction: .toggleAnalytics)
XCTAssertFalse(context.enableAnalytics) XCTAssertFalse(context.enableAnalytics)
} }
+25 -9
View File
@@ -47,7 +47,7 @@ class AnalyticsTests: XCTestCase {
func testAnalyticsPromptUserDeclinedPostHog() { func testAnalyticsPromptUserDeclinedPostHog() {
// Given an existing install of the app where the user previously declined PostHog // Given an existing install of the app where the user previously declined PostHog
applicationSettings.enableAnalytics = false applicationSettings.analyticsConsentState = .optedOut
// When the user is prompted for analytics // When the user is prompted for analytics
let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt
@@ -58,7 +58,7 @@ class AnalyticsTests: XCTestCase {
func testAnalyticsPromptUserAcceptedPostHog() { func testAnalyticsPromptUserAcceptedPostHog() {
// Given an existing install of the app where the user previously accepted PostHog // Given an existing install of the app where the user previously accepted PostHog
applicationSettings.enableAnalytics = true applicationSettings.analyticsConsentState = .optedIn
// When the user is prompted for analytics // When the user is prompted for analytics
let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt
@@ -69,7 +69,8 @@ class AnalyticsTests: XCTestCase {
func testAnalyticsPromptNotDisplayed() { func testAnalyticsPromptNotDisplayed() {
// Given a fresh install of the app both Analytics and BugReportService should be disabled // Given a fresh install of the app both Analytics and BugReportService should be disabled
XCTAssertFalse(ServiceLocator.shared.settings.enableAnalytics) XCTAssertEqual(ServiceLocator.shared.settings.analyticsConsentState, .unknown)
XCTAssertFalse(ServiceLocator.shared.analytics.isEnabled)
XCTAssertFalse(ServiceLocator.shared.analytics.isRunning) XCTAssertFalse(ServiceLocator.shared.analytics.isRunning)
XCTAssertFalse(analyticsClient.startCalled) XCTAssertFalse(analyticsClient.startCalled)
XCTAssertFalse(bugReportService.startCalled) XCTAssertFalse(bugReportService.startCalled)
@@ -80,7 +81,8 @@ class AnalyticsTests: XCTestCase {
// When analytics is opt-out // When analytics is opt-out
ServiceLocator.shared.analytics.optOut() ServiceLocator.shared.analytics.optOut()
// Then analytics should be disabled // Then analytics should be disabled
XCTAssertFalse(applicationSettings.enableAnalytics) XCTAssertEqual(applicationSettings.analyticsConsentState, .optedOut)
XCTAssertFalse(ServiceLocator.shared.analytics.isEnabled)
XCTAssertFalse(ServiceLocator.shared.analytics.isRunning) XCTAssertFalse(ServiceLocator.shared.analytics.isRunning)
XCTAssertFalse(analyticsClient.isRunning) XCTAssertFalse(analyticsClient.isRunning)
XCTAssertFalse(bugReportService.isRunning) XCTAssertFalse(bugReportService.isRunning)
@@ -94,7 +96,8 @@ class AnalyticsTests: XCTestCase {
// When analytics is opt-in // When analytics is opt-in
ServiceLocator.shared.analytics.optIn() ServiceLocator.shared.analytics.optIn()
// The analytics should be enabled // The analytics should be enabled
XCTAssertTrue(applicationSettings.enableAnalytics) XCTAssertEqual(applicationSettings.analyticsConsentState, .optedIn)
XCTAssertTrue(ServiceLocator.shared.analytics.isEnabled)
// Analytics client and the bug report service should have been started // Analytics client and the bug report service should have been started
XCTAssertTrue(analyticsClient.startCalled) XCTAssertTrue(analyticsClient.startCalled)
XCTAssertTrue(bugReportService.startCalled) XCTAssertTrue(bugReportService.startCalled)
@@ -102,20 +105,20 @@ class AnalyticsTests: XCTestCase {
func testAnalyticsStartIfNotEnabled() { func testAnalyticsStartIfNotEnabled() {
// Given an existing install of the app where the user previously declined the tracking // Given an existing install of the app where the user previously declined the tracking
applicationSettings.enableAnalytics = false applicationSettings.analyticsConsentState = .optedOut
// Analytics should not start // Analytics should not start
XCTAssertFalse(ServiceLocator.shared.analytics.isEnabled)
ServiceLocator.shared.analytics.startIfEnabled() ServiceLocator.shared.analytics.startIfEnabled()
XCTAssertFalse(ServiceLocator.shared.settings.enableAnalytics)
XCTAssertFalse(analyticsClient.startCalled) XCTAssertFalse(analyticsClient.startCalled)
XCTAssertFalse(bugReportService.startCalled) XCTAssertFalse(bugReportService.startCalled)
} }
func testAnalyticsStartIfEnabled() { func testAnalyticsStartIfEnabled() {
// Given an existing install of the app where the user previously accpeted the tracking // Given an existing install of the app where the user previously accpeted the tracking
applicationSettings.enableAnalytics = true applicationSettings.analyticsConsentState = .optedIn
// Analytics should start // Analytics should start
XCTAssertTrue(ServiceLocator.shared.analytics.isEnabled)
ServiceLocator.shared.analytics.startIfEnabled() ServiceLocator.shared.analytics.startIfEnabled()
XCTAssertTrue(ServiceLocator.shared.settings.enableAnalytics)
XCTAssertTrue(analyticsClient.startCalled) XCTAssertTrue(analyticsClient.startCalled)
XCTAssertTrue(bugReportService.startCalled) XCTAssertTrue(bugReportService.startCalled)
} }
@@ -182,4 +185,17 @@ class AnalyticsTests: XCTestCase {
// Then the properties should be cleared // Then the properties should be cleared
XCTAssertNil(client.pendingUserProperties, "The user properties should be cleared.") XCTAssertNil(client.pendingUserProperties, "The user properties should be cleared.")
} }
func testResetConsentState() {
// Given an existing install of the app where the user previously accpeted the tracking
applicationSettings.analyticsConsentState = .optedIn
XCTAssertFalse(ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt)
// When forgetting analytics consents
ServiceLocator.shared.analytics.resetConsentState()
// Then the analytics prompt should be presented again
XCTAssertEqual(applicationSettings.analyticsConsentState, .unknown)
XCTAssertTrue(ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt)
}
} }
+1
View File
@@ -0,0 +1 @@
Analytics: reset user's consents on logout.