-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTrack.swift
90 lines (75 loc) · 3.18 KB
/
Track.swift
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
import Foundation
import os.log
class Track {
let pixel: Pixel
let videoManager: VideoManager
let engagedTime: EngagedTime
private let parselyTracker: Parsely
init(trackerInstance: Parsely) {
parselyTracker = trackerInstance
pixel = Pixel(trackerInstance: parselyTracker)
videoManager = VideoManager(trackerInstance: parselyTracker)
engagedTime = EngagedTime(trackerInstance: parselyTracker)
}
func event(event: Event) {
if event.idsite.isEmpty {
os_log("idsite not specified. Use ParselyTracker.configure or specify it as an argument to tracking functions.", log: OSLog.tracker, type:.error)
return
}
parselyTracker.startFlushTimer();
pixel.beacon(event: event)
os_log_sending_event(event)
}
func pageview(url: String, urlref: String = "", metadata: ParselyMetadata?, extra_data: Dictionary<String, Any>?, idsite: String) {
let event_ = Event(
"pageview",
url: url,
urlref: urlref,
metadata: metadata,
extra_data: extra_data,
idsite: idsite
)
os_log("Sending a pageview from Track", log: OSLog.tracker, type:.debug)
event(event: event_)
}
func videoStart(url: String, urlref: String, vId: String, duration: TimeInterval, metadata: ParselyMetadata?, extra_data: Dictionary<String, Any>?, idsite: String) {
videoManager.trackPlay(url: url, urlref: urlref, vId: vId, duration: duration, metadata: metadata, extra_data: extra_data, idsite: idsite)
os_log("Tracked videoStart from Track", log: OSLog.tracker, type:.debug)
}
func videoPause() {
videoManager.trackPause()
os_log("Tracked videoPause from Track", log: OSLog.tracker, type:.debug)
}
func videoReset(url:String, vId:String) {
videoManager.reset(url:url, vId:vId)
}
func startEngagement(url: String, urlref: String = "", extra_data: Dictionary<String, Any>?, idsite: String) {
engagedTime.startInteraction(url: url, urlref: urlref, extra_data: extra_data, idsite: idsite)
os_log("track start engagement from Track", log: OSLog.tracker, type:.debug)
}
func stopEngagement() {
engagedTime.endInteraction()
os_log("track stop engagement from Track", log: OSLog.tracker, type:.debug)
}
internal func pause() {
os_log("Pausing from track", log:OSLog.tracker, type:.debug)
engagedTime.pause()
videoManager.pause()
}
internal func resume() {
os_log("Resuming from track", log:OSLog.tracker, type:.debug)
engagedTime.resume()
videoManager.resume()
}
internal func sendHeartbeats() {
os_log("Sending heartbeats from track", log:OSLog.tracker, type:.debug)
engagedTime.sendHeartbeats()
videoManager.sendHeartbeats()
}
}
/// Utility to log sending event with a dump of the event.
private func os_log_sending_event(_ event: Event, log: OSLog = .tracker, type: OSLogType = .debug) {
var eventDump = ""
dump(event.toDict(), to: &eventDump)
os_log("Sending an event from Track:\n%@", log: log, type: type, eventDump)
}