forked from tidepool-org/LoopSupport
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLoopKitAnalytics.swift
More file actions
109 lines (89 loc) · 3.19 KB
/
LoopKitAnalytics.swift
File metadata and controls
109 lines (89 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// LoopKitAnalytics.swift
// LoopSupportKitUI
//
// Created by Pete Schwamb on 12/29/22.
//
import Amplitude
import LoopKit
public enum UsageDataPrivacyPreference: String {
case noSharing
case shareInstallationStatsOnly
case shareUsageDetailsWithDevelopers
}
public class LoopKitAnalytics {
static public let shared = LoopKitAnalytics()
private var client: Amplitude?
public private(set) var usageDataPrivacyPreference: UsageDataPrivacyPreference? {
get {
return UserDefaults.standard.usageDataPrivacyPreference
}
set {
let oldValue = usageDataPrivacyPreference
guard newValue != oldValue else {
return
}
UserDefaults.standard.usageDataPrivacyPreference = newValue
if newValue == .noSharing {
client = nil
} else if oldValue == .noSharing {
createClient()
}
}
}
public func updateUsageDataPrivacyPreference(newValue: UsageDataPrivacyPreference) {
usageDataPrivacyPreference = newValue
}
public init() {
createClient()
}
private func createClient() {
#if targetEnvironment(simulator)
return
#else
guard let usageDataPrivacyPreference = usageDataPrivacyPreference, usageDataPrivacyPreference != .noSharing else {
return
}
let amplitude = Amplitude()
amplitude.setTrackingOptions(AMPTrackingOptions().disableCity().disableCarrier().disableIDFA().disableLatLng().disableIPAddress().disableRegion())
amplitude.initializeApiKey("7dd7414785560c0dd1ef802ac10c00b4")
client = amplitude
client?.logEvent("LoopKitAnalytics instantiation")
#endif
}
public func recordAnalyticsEvent(_ name: String, withProperties properties: [AnyHashable : Any]?, outOfSession: Bool) {
if usageDataPrivacyPreference == .shareUsageDetailsWithDevelopers {
client?.logEvent(name, withEventProperties: properties, outOfSession: outOfSession)
}
}
public func recordIdentify(_ property: String, value: String) {
if usageDataPrivacyPreference == .shareUsageDetailsWithDevelopers {
client?.identify(AMPIdentify().set(property, value: value as NSString))
}
}
public func recordIdentify(_ property: String, array: [String]) {
if usageDataPrivacyPreference == .shareUsageDetailsWithDevelopers {
client?.identify(AMPIdentify().set(property, value: array as NSArray))
}
}
}
extension UserDefaults {
private enum Key: String {
case UsageDataPrivacyPreference = "com.loopkit.Loop.UsageDataPrivacyPreference"
}
// Information for the extension from Loop
var usageDataPrivacyPreference: UsageDataPrivacyPreference? {
get {
if let rawValue = string(forKey: Key.UsageDataPrivacyPreference.rawValue),
let preference = UsageDataPrivacyPreference(rawValue: rawValue)
{
return preference
} else {
return nil
}
}
set {
set(newValue?.rawValue, forKey: Key.UsageDataPrivacyPreference.rawValue)
}
}
}