-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAnalyticsLogger.swift
More file actions
112 lines (89 loc) · 3.91 KB
/
Copy pathAnalyticsLogger.swift
File metadata and controls
112 lines (89 loc) · 3.91 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
109
110
111
112
import Foundation
import UIKit
class Weak<T: AnyObject> {
weak var value: T?
init(_ value: T?) {
self.value = value
}
}
class AnalyticsLogger: Encodable {
// Global Details
static var integrationVersion: String?
static var integrationName: String?
var component: Component
var instanceId: String
// Language returned by the server in the message response
var languageRendered: String?
// Includes things like fdata, experience IDs, debug IDs, and the like
var dynamicData: [String: AnyCodable] = [:]
// Events tied to the component
var events: [AnalyticsEvent] = []
enum Component {
case message(Weak<PayPalMessageView>)
case modal(Weak<PayPalMessageModal>)
}
init(_ component: Component) {
self.instanceId = UUID().uuidString
self.component = component
AnalyticsService.shared.addLogger(self)
}
deinit {}
enum StaticKey: String, CodingKey {
// Integration Details
case offerType = "offer_type"
case amount = "amount"
case pageType = "page_type"
case buyerCountryCode = "buyer_country_code"
case channel = "presentment_channel"
case languageRequested = "language_requested"
case languageRendered = "language_rendered"
// Message Only
case styleLogoType = "style_logo_type"
case styleColor = "style_color"
case styleTextAlign = "style_text_align"
// Other Details
case type = "type"
case instanceId = "instance_id"
// Component Events
case events = "component_events"
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StaticKey.self)
try container.encode(instanceId, forKey: .instanceId)
try container.encode(events, forKey: .events)
try dynamicData.encode(to: encoder)
switch component {
case .message(let weakMessage):
guard let message = weakMessage.value else { return }
try container.encode("message", forKey: .type)
try container.encodeIfPresent(message.offerType?.rawValue, forKey: .offerType)
try container.encodeIfPresent(message.amount?.description, forKey: .amount)
try container.encodeIfPresent(message.pageType?.rawValue, forKey: .pageType)
try container.encodeIfPresent(message.buyerCountry, forKey: .buyerCountryCode)
try container.encodeIfPresent(message.channel, forKey: .channel)
try container.encodeIfPresent(message.logoType.rawValue, forKey: .styleLogoType)
try container.encodeIfPresent(message.color.rawValue, forKey: .styleColor)
try container.encodeIfPresent(message.textAlign.rawValue, forKey: .styleTextAlign)
// Language requested by the merchant via locale/language config
let languageRequested = message.locale?.replacingOccurrences(of: "_", with: "-")
?? message.language
?? "undefined"
try container.encodeIfPresent(languageRequested, forKey: .languageRequested)
try container.encodeIfPresent(languageRendered ?? "undefined", forKey: .languageRendered)
case .modal(let weakModal):
guard let modal = weakModal.value else { return }
try container.encode("modal", forKey: .type)
try container.encodeIfPresent(modal.offerType?.rawValue, forKey: .offerType)
try container.encodeIfPresent(modal.amount?.description, forKey: .amount)
try container.encodeIfPresent(modal.pageType?.rawValue, forKey: .pageType)
try container.encodeIfPresent(modal.buyerCountry, forKey: .buyerCountryCode)
try container.encodeIfPresent(modal.channel, forKey: .channel)
}
}
func addEvent(_ event: AnalyticsEvent) {
self.events.append(event)
}
func clearEvents() {
events.removeAll()
}
}