Skip to content

Commit

Permalink
DX improvements and more info in logs (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio authored Jan 8, 2025
2 parents 9b9db54 + a257fd3 commit f75bc91
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 19 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Apply to all files
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

# Ruby specific rules
[{*.rb,Fastfile,Gemfile}]
indent_style = space
indent_size = 2

[*.{swift,h,m}]
indent_style = space
indent_size = 4
9 changes: 1 addition & 8 deletions .github/workflows/ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,4 @@ jobs:
with:
xcode-version: ${{ matrix.xcode }}
- name: Build and Test
run: |
xcodebuild test \
-project Demo/ParselyDemo.xcodeproj \
-scheme ParselyDemo \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 14,OS=latest' \
| xcpretty \
&& exit ${PIPESTATUS[0]}
run: make test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ xcuserdata/*

# Ruby tooling
vendor/bundle


# SwiftLint Remote Config Cache
.swiftlint/RemoteConfigCache
7 changes: 7 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
swiftlint_version: 0.57.0

parent_config: https://raw.githubusercontent.com/Automattic/swiftlint-config/b497131f8d0fddbf3b23278cfc4ef8d86c9bcb20/.swiftlint.yml
remote_timeout: 10.0

excluded:
- .build
14 changes: 9 additions & 5 deletions Demo/ParselyDemo/FirstViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@ class FirstViewController: UIViewController {
let delegate = UIApplication.shared.delegate as! AppDelegate

@IBAction func didTouchButton(_ sender: Any) {
os_log("didTouchButton", log: OSLog.default, type: .debug)
log("didTouchButton")
let demoMetas = ParselyMetadata(authors: ["Yogi Berr"])
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")
}

@IBAction func didStartEngagement(_ sender: Any) {
os_log("didStartEngagement", log: OSLog.default, type: .debug)
log("didStartEngagement")
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")
}

@IBAction func didStopEngagement(_ sender: Any) {
os_log("didStopEngagement", log: OSLog.default, type: .debug)
log("didStopEngagement")
delegate.parsely.stopEngagement()
}
@IBAction func didStartVideo(_ sender: Any) {
os_log("didStartVideo", log: OSLog.default, type: .debug)
log("didStartVideo")
let demoMetas = ParselyMetadata(authors: ["Yogi Berr"], duration: TimeInterval(10))
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"])
}
@IBAction func didPauseVideo(_ sender: Any) {
os_log("didStopVideo", log: OSLog.default, type: .debug)
log("didStopVideo")
delegate.parsely.trackPause()
}

private func log(_ message: String) {
os_log("[Parsely Demo App] %@", log: OSLog.default, type: .debug, message)
}
}
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# TODO: Use newer Sim. Sticking with it at the moment because we know it works in the existing GitHub action setup.
SIMULATOR_NAME ?= iPhone 14
SIMULATOR_OS ?= latest
XCODE_PATH ?= /Applications/Xcode.app

# Convenience to open the lib and demo project with Xcode, given it's not in the root.
open:
open -a $(XCODE_PATH) ./Demo/ParselyDemo.xcodeproj

# TODO: Move off xcpretty to xcbeautify. Sticking with it at the moment because we know it works in the existing GitHub action setup.
test:
set -o pipefail \
&& xcodebuild test \
-project Demo/ParselyDemo.xcodeproj \
-scheme ParselyDemo \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=$(SIMULATOR_NAME),OS=$(SIMULATOR_OS)' \
| xcpretty

# TODO: Add automation to set up SwiftLint
format:
swiftlint --fix --format
18 changes: 14 additions & 4 deletions Sources/Sampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,28 @@ class Sampler {
os_log("No accumulator found for %s, skipping sendHeartbeat", log: OSLog.tracker, type:.debug, key)
return
}
let now = Date()
let incSecs: TimeInterval = trackedData.accumulatedTime
if incSecs > 0 {
os_log("Sending heartbeat for %s", log: OSLog.tracker, type:.debug, key)
os_log(
"Sending heartbeat for %s. Timestamp: %s.",
log: OSLog.tracker,
type:.debug,
key,
now.description
)
heartbeatFn(data: trackedData, enableHeartbeats: true)
}
trackedData.accumulatedTime = 0
let totalTrackedTime: TimeInterval = Date().timeIntervalSince(trackedData.firstSampleTime!);
trackedData.heartbeatTimeout = self.getHeartbeatInterval(
let totalTrackedTime: TimeInterval = now.timeIntervalSince(trackedData.firstSampleTime!)
trackedData.heartbeatTimeout = getHeartbeatInterval(
existingTimeout: trackedData.heartbeatTimeout!,
totalTrackedTime: totalTrackedTime)
totalTrackedTime: totalTrackedTime
)
updateAccumulator(acc: trackedData)
heartbeatInterval = trackedData.heartbeatTimeout!

os_log("Send heartbeat completed. New heartbeat timeout %.2f seconds.", trackedData.heartbeatTimeout!)
}

@objc internal func sendHeartbeats() -> Void {
Expand Down
10 changes: 8 additions & 2 deletions Sources/Track.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class Track {
parselyTracker.startFlushTimer();

pixel.beacon(event: event)
os_log("Sending an event from Track", log: OSLog.tracker, type:.debug)
dump(event.toDict())
os_log_sending_event(event)
}

func pageview(url: String, urlref: String = "", metadata: ParselyMetadata?, extra_data: Dictionary<String, Any>?, idsite: String) {
Expand Down Expand Up @@ -82,3 +81,10 @@ class Track {
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)
}

0 comments on commit f75bc91

Please sign in to comment.