-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Outline all new signals & parameters & sprinkle TODOs to finalize
- Loading branch information
Showing
9 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#if canImport(UIKit) | ||
import UIKit | ||
#elseif canImport(AppKit) | ||
import AppKit | ||
#endif | ||
|
||
@MainActor | ||
final class DurationSignalTracker { | ||
static let shared = DurationSignalTracker() | ||
|
||
private struct CachedData { | ||
let startTime: Date | ||
let parameters: [String: String] | ||
} | ||
|
||
private var startedSignals: [String: CachedData] = [:] | ||
private var lastEnteredBackground: Date? | ||
|
||
private init() { | ||
self.setupAppLifecycleObservers() | ||
} | ||
|
||
func startTracking(_ signalName: String, parameters: [String: String]) { | ||
self.startedSignals[signalName] = CachedData(startTime: Date(), parameters: parameters) | ||
} | ||
|
||
func stopTracking(_ signalName: String) -> (duration: TimeInterval, parameters: [String: String])? { | ||
guard let trackingData = self.startedSignals[signalName] else { return nil } | ||
self.startedSignals[signalName] = nil | ||
|
||
let duration = Date().timeIntervalSince(trackingData.startTime) | ||
return (duration, trackingData.parameters) | ||
} | ||
|
||
private func setupAppLifecycleObservers() { | ||
#if canImport(UIKit) | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(handleDidEnterBackgroundNotification), | ||
name: UIApplication.didEnterBackgroundNotification, | ||
object: nil | ||
) | ||
|
||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(handleWillEnterForegroundNotification), | ||
name: UIApplication.willEnterForegroundNotification, | ||
object: nil | ||
) | ||
#elseif canImport(AppKit) | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(handleDidEnterBackgroundNotification), | ||
name: NSApplication.didResignActiveNotification, | ||
object: nil | ||
) | ||
|
||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(handleWillEnterForegroundNotification), | ||
name: NSApplication.willBecomeActiveNotification, | ||
object: nil | ||
) | ||
#endif | ||
} | ||
|
||
@objc | ||
private func handleDidEnterBackgroundNotification() { | ||
self.lastEnteredBackground = Date() | ||
} | ||
|
||
@objc | ||
private func handleWillEnterForegroundNotification() { | ||
guard let lastEnteredBackground else { return } | ||
let backgroundDuration = Date().timeIntervalSince(lastEnteredBackground) | ||
|
||
for (signalName, data) in self.startedSignals { | ||
self.startedSignals[signalName] = CachedData( | ||
startTime: data.startTime.addingTimeInterval(backgroundDuration), | ||
parameters: data.parameters | ||
) | ||
} | ||
|
||
self.lastEnteredBackground = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
|
||
@MainActor | ||
final class SessionManager { | ||
static let shared = SessionManager() | ||
private init() {} | ||
|
||
// TODO: make sure that all session start dates and their duration are persisted (use a Codable?) | ||
// TODO: implement auto-detection of new install and send `newInstallDetected` with `firstSessionDate` | ||
} |
47 changes: 47 additions & 0 deletions
47
Sources/TelemetryDeck/PirateMetrics/TelemetryDeck+Acquisition.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Foundation | ||
|
||
public extension TelemetryDeck { | ||
static func acquiredUser( | ||
channel: String, | ||
parameters: [String: String] = [:], | ||
customUserID: String? = nil | ||
) { | ||
let acquisitionParameters = ["TelemetryDeck.Acquisition.channel": channel] | ||
|
||
// TODO: persist channel and send with every request | ||
|
||
self.internalSignal( | ||
"TelemetryDeck.Acquisition.userAcquired", | ||
parameters: acquisitionParameters.merging(parameters) { $1 }, | ||
customUserID: customUserID | ||
) | ||
} | ||
|
||
static func leadStarted( | ||
leadID: String, | ||
parameters: [String: String] = [:], | ||
customUserID: String? = nil | ||
) { | ||
let leadParameters: [String: String] = ["TelemetryDeck.Acquisition.leadID": leadID] | ||
|
||
self.internalSignal( | ||
"TelemetryDeck.Acquisition.leadStarted", | ||
parameters: leadParameters.merging(parameters) { $1 }, | ||
customUserID: customUserID | ||
) | ||
} | ||
|
||
static func leadConverted( | ||
leadID: String, | ||
parameters: [String: String] = [:], | ||
customUserID: String? = nil | ||
) { | ||
let leadParameters: [String: String] = ["TelemetryDeck.Acquisition.leadID": leadID] | ||
|
||
self.internalSignal( | ||
"TelemetryDeck.Acquisition.leadConverted", | ||
parameters: leadParameters.merging(parameters) { $1 }, | ||
customUserID: customUserID | ||
) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Sources/TelemetryDeck/PirateMetrics/TelemetryDeck+Activation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Foundation | ||
|
||
extension TelemetryDeck { | ||
static func onboardingCompleted( | ||
parameters: [String: String] = [:], | ||
customUserID: String? = nil | ||
) { | ||
let onboardingParameters: [String: String] = [:] | ||
|
||
self.internalSignal( | ||
"TelemetryDeck.Activation.onboardingCompleted", | ||
parameters: onboardingParameters.merging(parameters) { $1 }, | ||
customUserID: customUserID | ||
) | ||
} | ||
|
||
static func coreFeatureUsed( | ||
featureName: String, | ||
parameters: [String: String] = [:], | ||
customUserID: String? = nil | ||
) { | ||
let featureParameters = [ | ||
"TelemetryDeck.Activation.featureName": featureName | ||
] | ||
|
||
self.internalSignal( | ||
"TelemetryDeck.Activation.coreFeatureUsed", | ||
parameters: featureParameters.merging(parameters) { $1 }, | ||
customUserID: customUserID | ||
) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
Sources/TelemetryDeck/PirateMetrics/TelemetryDeck+Referral.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Foundation | ||
|
||
extension TelemetryDeck { | ||
static func referralSent( | ||
receiversCount: Int = 1, | ||
kind: String? = nil, | ||
parameters: [String: String] = [:], | ||
customUserID: String? = nil | ||
) { | ||
// TODO: document all new parameters and their types in the default parameters doc | ||
var referralParameters = ["TelemetryDeck.Referral.receiversCount": String(receiversCount)] | ||
|
||
if let kind { | ||
referralParameters["TelemetryDeck.Referral.kind"] = kind | ||
} | ||
|
||
self.internalSignal( | ||
"TelemetryDeck.Referral.sent", | ||
parameters: referralParameters.merging(parameters) { $1 }, | ||
customUserID: customUserID | ||
) | ||
} | ||
|
||
// TODO: explicitly mention how this can be used for NPS Score or for App Store like ratings | ||
static func userRatingSubmitted( | ||
rating: Int, | ||
comment: String? = nil, | ||
parameters: [String: String] = [:], | ||
customUserID: String? = nil | ||
) { | ||
guard (0...10).contains(rating) else { | ||
TelemetryManager.shared.configuration.logHandler?.log(.error, message: "Rating must be between 0 and 10") | ||
return | ||
} | ||
|
||
var ratingParameters = [ | ||
"TelemetryDeck.Referral.ratingValue": String(rating) | ||
] | ||
|
||
if let comment { | ||
ratingParameters["TelemetryDeck.Referral.ratingComment"] = comment | ||
} | ||
|
||
self.internalSignal( | ||
"TelemetryDeck.Referral.userRatingSubmitted", | ||
parameters: ratingParameters.merging(parameters) { $1 }, | ||
customUserID: customUserID | ||
) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Sources/TelemetryDeck/PirateMetrics/TelemetryDeck+Revenue.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Foundation | ||
|
||
extension TelemetryDeck { | ||
static func paywallShown( | ||
reason: String, | ||
parameters: [String: String] = [:], | ||
customUserID: String? = nil | ||
) { | ||
let paywallParameters = ["TelemetryDeck.Revenue.paywallShowReason": reason] | ||
|
||
self.internalSignal( | ||
"TelemetryDeck.Revenue.paywallShown", | ||
parameters: paywallParameters.merging(parameters) { $1 }, | ||
customUserID: customUserID | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters