-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockAnalyticsEngine.swift
More file actions
46 lines (39 loc) · 1.5 KB
/
MockAnalyticsEngine.swift
File metadata and controls
46 lines (39 loc) · 1.5 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
//
// MockAnalyticsEngine.swift
// CTAnalyticsTests
//
// Created by Mark Pospesel on 7/20/21.
//
import Foundation
/// Mock analytics engine. Stores events to an `allEvents` array each time
/// `track(event:)` is called. It also stores information by event type.
///
/// Use this only for unit testing. Consider whether `SpyAnalyticsEngine` might be a better fit.
final public class MockAnalyticsEngine {
/// Array of all events tracked
public var allEvents: [AnalyticsEvent] = []
/// Ordered array of screen names tracked in `.screenView` events
public private(set) var screenViews: [String] = []
/// Dictionary of user properties tracked in `.userProperty` events
public private(set) var userProperties: [String: String] = [:]
/// Dictionary of event metadata tracked in `.event` events
public private(set) var events: [String: Metadata?] = [:]
/// Initialize mock analytics engine
public init() { }
}
// MARK: - AnalyticsEngine
extension MockAnalyticsEngine: AnalyticsEngine {
/// Log an analytics event
/// - Parameter event: the event to log
public func track(event: AnalyticsEvent) {
allEvents.append(event)
switch event {
case .screenView(screenName: let screenName):
screenViews.append(screenName)
case .userProperty(name: let name, value: let value):
userProperties[name] = value
case .event(name: let name, parameters: let parameters):
events[name] = parameters
}
}
}