• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperwallet / hyperwallet-ios-ui-sdk / 4735069856

pending completion
4735069856

push

github

Gustavo Meyer
DTPOMERSER-84 - Address swift lint

2255 of 2353 relevant lines covered (95.84%)

17.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.56
/Common/Sources/Insights/HyperwalletInsights.swift
1
//
2
// Copyright 2018 - Present Hyperwallet
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5
// and associated documentation files (the "Software"), to deal in the Software without restriction,
6
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
7
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in all copies or
11
// substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18

19
import Foundation
20
import HyperwalletSDK
21
import Insights
22

23
/// Protocol for HyperwalletInsights
24
public protocol HyperwalletInsightsProtocol: AnyObject {
25
    /// Track Clicks
26
    ///
27
    /// - Parameters:
28
    ///   - pageName: Name of the page
29
    ///   - pageGroup: Page group name
30
    ///   - link: The link clicked - example : select-transfer-method
31
    ///   - params: A list of other information to be tracked - example : country,currency
32
    func trackClick(pageName: String, pageGroup: String, link: String, params: [String: String])
33

34
    /// Track Impressions
35
    ///
36
    /// - Parameters:
37
    ///   - pageName: Name of the page - example : transfer-method:add:select-transfer-method
38
    ///   - pageGroup: Page group name - example : transfer-method
39
    ///   - params: A list of other information to be tracked - example : country,currency
40
    func trackImpression(pageName: String, pageGroup: String, params: [String: String])
41

42
    /// Track Error
43
    ///
44
    /// - Parameters:
45
    ///   - pageName: Name of the page - example : transfer-method:add:select-transfer-method
46
    ///   - pageGroup: Page group name - example : transfer-method
47
    ///   - errorInfo: The ErrorInfo structure is used to describe an occurred error
48
    func trackError(pageName: String, pageGroup: String, errorInfo: ErrorInfo)
49
}
50
/// Class responsible for initializing the Insights module.
51
/// It contains methods to call Insights for various actions performed by the user
52
public class HyperwalletInsights: HyperwalletInsightsProtocol {
53
    private static var instance: HyperwalletInsights?
54
    private var configuration: Configuration?
55
    var insights: InsightsProtocol?
56

57
    /// Returns the previously initialized instance of the HyperwalletInsights interface object
58
    public static var shared: HyperwalletInsights {
23✔
59
        return instance ?? HyperwalletInsights()
23✔
60
    }
23✔
61

62
    private init() {
5✔
63
        loadConfigurationAndInitializeInsights(completion: { _ in })
5✔
64
    }
5✔
65
    
66
    private init(_ configuration: Configuration) {
×
67
        self.configuration = configuration
×
68
        initializeInsights(configuration: configuration)
×
69
    }
×
70

71
    /// Clears Insights SDK instance.
72
    public static func clearInstance() {
3✔
73
        Insights.clearInstance()
3✔
74
        instance = nil
3✔
75
    }
3✔
76

77
    /// Set up HyperwalletInsights
78
    public static func setup() {
12✔
79
        if instance == nil {
12✔
80
            instance = HyperwalletInsights()
4✔
81
        }
12✔
82
    }
12✔
83
    
84
    static func setup(_ configuration: Configuration) {
×
85
        instance = HyperwalletInsights(configuration)
×
86
    }
×
87

88
    /// Track Clicks
89
    ///
90
    /// - Parameters:
91
    ///   - pageName: Name of the page
92
    ///   - pageGroup: Page group name
93
    ///   - link: The link clicked - example : select-transfer-method
94
    ///   - params: A list of other information to be tracked - example : country,currency
95
    public func trackClick(pageName: String, pageGroup: String, link: String, params: [String: String]) {
2✔
96
        DispatchQueue.global().async { [weak self] in
2✔
97
            if let insights = self?.insights {
2✔
98
                insights.trackClick(pageName: pageName, pageGroup: pageGroup, link: link, params: params)
1✔
99
            } else {
2✔
100
                self?.loadConfigurationAndInitializeInsights { isInsightsInitialized in
1✔
101
                    if isInsightsInitialized {
1✔
102
                        Insights.shared?.trackClick(pageName: pageName,
1✔
103
                                                    pageGroup: pageGroup,
1✔
104
                                                    link: link,
1✔
105
                                                    params: params)
1✔
106
                    }
1✔
107
                }
1✔
108
            }
2✔
109
        }
2✔
110
    }
2✔
111

112
    /// Track Error
113
    ///
114
    /// - Parameters:
115
    ///   - pageName: Name of the page - example : transfer-method:add:select-transfer-method
116
    ///   - pageGroup: Page group name - example : transfer-method
117
    ///   - ErrorInfo:  ErrorInfo have the information about the error
118
    public func trackError(pageName: String, pageGroup: String, errorInfo: ErrorInfo) {
3✔
119
        DispatchQueue.global().async { [weak self] in
3✔
120
            if let insights = self?.insights {
3✔
121
                insights.trackError(pageName: pageName, pageGroup: pageGroup, errorInfo: errorInfo)
1✔
122
            } else {
3✔
123
                self?.loadConfigurationAndInitializeInsights { isInsightsInitialized in
2✔
124
                    if isInsightsInitialized {
2✔
125
                        Insights.shared?.trackError(pageName: pageName, pageGroup: pageGroup, errorInfo: errorInfo)
1✔
126
                    }
2✔
127
                }
2✔
128
            }
3✔
129
        }
3✔
130
    }
3✔
131

132
    /// Track Impressions
133
    ///
134
    /// - Parameters:
135
    ///   - pageName: Name of the page - example : transfer-method:add:select-transfer-method
136
    ///   - pageGroup: Page group name - example : transfer-method
137
    ///   - params: A list of other information to be tracked - example : country,currency
138
    public func trackImpression(pageName: String, pageGroup: String, params: [String: String]) {
2✔
139
        DispatchQueue.global().async { [weak self] in
2✔
140
            if let insights = self?.insights {
2✔
141
                insights.trackImpression(pageName: pageName, pageGroup: pageGroup, params: params)
1✔
142
            } else {
2✔
143
                self?.loadConfigurationAndInitializeInsights { isInsightsInitialized in
1✔
144
                    if isInsightsInitialized {
1✔
145
                        Insights.shared?.trackImpression(pageName: pageName, pageGroup: pageGroup, params: params)
1✔
146
                    }
1✔
147
                }
1✔
148
            }
2✔
149
        }
2✔
150
    }
2✔
151

152
    private func loadConfigurationAndInitializeInsights(completion: @escaping(Bool) -> Void) {
9✔
153
        loadConfiguration { configuration in
9✔
154
            if let configuration = configuration {
9✔
155
                self.initializeInsights(configuration: configuration)
7✔
156
                completion(true)
7✔
157
            } else {
9✔
158
                completion(false)
2✔
159
            }
9✔
160
        }
9✔
161
    }
9✔
162

163
    private func loadConfiguration(completion: @escaping(Configuration?) -> Void) {
9✔
164
        if configuration != nil {
9✔
165
            completion(configuration)
×
166
            return
×
167
        }
9✔
168
        
9✔
169
        // Fetch configuration again
9✔
170
        Hyperwallet.shared.getConfiguration { configuration, _ in
9✔
171
            if let configuration = configuration {
9✔
172
                completion(configuration)
7✔
173
            } else {
9✔
174
                completion(nil)
2✔
175
            }
9✔
176
        }
9✔
177
    }
9✔
178

179
    /// Initialize the Insights module if the url and environment variables are available
180
    private func initializeInsights(configuration: Configuration) {
7✔
181
        if let environment = configuration.environment,
7✔
182
            let insightsUrl = configuration.insightsUrl,
7✔
183
            let sdkVersion = HyperwalletBundle.currentSDKAppVersion {
7✔
184
            Insights.setup(environment: environment,
7✔
185
                           programToken: configuration.issuer,
7✔
186
                           sdkVersion: sdkVersion,
7✔
187
                           apiUrl: insightsUrl,
7✔
188
                           userToken: configuration.userToken)
7✔
189
            insights = Insights.shared
7✔
190
        }
7✔
191
    }
7✔
192
}
193

