Skip to content

Commit

Permalink
Fix telemetry opt-out through attribution dialog (better fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Yeshkunov committed Oct 14, 2021
1 parent d84adc3 commit be5976e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone.
* Make `PointAnnotation.Image`'s fields public. ([#753](https://github.com/mapbox/mapbox-maps-ios/pull/753))
* Passing an unsupported locale into `Style.localizeLabels(into:forLayerIds:)` throws an error instead of crashing. ([#752](https://github.com/mapbox/mapbox-maps-ios/pull/752))
* Set `MapboxMap` flags during gestures and animations. ([#754](https://github.com/mapbox/mapbox-maps-ios/pull/754))
* Fix telemetry opt-out through attribution dialog. ([#758](https://github.com/mapbox/mapbox-maps-ios/pull/758))

## 10.0.0 - October 6, 2021

Expand Down
26 changes: 22 additions & 4 deletions Sources/MapboxMaps/Foundation/Events/EventsManager.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
import UIKit
import MapboxMobileEvents

extension UserDefaults {
// dynamic var's name has to be the same as corresponding key in UserDefaults
// to make KVO observing work properly
@objc dynamic var MGLMapboxMetricsEnabled: Bool {
get {
return bool(forKey: #keyPath(MGLMapboxMetricsEnabled))
}
set {
set(newValue, forKey: #keyPath(MGLMapboxMetricsEnabled))
}
}
}

internal class EventsManager: EventsListener {
private enum Constants {
static let MGLAPIClientUserAgentBase = "mapbox-maps-ios"
}

var telemetry: TelemetryProtocol!
private var metricsEnabledObservation: NSKeyValueObservation?

init(accessToken: String) {
let sdkVersion = "10.0.0"
let sdkVersion = Bundle.mapboxMapsMetadata.version
let mmeEventsManager = MMEEventsManager.shared()
telemetry = mmeEventsManager
mmeEventsManager.initialize(withAccessToken: accessToken,
userAgentBase: Constants.MGLAPIClientUserAgentBase,
hostSDKVersion: sdkVersion)
mmeEventsManager.skuId = "00"
}

init(with telemetry: TelemetryProtocol?) {
self.telemetry = telemetry
metricsEnabledObservation = UserDefaults.standard.observe(\.MGLMapboxMetricsEnabled, options: [.initial, .new]) { _, change in
DispatchQueue.main.async {
guard let newValue = change.newValue else { return }
UserDefaults.mme_configuration().mme_isCollectionEnabled = newValue
MMEEventsManager.shared().pauseOrResumeMetricsCollectionIfRequired()
}
}
}

func push(event: EventType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class InfoButtonOrnament: UIView {
}

internal var isMetricsEnabled: Bool {
return UserDefaults.standard.bool(forKey: Ornaments.metricsEnabledKey)
return UserDefaults.standard.MGLMapboxMetricsEnabled
}

internal weak var delegate: InfoButtonOrnamentDelegate?
Expand Down
1 change: 0 additions & 1 deletion Sources/MapboxMaps/Ornaments/OrnamentsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public enum OrnamentVisibility: String, Equatable {

internal struct Ornaments {
static let localizableTableName = "OrnamentsLocalizable"
static let metricsEnabledKey = "MGLMapboxMetricsEnabled"
static let telemetryURL = "https://www.mapbox.com/telemetry/"
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/MapboxMaps/Style/AttributionDialogManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ internal class AttributionDialogManager {

internal var isMetricsEnabled: Bool {
get {
UserDefaults.standard.bool(forKey: Ornaments.metricsEnabledKey)
UserDefaults.standard.MGLMapboxMetricsEnabled
}
set {
UserDefaults.standard.set(newValue, forKey: Ornaments.metricsEnabledKey)
UserDefaults.standard.MGLMapboxMetricsEnabled = newValue
}
}

Expand Down
43 changes: 43 additions & 0 deletions Tests/MapboxMapsTests/Foundation/Events/EventsManagerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import XCTest
@testable import MapboxMaps

final class EventsManagerTests: XCTestCase {

var eventsManager: EventsManager!

override func setUp() {
eventsManager = EventsManager(accessToken: "empty")
}

override func tearDown() {
eventsManager = nil
}

func testUserDefaultsDynamicProperty() {
UserDefaults.standard.MGLMapboxMetricsEnabled = false

UserDefaults.standard.MGLMapboxMetricsEnabled = true
XCTAssertEqual(UserDefaults.standard.bool(forKey: "MGLMapboxMetricsEnabled"), true)

UserDefaults.standard.MGLMapboxMetricsEnabled = false
XCTAssertEqual(UserDefaults.standard.bool(forKey: "MGLMapboxMetricsEnabled"), false)

UserDefaults.standard.set(true, forKey: "MGLMapboxMetricsEnabled")
XCTAssertEqual(UserDefaults.standard.MGLMapboxMetricsEnabled, true)

UserDefaults.standard.set(false, forKey: "MGLMapboxMetricsEnabled")
XCTAssertEqual(UserDefaults.standard.MGLMapboxMetricsEnabled, false)
}

func testMGLMapboxMetricsModifiesMMECollectionDisabled() {
UserDefaults.standard.MGLMapboxMetricsEnabled = false

let trueExpectation = keyValueObservingExpectation(for: UserDefaults.mme_configuration(), keyPath: "MMECollectionDisabled", expectedValue: false)
UserDefaults.standard.MGLMapboxMetricsEnabled = true
wait(for: [trueExpectation], timeout: 1)

let falseExpectation = keyValueObservingExpectation(for: UserDefaults.mme_configuration(), keyPath: "MMECollectionDisabled", expectedValue: true)
UserDefaults.standard.MGLMapboxMetricsEnabled = false
wait(for: [falseExpectation], timeout: 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class InfoButtonOrnamentTests: XCTestCase {
infoButton.delegate = attributionDialogManager

parentViewController.view.addSubview(infoButton)
UserDefaults.standard.set(true, forKey: Ornaments.metricsEnabledKey)
UserDefaults.standard.MGLMapboxMetricsEnabled = true
infoButton.infoTapped()

var infoAlert = try XCTUnwrap(parentViewController.currentAlert, "The info alert controller could not be found.")
Expand Down Expand Up @@ -70,7 +70,7 @@ class InfoButtonOrnamentTests: XCTestCase {
}

func testTelemetryOptIn() throws {
UserDefaults.standard.set(false, forKey: Ornaments.metricsEnabledKey)
UserDefaults.standard.MGLMapboxMetricsEnabled = false
let infoButton = InfoButtonOrnament()
infoButton.delegate = attributionDialogManager

Expand Down

0 comments on commit be5976e

Please sign in to comment.