Skip to content

Commit f75bc91

Browse files
authored
DX improvements and more info in logs (#93)
2 parents 9b9db54 + a257fd3 commit f75bc91

File tree

8 files changed

+79
-19
lines changed

8 files changed

+79
-19
lines changed

.editorconfig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Apply to all files
2+
[*]
3+
end_of_line = lf
4+
insert_final_newline = true
5+
trim_trailing_whitespace = true
6+
7+
# Ruby specific rules
8+
[{*.rb,Fastfile,Gemfile}]
9+
indent_style = space
10+
indent_size = 2
11+
12+
[*.{swift,h,m}]
13+
indent_style = space
14+
indent_size = 4

.github/workflows/ios.yml

+1-8
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,4 @@ jobs:
3434
with:
3535
xcode-version: ${{ matrix.xcode }}
3636
- name: Build and Test
37-
run: |
38-
xcodebuild test \
39-
-project Demo/ParselyDemo.xcodeproj \
40-
-scheme ParselyDemo \
41-
-sdk iphonesimulator \
42-
-destination 'platform=iOS Simulator,name=iPhone 14,OS=latest' \
43-
| xcpretty \
44-
&& exit ${PIPESTATUS[0]}
37+
run: make test

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ xcuserdata/*
1111

1212
# Ruby tooling
1313
vendor/bundle
14+
15+
16+
# SwiftLint Remote Config Cache
17+
.swiftlint/RemoteConfigCache

.swiftlint.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
swiftlint_version: 0.57.0
2+
3+
parent_config: https://raw.githubusercontent.com/Automattic/swiftlint-config/b497131f8d0fddbf3b23278cfc4ef8d86c9bcb20/.swiftlint.yml
4+
remote_timeout: 10.0
5+
6+
excluded:
7+
- .build

Demo/ParselyDemo/FirstViewController.swift

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,31 @@ class FirstViewController: UIViewController {
66
let delegate = UIApplication.shared.delegate as! AppDelegate
77

88
@IBAction func didTouchButton(_ sender: Any) {
9-
os_log("didTouchButton", log: OSLog.default, type: .debug)
9+
log("didTouchButton")
1010
let demoMetas = ParselyMetadata(authors: ["Yogi Berr"])
1111
delegate.parsely.trackPageView(url: "http://parsely.com/path/cool-blog-post/1?qsarg=nawp&anotherone=yup", metadata: demoMetas, extraData: ["product-id": "12345"], siteId: "subdomain.parsely-test.com")
1212
}
1313

1414
@IBAction func didStartEngagement(_ sender: Any) {
15-
os_log("didStartEngagement", log: OSLog.default, type: .debug)
15+
log("didStartEngagement")
1616
delegate.parsely.startEngagement(url: "http://parsely.com/very-not-real", urlref:"http://parsely.com/not-real", extraData: ["product-id": "12345"], siteId: "engaged.parsely-test.com")
1717
}
1818

1919
@IBAction func didStopEngagement(_ sender: Any) {
20-
os_log("didStopEngagement", log: OSLog.default, type: .debug)
20+
log("didStopEngagement")
2121
delegate.parsely.stopEngagement()
2222
}
2323
@IBAction func didStartVideo(_ sender: Any) {
24-
os_log("didStartVideo", log: OSLog.default, type: .debug)
24+
log("didStartVideo")
2525
let demoMetas = ParselyMetadata(authors: ["Yogi Berr"], duration: TimeInterval(10))
2626
delegate.parsely.trackPlay(url: "http://parsely.com/path/cool-blog-post/1?qsarg=nawp&anotherone=yup", urlref: "not-a-real-urlref", videoID: "videoOne", duration: TimeInterval(6000), metadata: demoMetas, extraData: ["product-id": "12345", "ts": "should be overwritten"])
2727
}
2828
@IBAction func didPauseVideo(_ sender: Any) {
29-
os_log("didStopVideo", log: OSLog.default, type: .debug)
29+
log("didStopVideo")
3030
delegate.parsely.trackPause()
3131
}
32+
33+
private func log(_ message: String) {
34+
os_log("[Parsely Demo App] %@", log: OSLog.default, type: .debug, message)
35+
}
3236
}

Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# TODO: Use newer Sim. Sticking with it at the moment because we know it works in the existing GitHub action setup.
2+
SIMULATOR_NAME ?= iPhone 14
3+
SIMULATOR_OS ?= latest
4+
XCODE_PATH ?= /Applications/Xcode.app
5+
6+
# Convenience to open the lib and demo project with Xcode, given it's not in the root.
7+
open:
8+
open -a $(XCODE_PATH) ./Demo/ParselyDemo.xcodeproj
9+
10+
# TODO: Move off xcpretty to xcbeautify. Sticking with it at the moment because we know it works in the existing GitHub action setup.
11+
test:
12+
set -o pipefail \
13+
&& xcodebuild test \
14+
-project Demo/ParselyDemo.xcodeproj \
15+
-scheme ParselyDemo \
16+
-sdk iphonesimulator \
17+
-destination 'platform=iOS Simulator,name=$(SIMULATOR_NAME),OS=$(SIMULATOR_OS)' \
18+
| xcpretty
19+
20+
# TODO: Add automation to set up SwiftLint
21+
format:
22+
swiftlint --fix --format

Sources/Sampler.swift

+14-4
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,28 @@ class Sampler {
147147
os_log("No accumulator found for %s, skipping sendHeartbeat", log: OSLog.tracker, type:.debug, key)
148148
return
149149
}
150+
let now = Date()
150151
let incSecs: TimeInterval = trackedData.accumulatedTime
151152
if incSecs > 0 {
152-
os_log("Sending heartbeat for %s", log: OSLog.tracker, type:.debug, key)
153+
os_log(
154+
"Sending heartbeat for %s. Timestamp: %s.",
155+
log: OSLog.tracker,
156+
type:.debug,
157+
key,
158+
now.description
159+
)
153160
heartbeatFn(data: trackedData, enableHeartbeats: true)
154161
}
155162
trackedData.accumulatedTime = 0
156-
let totalTrackedTime: TimeInterval = Date().timeIntervalSince(trackedData.firstSampleTime!);
157-
trackedData.heartbeatTimeout = self.getHeartbeatInterval(
163+
let totalTrackedTime: TimeInterval = now.timeIntervalSince(trackedData.firstSampleTime!)
164+
trackedData.heartbeatTimeout = getHeartbeatInterval(
158165
existingTimeout: trackedData.heartbeatTimeout!,
159-
totalTrackedTime: totalTrackedTime)
166+
totalTrackedTime: totalTrackedTime
167+
)
160168
updateAccumulator(acc: trackedData)
161169
heartbeatInterval = trackedData.heartbeatTimeout!
170+
171+
os_log("Send heartbeat completed. New heartbeat timeout %.2f seconds.", trackedData.heartbeatTimeout!)
162172
}
163173

164174
@objc internal func sendHeartbeats() -> Void {

Sources/Track.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class Track {
2222
parselyTracker.startFlushTimer();
2323

2424
pixel.beacon(event: event)
25-
os_log("Sending an event from Track", log: OSLog.tracker, type:.debug)
26-
dump(event.toDict())
25+
os_log_sending_event(event)
2726
}
2827

2928
func pageview(url: String, urlref: String = "", metadata: ParselyMetadata?, extra_data: Dictionary<String, Any>?, idsite: String) {
@@ -82,3 +81,10 @@ class Track {
8281
videoManager.sendHeartbeats()
8382
}
8483
}
84+
85+
/// Utility to log sending event with a dump of the event.
86+
private func os_log_sending_event(_ event: Event, log: OSLog = .tracker, type: OSLogType = .debug) {
87+
var eventDump = ""
88+
dump(event.toDict(), to: &eventDump)
89+
os_log("Sending an event from Track:\n%@", log: log, type: type, eventDump)
90+
}

0 commit comments

Comments
 (0)