194
/// A helper class to build the `ErrorInfo` instance.
195
public class ErrorInfoBuilder {
196
    private let description = Thread.callStackSymbols.joined(separator: "\n")
6✔
197
    private let message: String
198
    private let type: String
199
    private var code = ""
200
    private var fieldName = ""
201

202
    /// Initializes ErrorInfoBuilder
203
    ///
204
    /// - Parameters:
205
    ///   - type: The Type of error that occurred.
206
    ///   - message: The Field Name is especially interesting when there is a validation error/issue in combination
207
    ///     with error_type = FORM
208
    public init(type: String, message: String) {
6✔
209
        self.type = type
6✔
210
        self.message = message
6✔
211
    }
6✔
212

213
    /// Sets FieldName
214
    ///
215
    /// - Parameter fieldName: The Field Name is especially interesting when there is a validation
216
    ///     error/issue in combination with error_type = FORM or when an API error occurs in relation
217
    ///     to a field, error_type = API
218
    /// - Returns: ErrorInfoBuilder
219
    public func fieldName(_ fieldName: String) -> ErrorInfoBuilder {
6✔
220
        self.fieldName = fieldName
6✔
221
        return self
6✔
222
    }
6✔
223

224
    /// Sets Code
225
    ///
226
    /// - Parameter code: The Error Code is the type of error that occurred
227
    /// - Returns: ErrorInfoBuilder
228
    public func code(_ code: String) -> ErrorInfoBuilder {
5✔
229
        self.code = code
5✔
230
        return self
5✔
231
    }
5✔
232

233
    /// Builds a new instance of the `ErrorInfo`.
234
    ///
235
    /// - Returns: a new instance of the `ErrorInfo`.
236
    public func build() -> ErrorInfo {
6✔
237
        return ErrorInfo(type: type,
6✔
238
                         message: message,
6✔
239
                         fieldName: fieldName,
6✔
240
                         description: description,
6✔
241
                         code: code)
6✔
242
    }
6✔
243
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc