Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Oct 21, 2023
1 parent ac2cce0 commit f9503ea
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

> Telemetry is an open-source SDK for Google Analytics. We believe in transparency and security, hence the open-source approach.

## Installation
You can add Telemetry to your `Package.swift` file:

Expand Down Expand Up @@ -42,7 +41,6 @@ Telemetry.screenView("Cheers")
### Sessions
By calling `session(start: true)` when the application opens and `session(start: false)` when it closes, you can track individual user sessions. Here's an example of how to do this in your `UIApplicationDelegate` application:


```swift
Telemetry.trackerID = "UA-XXXXX-XX")
Telemetry.session(start: true) // applicationDidBecomeActive
Expand Down Expand Up @@ -79,7 +77,6 @@ Telemetry.timing(category: "Database", variable: "Fetch", time: elapsedTime, lab
- When setting up your Google Analytics account, ensure to use the legacy `Universal Analytics property` and not GA4. This legacy option is under the advanced menu during account setup.
- Why are closed-source SDKs a concern? According to Apple's app-review guidelines: `Ensure that all software frameworks and dependencies also adhere to the App Store Review Guidelines`.


### Resources:
- Anonymous GA: https://stackoverflow.com/questions/50392242/how-anonymize-google-analytics-for-ios-for-gdpr-rgpd-purpose
- Guide on fingerprinting in iOS: https://nshipster.com/device-identifiers/
Expand All @@ -88,6 +85,5 @@ Telemetry.timing(category: "Database", variable: "Fetch", time: elapsedTime, lab
- Another noteworthy tracker project: https://github.com/devxoul/Umbrella
- Using Google Analytics for Tracking SaaS: https://reflectivedata.com/using-google-analytics-for-tracking-saas/


### Todo:
- Add info to this readme on how to setup Google analytics for your google account etc 🚧
6 changes: 3 additions & 3 deletions Sources/Telemetry/Telemetry+Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension Telemetry {
}
// If custom dimensions are not nil, merge them into the query arguments
if let customDim: [String: String] = self.customDimArgs {
queryArgs.merge(customDim) { _, new in new }
queryArgs.merge(customDim) { (_: String, new: String) in new }
}
// Update the "aip" key in the query arguments based on the anonymizeIP flag
queryArgs["aip"] = anonymizeIP ? "1" : nil
Expand All @@ -55,9 +55,9 @@ extension Telemetry {
// Generate a URL with the arguments, return if URL generation fails
guard let url: URL = Self.getURL(with: arguments) else { return }
// Create a data task with the URL
let task = session.dataTask(with: url) { _, _, error in
let task: URLSessionDataTask = session.dataTask(with: url) { (_ : Data?, _ : URLResponse?, error: Error?) in
// If there is an error, print it and call the completion handler with false
if let errorResponse = error?.localizedDescription {
if let errorResponse: String = error?.localizedDescription {
Swift.print("⚠️️ Failed to deliver GA Request. ", errorResponse)
complete?(false)
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/Telemetry/Telemetry+Const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ extension Telemetry {
* Flag to anonymize user's IP
* - Description: To ensure GDPR compliance, Telemetry requests Google Analytics to anonymize user IPs by default. Set this to false to opt-out.
*/
public static var anonymizeIP = true

public static var anonymizeIP: Bool = true
/**
* Google Analytics Identifier (Tracker ID)
* - Remark: This token can be obtained from the Google Analytics entity's admin page.
* - Remark: A valid Google Analytics tracker ID (format: UA-XXXXX-XX) must be set before reporting any events.
*/
public static var trackerId: String = "UA-XXXXX-XX"

/**
* Custom dimension arguments
* - Description: A dictionary of custom key-value pairs to be added to every query.
Expand All @@ -47,7 +45,7 @@ extension Telemetry {
* Network session
* - Remark: Consider renaming to urlSession
*/
public static let session = URLSession.shared
public static let session: URLSession = .shared
/**
* Telemetry type
* - Description: Allows switching between ga-endpoint and aggregator-endpoint
Expand Down
2 changes: 1 addition & 1 deletion Sources/Telemetry/ext/Dict+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension Dictionary {
* - Returns: A new dictionary that contains the combined key-value pairs.
*/
func combinedWith(_ other: [Key: Value]) -> [Key: Value] {
var dict = self // Initializes a new dictionary with the current dictionary
var dict: [Key : Value] = self // Initializes a new dictionary with the current dictionary
for (key, value) in other { // Loops through the key-value pairs in the other dictionary
dict[key] = value // Adds the key-value pair to the new dictionary
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/Telemetry/ext/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class Keychain {
*/
internal static func set(key: String, value: String) throws {
// Convert the string value to data
guard let valueData = value.data(using: .utf8) else {
guard let valueData: Data = value.data(using: .utf8) else {
Swift.print("Keychain: Unable to store data, invalid input - key: \(key), value: \(value)")
return
}
Expand Down Expand Up @@ -54,15 +54,16 @@ internal class Keychain {
]
var result: AnyObject?
// Load the item from the keychain
let resultCodeLoad = withUnsafeMutablePointer(to: &result) {
let resultCodeLoad: OSStatus = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(queryLoad as CFDictionary, UnsafeMutablePointer($0))
}
if resultCodeLoad != 0 { // Checks if the result code for loading the keychain data is not 0
print("Keychain: unable to load data - \(resultCodeLoad)") // Prints an error message with the result code if the keychain data cannot be loaded
return nil // Returns nil if the keychain data cannot be loaded
}
// Convert the data to a string
guard let resultVal = result as? NSData, let keyValue = NSString(data: resultVal as Data, encoding: String.Encoding.utf8.rawValue) as String? else {
guard let resultVal: NSData = result as? NSData,
let keyValue: String = NSString(data: resultVal as Data, encoding: String.Encoding.utf8.rawValue) as String? else {
print("Keychain: error parsing keychain result - \(resultCodeLoad)")
return nil
}
Expand All @@ -85,7 +86,7 @@ extension Keychain {
kSecAttrAccount as String: itemKey as AnyObject // Define the account attribute of the item to be deleted
]
// Delete the item from the keychain
let resultCodeDelete = SecItemDelete(queryDelete as CFDictionary)
let resultCodeDelete: OSStatus = SecItemDelete(queryDelete as CFDictionary)
if resultCodeDelete != 0 { // Checks if the result code for deleting the keychain item is not 0
print("Keychain: unable to delete from keychain: \(resultCodeDelete)") // Prints an error message with the result code if the keychain item cannot be deleted
} else {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Telemetry/util/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class System {
* TODO: Consider handling different language formats (e.g., en-US, en-GB).
*/
internal static let userLanguage: String = {
guard let locale = Locale.preferredLanguages.first, !locale.isEmpty else {
guard let locale: String = Locale.preferredLanguages.first, !locale.isEmpty else {
return "(not set)"
}
return locale
Expand Down Expand Up @@ -73,11 +73,11 @@ internal class System {
// Check if the OS is macOS
#if os(macOS)
// Get the OS version
let osVersion = ProcessInfo.processInfo.operatingSystemVersionString
let osVersion: String = ProcessInfo.processInfo.operatingSystemVersionString
// Replace "." with "_" in the version string
let versionString = osVersion.replacingOccurrences(of: ".", with: "_")
let versionString: String = osVersion.replacingOccurrences(of: ".", with: "_")
// Define the user agent for macOS
let fallbackAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X \(versionString)) AppleWebKit/603.2.4 (KHTML, like Gecko) \(appName)/\(appVersion)" // swiftlint:disable:this line_length
let fallbackAgent: String = "Mozilla/5.0 (Macintosh; Intel Mac OS X \(versionString)) AppleWebKit/603.2.4 (KHTML, like Gecko) \(appName)/\(appVersion)" // swiftlint:disable:this line_length
#else
// If not macOS, then it's iOS. Get the device details
let currentDevice = UIDevice.current
Expand Down

0 comments on commit f9503ea

Please sign in to comment.