-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMixpanelService.swift
More file actions
108 lines (79 loc) · 2.92 KB
/
MixpanelService.swift
File metadata and controls
108 lines (79 loc) · 2.92 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
//
// MixpanelService.swift
// MixpanelServiceKit
//
// Created by Cameron Ingham on 5/29/23.
//
import Mixpanel
import LoopKit
public final class MixpanelService: Service {
public static let pluginIdentifier = "MixpanelService"
public static let localizedTitle = LocalizedString("Mixpanel", comment: "The title of the Mixpanel service")
public weak var serviceDelegate: ServiceDelegate?
public weak var stateDelegate: StatefulPluggableDelegate?
public var token: String?
private var client: MixpanelInstance?
public init() {}
public init?(rawState: RawStateValue) {
self.token = try? KeychainManager().getMixpanelToken()
createClient()
}
public var rawState: RawStateValue {
return [:]
}
public let isOnboarded = true // No distinction between created and onboarded
public var hasConfiguration: Bool { return token?.isEmpty == false }
public func completeCreate() {
try! KeychainManager().setMixpanelToken(token)
createClient()
}
public func completeUpdate() {
try! KeychainManager().setMixpanelToken(token)
createClient()
stateDelegate?.pluginDidUpdateState(self)
}
public func completeDelete() {
try! KeychainManager().setMixpanelToken()
stateDelegate?.pluginWantsDeletion(self)
}
private func createClient() {
if let token = token {
let mixpanel = Mixpanel.initialize(token: token, trackAutomaticEvents: true)
client = mixpanel
} else {
client = nil
}
}
}
extension MixpanelService: AnalyticsService {
public func recordAnalyticsEvent(_ name: String, withProperties properties: [AnyHashable: Any]?, outOfSession: Bool) {
client?.track(event: name, properties: mappedProperties(from: properties))
}
public func recordIdentify(_ property: String, value: String) {
client?.people.set(property: property, to: value)
}
public func recordIdentify(_ property: String, array: [String]) {
client?.people.set(property: property, to: array)
}
}
extension KeychainManager {
func setMixpanelToken(_ mixpanelToken: String? = nil) throws {
try replaceGenericPassword(mixpanelToken, forService: MixpanelTokenService)
}
func getMixpanelToken() throws -> String {
return try getGenericPasswordForService(MixpanelTokenService)
}
}
fileprivate let MixpanelTokenService = "MixpanelToken"
private extension MixpanelService {
func mappedProperties(from properties: [AnyHashable: Any]?) -> [String: MixpanelType] {
guard let properties else { return [:] }
var mappedProperties: [String: MixpanelType] = [:]
for (key, value) in properties {
if let key = key as? String, let value = value as? MixpanelType {
mappedProperties[key] = value
}
}
return mappedProperties
}
}