@@ -18,6 +18,18 @@ public class WooAnalytics {
1818 ///
1919 private var applicationOpenedTime : Date ?
2020
21+ /// Check user opt-in for analytics
22+ ///
23+ var userHasOptedIn : Bool {
24+ get {
25+ let optedIn : Bool ? = UserDefaults . standard. object ( forKey: . userOptedInAnalytics)
26+ return optedIn ?? true // analytics tracking on by default
27+ }
28+ set {
29+ UserDefaults . standard. set ( newValue, forKey: . userOptedInAnalytics)
30+ }
31+ }
32+
2133
2234 // MARK: - Initialization
2335
@@ -48,6 +60,10 @@ public extension WooAnalytics {
4860 /// It's good to call this function after a user logs in or out of the app.
4961 ///
5062 func refreshUserData( ) {
63+ guard userHasOptedIn == true else {
64+ return
65+ }
66+
5167 analyticsProvider. refreshUserData ( )
5268 }
5369
@@ -56,6 +72,10 @@ public extension WooAnalytics {
5672 /// - Parameter stat: the event name
5773 ///
5874 func track( _ stat: WooAnalyticsStat ) {
75+ guard userHasOptedIn == true else {
76+ return
77+ }
78+
5979 track ( stat, withProperties: nil )
6080 }
6181
@@ -66,6 +86,10 @@ public extension WooAnalytics {
6686 /// - properties: a collection of properties related to the event
6787 ///
6888 func track( _ stat: WooAnalyticsStat , withProperties properties: [ AnyHashable : Any ] ? ) {
89+ guard userHasOptedIn == true else {
90+ return
91+ }
92+
6993 if let updatedProperties = updatePropertiesIfNeeded ( for: stat, properties: properties) {
7094 analyticsProvider. track ( stat. rawValue, withProperties: updatedProperties)
7195 } else {
@@ -80,6 +104,10 @@ public extension WooAnalytics {
80104 /// - error: the error to track
81105 ///
82106 func track( _ stat: WooAnalyticsStat , withError error: Error ) {
107+ guard userHasOptedIn == true else {
108+ return
109+ }
110+
83111 let err = error as NSError
84112 let errorDictionary = [ Constants . errorKeyCode: " \( err. code) " ,
85113 Constants . errorKeyDomain: err. domain,
@@ -90,15 +118,45 @@ public extension WooAnalytics {
90118}
91119
92120
121+ // MARK: - Opt Out
122+ //
123+ extension WooAnalytics {
124+
125+ func setUserHasOptedIn( _ optedIn: Bool ) {
126+ userHasOptedIn = optedIn
127+
128+ if optedIn {
129+ refreshUserData ( )
130+ startObservingNotifications ( )
131+ DDLogInfo ( " 🔵 Tracking started. " )
132+ } else {
133+ stopObservingNotifications ( )
134+ analyticsProvider. clearEvents ( )
135+ analyticsProvider. clearUsers ( )
136+ DDLogInfo ( " 🔴 Tracking opt-out complete. " )
137+ }
138+ }
139+ }
140+
141+
93142// MARK: - Private Helpers
94143//
95144private extension WooAnalytics {
96145
97146 func startObservingNotifications( ) {
147+ guard userHasOptedIn == true else {
148+ return
149+ }
150+
98151 NotificationCenter . default. addObserver ( self , selector: #selector( trackApplicationOpened) , name: UIApplication . didBecomeActiveNotification, object: nil )
99152 NotificationCenter . default. addObserver ( self , selector: #selector( trackApplicationClosed) , name: UIApplication . didEnterBackgroundNotification, object: nil )
100153 }
101154
155+ func stopObservingNotifications( ) {
156+ NotificationCenter . default. removeObserver ( self , name: UIApplication . didBecomeActiveNotification, object: nil )
157+ NotificationCenter . default. removeObserver ( self , name: UIApplication . didEnterBackgroundNotification, object: nil )
158+ }
159+
102160 @objc func trackApplicationOpened( ) {
103161 track ( . applicationOpened)
104162 applicationOpenedTime = Date ( )
0 commit comments