Skip to content

Commit

Permalink
Merge pull request #3 from simpleanalytics/feature/smallchanges
Browse files Browse the repository at this point in the history
Adds isOptedOut storage in UserDefaults
  • Loading branch information
roelvanderkraan authored Nov 8, 2023
2 parents be8115b + f39bb07 commit f21177f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
1 change: 0 additions & 1 deletion Sources/SimpleAnalytics/RequestDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ internal struct RequestDispatcher {
parameters: event,
encoder: JSONParameterEncoder.default).responseData { response in

debugPrint(response)
if let httpStatusCode = response.response?.statusCode {
switch httpStatusCode {
case 201:
Expand Down
28 changes: 20 additions & 8 deletions Sources/SimpleAnalytics/SimpleAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ import Foundation
/// static let shared: SimpleAnalytics = SimpleAnalytics(hostname: "mobileapp.yourdomain.com")
/// }
/// ````
public class SimpleAnalytics: NSObject {
final public class SimpleAnalytics: NSObject {
/// The hostname of the website in Simple Analytics the tracking should be send to. Without `https://`
private let hostname: String
private let userAgent: String
private let userLanguage: String
private let userTimezone: String
/// The last date a unique visit was tracked.
private var visitDate: Date?
/// Key used to store the visit date in UserDefaults
private var visitDateKey = "simpleanalytics.visitdate"

/// Defines if the user is opted out. When set to `true`, all tracking will be skipped. This is not persisted between sessions.
public var isOptedOut: Bool = false
/// Defines if the user is opted out. When set to `true`, all tracking will be skipped. This is persisted between sessions.
public var isOptedOut: Bool {
get {
UserDefaults.standard.bool(forKey: Keys.optedOutKey)
}
set {
UserDefaults.standard.setValue(newValue, forKey: Keys.optedOutKey)
}
}

/// Create the SimpleAnalytics instance that can be used to trigger events and pageviews.
/// - Parameter hostname: The hostname as found in SimpleAnalytics, without `https://`
Expand All @@ -43,7 +48,7 @@ public class SimpleAnalytics: NSObject {
self.userAgent = UserAgent.userAgentString()
self.userLanguage = Locale.current.identifier
self.userTimezone = TimeZone.current.identifier
self.visitDate = UserDefaults.standard.object(forKey: visitDateKey) as? Date
self.visitDate = UserDefaults.standard.object(forKey: Keys.visitDateKey) as? Date
}

/// Track a pageview
Expand Down Expand Up @@ -118,13 +123,20 @@ public class SimpleAnalytics: NSObject {
} else {
// Last visit is not in today, so unique.
self.visitDate = Date()
UserDefaults.standard.set(visitDate, forKey: visitDateKey)
UserDefaults.standard.set(visitDate, forKey: Keys.visitDateKey)
return true
}
} else {
// No visit date yet, initialize it
visitDate = Date()
UserDefaults.standard.set(visitDate, forKey: visitDateKey)
UserDefaults.standard.set(visitDate, forKey: Keys.visitDateKey)
return true
}
}

/// Keys used to store things in UserDefaults
internal struct Keys {
static let visitDateKey = "simpleanalytics.visitdate"
static let optedOutKey = "simpleanalytics.isoptedout"
}
}

0 comments on commit f21177f

Please sign in to comment.