Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Oct 1, 2023
1 parent 7a67e4c commit 4b1c236
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 58 deletions.
19 changes: 10 additions & 9 deletions Sources/Telemetry/Telemetry+Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ extension Telemetry {
* - complete: Use complete only for the GA type
*/
public static func action(_ action: ActionKind, complete: Complete? = nil) {
// Check if the telemetry type is Google Analytics
if case TMType.ga = tmType {
// Send the action to Google Analytics
send(type: action.key, parameters: action.output, complete: complete)
} else if case TMType.agg(let agg) = tmType {
// If the telemetry type is not Google Analytics, append the action to the aggregator
if case TMType.ga = tmType { // Check if the telemetry type is Google Analytics
send(type: action.key, // Type of action being sent
parameters: action.output, // Parameters associated with the action
complete: complete // Completion handler to be called when the action is complete
) // Send the action to Google Analytics
} else if case TMType.agg(let agg) = tmType { // If the telemetry type is not Google Analytics,
do {
try agg.append(action: action)
try agg.append(action: action) // append the action to the aggregator
}
catch { // Catch and print any errors that occur when appending the action
Swift.print("Error: \(error.localizedDescription)")
}
// Catch and print any errors that occur when appending the action
catch { Swift.print("Error: \(error.localizedDescription)") }
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions Sources/Telemetry/Telemetry+Const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import Foundation
* Constants for Telemetry
*/
extension Telemetry {
// Typealias for completion handler
/**
* Typealias for completion handler
Æ - Parameter success: A boolean value indicating whether the operation was successful or not
*/
public typealias Complete = (_ success: Bool) -> Void

// Base URL for Google Analytics
/**
* Base URL for Google Analytics
*/
internal static let baseURL: URL? = .init(string: "https://www.google-analytics.com/")
}
/**
Expand Down
1 change: 0 additions & 1 deletion Sources/Telemetry/Telemetry.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* The `Telemetry` class facilitates event and screen view tracking using the Google Analytics Measurement protocol.
* More details can be found here: https://developers.google.com/analytics/devguides/collection/protocol/v1/reference
*
* - Note: Google has officially discontinued mobile analytics tracking through Google Analytics. It is recommended for new apps to use Firebase instead.
* - Note: This library converts screen views into pageviews. Therefore, new tracking properties must be configured as websites in the Google Analytics admin console.
* - Note: The app bundle identifier, which can be customized for privacy reasons, is used as a dummy hostname for tracking pageviews and screen views.
Expand Down
21 changes: 16 additions & 5 deletions Sources/Telemetry/type/Event.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import Foundation

/**
* `Event` is a struct that represents a telemetry event.
* It conforms to the `ActionKind` protocol.
*/
public struct Event: ActionKind {
public let category: String // The category of the event
public let action: String // The action of the event
public let label: String // The label of the event
public let params: [String: String] // A dictionary of additional parameters for the event
/**
* The category of the event
*/
public let category: String
/**
* The action of the event
*/
public let action: String
/**
* The label of the event
*/
public let label: String
/**
* A dictionary of additional parameters for the event
*/
public let params: [String: String]
/**
* Initializes an `Event` instance.
* - Parameters:
Expand Down
15 changes: 12 additions & 3 deletions Sources/Telemetry/type/Exception.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import Foundation
* It conforms to the `ActionKind` protocol.
*/
public struct Exception: ActionKind {
public let description: String // A string that describes the exception.
public let isFatal: Bool // A boolean that indicates if the exception was fatal to the execution of the program.
public let params: [String: String] // A dictionary of additional parameters for the event.
/**
* A string that describes the exception.
*/
public let description: String
/**
* A boolean that indicates if the exception was fatal to the execution of the program.
*/
public let isFatal: Bool
/**
* A dictionary of additional parameters for the event.
*/
public let params: [String: String]
/**
* Initializes an instance of `Exception`.
* - Parameters:
Expand Down
12 changes: 9 additions & 3 deletions Sources/Telemetry/type/ScreenView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import Foundation
* It contains the name of the screen and a dictionary of parameters associated with the screen view event.
*/
public struct ScreenView: ActionKind {
// The name of the screen.
/**
* The name of the screen.
*/
public let name: String
// A dictionary of parameters associated with the screen view event.
/**
* A dictionary of parameters associated with the screen view event.
*/
public let params: [String: String]
/**
* This method tracks a screen view event as a page view to Google Analytics by setting the required parameters.
Expand All @@ -29,7 +33,9 @@ public struct ScreenView: ActionKind {
* `ScreenView` extension that provides additional functionality.
*/
extension ScreenView {
// The key used to identify a page view event.
/**
* The key used to identify a page view event.
*/
public var key: String { "pageview" }
/**
* This computed property generates the output dictionary for the screen view event.
Expand Down
24 changes: 15 additions & 9 deletions Sources/Telemetry/type/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,44 @@ import Foundation
/**
* Session
* This struct represents a session in the context of Google Analytics.
* It has two properties: start and params.
* The start property is a boolean indicating whether the session has started or ended.
* The params property is a dictionary of parameters associated with the session.
*/
public struct Session: ActionKind {
/**
A boolean indicating whether the session has started or ended.
*/
public let start: Bool
/**
A dictionary of parameters associated with the session.
*/
public let params: [String: String]

/**
* This initializer sets up a new Session instance.
* It tracks a session start to Google Analytics by setting the `sc` parameter of the request.
* The `dp` parameter is set to the name of the application.
* - Remark: Sessions are reported with `session(_:parameters:)` with the first parameter set to true for session start or false for session end
* - Parameter start: true indicate session started, false - session finished.
* - Parameter params: A dictionary of parameters associated with the session. Default is an empty dictionary.
* - Parameters:
* - start: true indicate session started, false - session finished.
* - params: A dictionary of parameters associated with the session. Default is an empty dictionary.
*/
public init(start: Bool, params: [String: String] = .init()) {
self.start = start
self.params = params
}
}

/**
* Extension of Session
* This extension adds two computed properties to the Session struct: key and output.
* The key property is a string that represents the key for the session.
* The output property is a dictionary that represents the output of the session.
*/
extension Session {
// The key for the session.
/**
* The key for the session.
*/
public var key: String { "session" }
// The output of the session. It includes the original parameters and adds two more: "sc" and "dp".
/**
* The output of the session. It includes the original parameters and adds two more: "sc" and "dp".
*/
public var output: [String: String] {
var params: [String: String] = self.params
params["sc"] = start ? "start" : "end" // "sc" indicates whether the session has started or ended.
Expand Down
33 changes: 26 additions & 7 deletions Sources/Telemetry/type/Timing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ import Foundation
* It includes properties for category, name, label, time, and additional parameters.
*/
public struct Timing: ActionKind {
public let category: String // The category of the timing event
public let name: String // The name of the timing event
public let label: String // The label for the timing event
public let time: TimeInterval // The time duration of the timing event in seconds
public let params: [String: String] // Additional parameters for the timing event
/**
* The category of the timing event
*/
public let category: String
/**
* The name of the timing event
*/
public let name: String
/**
* The label for the timing event
*/
public let label: String
/**
* The time duration of the timing event in seconds
*/
public let time: TimeInterval
/**
* Additional parameters for the timing event
*/
public let params: [String: String]
/**
* Initializer for the Timing struct.
* - Parameters:
Expand All @@ -32,9 +47,13 @@ public struct Timing: ActionKind {
* This extension adds two computed properties: key and output.
*/
extension Timing {
// A key for the timing event
/**
* The key for the timing event.
*/
public var key: String { "timing" }
// The output dictionary that includes all properties of the timing event
/**
* The output dictionary that includes all properties of the timing event.
*/
public var output: [String: String] {
// Initialize a new variable `params` with the current instance's `params`
var params: [String: String] = self.params
Expand Down
28 changes: 19 additions & 9 deletions Sources/Telemetry/type/util/Aggregator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ import JSONSugar
* - Fixme: ⚠️️ Rename to TMAggregator?
*/
public class Aggregator: Codable {
public var filePath: String // File path where the data will be stored
public var events: [Event] // Array of events
public var sessions: [Session] // Array of sessions
public var exceptions: [Exception] // Array of exceptions
public var screenViews: [ScreenView] // Array of screen views
public var timings: [Timing] // Array of timings
// Initializer for the Aggregator class
public var filePath: String
public var events: [Event]
public var sessions: [Session]
public var exceptions: [Exception]
public var screenViews: [ScreenView]
public var timings: [Timing]
/**
* Initializer for the Aggregator class
* - Parameters:
* - filePath: A string value representing the file path
* - events: An array of Event objects
* - sessions: An array of Session objects
* - exceptions: An array of Exception objects
* - screenViews: An array of ScreenView objects
* - timings: An array of Timing objects
*/
init(filePath: String = tempFilePath, events: [Event] = [], sessions: [Session] = [], exceptions: [Exception] = [], screenViews: [ScreenView] = [], timings: [Timing] = []) {
self.filePath = filePath
self.events = events
Expand All @@ -31,8 +40,9 @@ public class Aggregator: Codable {
// Extension for the Aggregator class to add actions
extension Aggregator {
/**
* Add action
* - Fixme: ⚠️️ Rename to send? or noterize or something?
* Add an action to the aggregator
* - Parameter action: The action to be added
* - Throws: An error if the action cannot be added
*/
public func append(action: ActionKind) throws {
// Depending on the type of action, append it to the corresponding array
Expand Down
24 changes: 16 additions & 8 deletions Sources/Telemetry/util/Identity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,24 @@ extension Identity {
}
/**
* Enum for persistence level.
* - Description: This enum defines the different types of storage that can be used to store the identity.
* - Note: The "vendor" option uses the vendor ID to store the identity, but it may not work on macOS.
* - Note: The "userdefault" option uses UserDefaults to store the identity.
* - Note: The "keychain" option uses the Keychain to store the identity.
* - Important: .vendor: Does not work on macOS, or does it now? - Fixme: ⚠️️ confirm this
*/
public enum IDType {
/**
* This enum defines the different types of storage that can be used to store the identity.
* - Note: The "vendor" option uses the vendor ID to store the identity, but it may not work on macOS.
* - Note: The "userdefault" option uses UserDefaults to store the identity.
* - Note: The "keychain" option uses the Keychain to store the identity.
* - Important: .vendor: Does not work on macOS, or does it now? - Fixme: ⚠️️ confirm this
* Uses the vendor ID to store the identity
*/
case vendor // Uses the vendor ID to store the identity
case userdefault // Uses UserDefaults to store the identity
case keychain // Uses the Keychain to store the identity
case vendor
/**
* Uses UserDefaults to store the identity
*/
case userdefault
/**
* Uses the Keychain to store the identity
*/
case keychain
case keychain
}
2 changes: 1 addition & 1 deletion Sources/Telemetry/util/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal class System {
}()
/**
* Provides the user agent string for the current device and OS.
* This is useful for identifying the device and OS in web requests.
* - Note: This is useful for identifying the device and OS in web requests.
*/
internal static let userAgent: String = {
// Check if the OS is macOS
Expand Down

0 comments on commit 4b1c236

Please sign in to comment.