Skip to content

Commit

Permalink
fix: track changes to defaultTracking after initialization (#208)
Browse files Browse the repository at this point in the history
* fix: track changes to defaultTracking after initialization

* fix: replace defaultTracking with autocapture if changed

* fix: fix typo

* fix: fix nit

* style: rename parameter name
  • Loading branch information
PouriaAmini authored Aug 7, 2024
1 parent f0f7c40 commit 5c72cc0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 32 deletions.
17 changes: 14 additions & 3 deletions Sources/Amplitude/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ public class Configuration {
public var identifyBatchIntervalMillis: Int
public internal(set) var migrateLegacyData: Bool
@available(*, deprecated, renamed: "autocapture", message: "Please use `autocapture` instead.")
/// The SDK no longer tracks changes to the defaultTracking options after initialization.
public var defaultTracking: DefaultTrackingOptions = DefaultTrackingOptions() {
didSet { autocapture = defaultTracking.autocaptureOptions }
public lazy var defaultTracking: DefaultTrackingOptions = {
DefaultTrackingOptions(delegate: self)
}() {
didSet {
defaultTracking.delegate = self
autocapture = defaultTracking.autocaptureOptions
}
}
public internal(set) var autocapture: AutocaptureOptions
public var offline: Bool?
Expand Down Expand Up @@ -178,3 +182,10 @@ public class Configuration {
return Configuration.getNormalizeInstanceName(self.instanceName)
}
}

extension Configuration: DefaultTrackingOptionsDelegate {
@available(*, deprecated)
func didChangeOptions(options: DefaultTrackingOptions) {
autocapture = options.autocaptureOptions
}
}
54 changes: 25 additions & 29 deletions Sources/Amplitude/DefaultTrackingOptions.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import Foundation

protocol DefaultTrackingOptionsDelegate: AnyObject {
@available(*, deprecated)
func didChangeOptions(options: DefaultTrackingOptions)
}

@available(*, deprecated, renamed: "AutocaptureOptions", message: "Please use `AutocaptureOptions` instead")
public class DefaultTrackingOptions {
public static var ALL: DefaultTrackingOptions {
Expand All @@ -10,54 +15,45 @@ public class DefaultTrackingOptions {
}

public var sessions: Bool {
get {
autocaptureOptions.contains(.sessions)
}
set {
if newValue {
autocaptureOptions.insert(.sessions)
} else {
autocaptureOptions.remove(.sessions)
}
didSet {
delegate?.didChangeOptions(options: self)
}
}

public var appLifecycles: Bool {
get {
autocaptureOptions.contains(.appLifecycles)
}
set {
if newValue {
autocaptureOptions.insert(.appLifecycles)
} else {
autocaptureOptions.remove(.appLifecycles)
}
didSet {
delegate?.didChangeOptions(options: self)
}
}

public var screenViews: Bool {
get {
autocaptureOptions.contains(.screenViews)
}
set {
if newValue {
autocaptureOptions.insert(.screenViews)
} else {
autocaptureOptions.remove(.screenViews)
}
didSet {
delegate?.didChangeOptions(options: self)
}
}

var autocaptureOptions: AutocaptureOptions
weak var delegate: DefaultTrackingOptionsDelegate?

var autocaptureOptions: AutocaptureOptions {
return [
sessions ? .sessions : [],
appLifecycles ? .appLifecycles : [],
screenViews ? .screenViews : []
].reduce(into: []) { $0.formUnion($1) }
}

public init(
sessions: Bool = true,
appLifecycles: Bool = false,
screenViews: Bool = false
) {
self.autocaptureOptions = []
self.sessions = sessions
self.appLifecycles = appLifecycles
self.screenViews = screenViews
}

convenience init(delegate: DefaultTrackingOptionsDelegate) {
self.init()
self.delegate = delegate
}
}
45 changes: 45 additions & 0 deletions Tests/AmplitudeTests/AutocaptureOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,49 @@ final class AutocaptureOptionsTests: XCTestCase {
XCTAssertFalse(options.contains(.sessions))
XCTAssertTrue(options.contains(.elementInteractions))
}

func testDefaultTrackingOptionChangesReflectInAutocapture() {
let configuration = Configuration(
apiKey: "test-api-key"
)

XCTAssertTrue(configuration.autocapture.contains(.sessions))

(configuration as DeprecationWarningDiscardable).setDefaultTrackingOptions(sessions: false, appLifecycles: true, screenViews: true)

XCTAssertFalse(configuration.autocapture.contains(.sessions))
XCTAssertTrue(configuration.autocapture.contains(.appLifecycles))
XCTAssertTrue(configuration.autocapture.contains(.screenViews))
}

func testDefaultTrackingInstanceChangeReflectInAutocapture() {
let configuration = Configuration(
apiKey: "test-api-key"
)

(configuration as DeprecationWarningDiscardable).setDefaultTracking(sessions: false, appLifecycles: true, screenViews: true)

XCTAssertFalse(configuration.autocapture.contains(.sessions))
XCTAssertTrue(configuration.autocapture.contains(.appLifecycles))
XCTAssertTrue(configuration.autocapture.contains(.screenViews))
}
}

private protocol DeprecationWarningDiscardable {
func setDefaultTracking(sessions: Bool, appLifecycles: Bool, screenViews: Bool)
func setDefaultTrackingOptions(sessions: Bool, appLifecycles: Bool, screenViews: Bool)
}

extension Configuration: DeprecationWarningDiscardable {
@available(*, deprecated)
func setDefaultTracking(sessions: Bool, appLifecycles: Bool, screenViews: Bool) {
defaultTracking = DefaultTrackingOptions(sessions: sessions, appLifecycles: appLifecycles, screenViews: screenViews)
}

@available(*, deprecated)
func setDefaultTrackingOptions(sessions: Bool, appLifecycles: Bool, screenViews: Bool) {
defaultTracking.sessions = sessions
defaultTracking.appLifecycles = appLifecycles
defaultTracking.screenViews = screenViews
}
}

0 comments on commit 5c72cc0

Please sign in to comment.