Skip to content

Commit

Permalink
Merge pull request #214 from TelemetryDeck/feature/default-hashed-user
Browse files Browse the repository at this point in the history
Get 'hashedDefaultUser' working even without a custom user set
  • Loading branch information
Jeehut authored Dec 2, 2024
2 parents 274c25a + 2515ead commit 359a1df
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 3 additions & 1 deletion Sources/TelemetryDeck/Signals/SignalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import TVUIKit
protocol SignalManageable {
func processSignal(_ signalName: String, parameters: [String: String], floatValue: Double?, customUserID: String?, configuration: TelemetryManagerConfiguration)
func attemptToSendNextBatchOfCachedSignals()

@MainActor var defaultUserIdentifier: String { get }
}

final class SignalManager: SignalManageable, @unchecked Sendable {
Expand Down Expand Up @@ -237,7 +239,7 @@ private extension SignalManager {

// MARK: - Helpers

private extension SignalManager {
extension SignalManager {
#if os(macOS)
/// A custom ``UserDefaults`` instance specific to TelemetryDeck and the current application.
private var customDefaults: UserDefaults? {
Expand Down
5 changes: 3 additions & 2 deletions Sources/TelemetryDeck/TelemetryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ public final class TelemetryManager: @unchecked Sendable {
TelemetryDeck.updateDefaultUserID(to: newDefaultUser)
}

public var hashedDefaultUser: String? {
guard let defaultUser = configuration.defaultUser else { return nil }
@MainActor
public var hashedDefaultUser: String {
let defaultUser = self.signalManager.defaultUserIdentifier
return CryptoHashing.sha256(string: defaultUser, salt: configuration.salt)
}

Expand Down
12 changes: 7 additions & 5 deletions Tests/TelemetryDeckTests/TelemetryDeckTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct TelemetryDeckTests {
}
}

var configuration = TelemetryManagerConfiguration(appID: UUID().uuidString)
let configuration = TelemetryManagerConfiguration(appID: UUID().uuidString)
configuration.metadataEnrichers.append(BasicEnricher())

let signalManager = FakeSignalManager()
Expand All @@ -85,7 +85,7 @@ struct TelemetryDeckTests {
}
}

var configuration = TelemetryManagerConfiguration(appID: UUID().uuidString)
let configuration = TelemetryManagerConfiguration(appID: UUID().uuidString)
configuration.metadataEnrichers.append(BasicEnricher())

let signalManager = FakeSignalManager()
Expand Down Expand Up @@ -117,7 +117,7 @@ struct TelemetryDeckTests {
func sendsSignals_withAnalyticsExplicitlyEnabled() {
let YOUR_APP_ID = "44e0f59a-60a2-4d4a-bf27-1f96ccb4aaa3"

var configuration = TelemetryManagerConfiguration(appID: YOUR_APP_ID)
let configuration = TelemetryManagerConfiguration(appID: YOUR_APP_ID)
configuration.analyticsDisabled = false

let signalManager = FakeSignalManager()
Expand All @@ -132,7 +132,7 @@ struct TelemetryDeckTests {
func doesNotSendSignals_withAnalyticsExplicitlyDisabled() {
let YOUR_APP_ID = "44e0f59a-60a2-4d4a-bf27-1f96ccb4aaa3"

var configuration = TelemetryManagerConfiguration(appID: YOUR_APP_ID)
let configuration = TelemetryManagerConfiguration(appID: YOUR_APP_ID)
configuration.analyticsDisabled = true

let signalManager = FakeSignalManager()
Expand All @@ -149,7 +149,7 @@ struct TelemetryDeckTests {

let YOUR_APP_ID = "44e0f59a-60a2-4d4a-bf27-1f96ccb4aaa3"

var configuration = TelemetryManagerConfiguration(appID: YOUR_APP_ID)
let configuration = TelemetryManagerConfiguration(appID: YOUR_APP_ID)
configuration.analyticsDisabled = false

let signalManager = FakeSignalManager()
Expand Down Expand Up @@ -206,4 +206,6 @@ private class FakeSignalManager: @preconcurrency SignalManageable {
}

func attemptToSendNextBatchOfCachedSignals() {}

var defaultUserIdentifier: String { UUID().uuidString }
}

0 comments on commit 359a1df

Please sign in to comment.