Closed ca13ra1 closed 2 years ago
Ah, I found it. There’s sendNewSessionBeganSignal
Solved and closing.
struct SomeApp: App {
@State var enabled: Bool = true
init() {
// MARK: - Analytics
let configuration = TelemetryManagerConfiguration(appID: "APP_ID_HERE")
configuration.sendNewSessionBeganSignal = enabled
TelemetryManager.initialize(with: configuration)
TelemetryManager.send("appLaunchedRegularly")
}
var body: some Scene {
WindowGroup {
ContentView()
.onChange(of: viewmodel.enabled, perform: { value in
enabled = value
})
}
}
}
You're almost correct: if you set configuration.sendNewSessionBeganSignal
to false
, this will not send a new "newSessionBegan" signal. However, if you manually call TelemetryManager.send
, this will still result in a signal being sent.
Completely disabling the SDK is a feature we're working on, but it isn't there yet (your PRs are very welcome tho). In the meantime, you'll have to manually check your setting and only call send
if enabled == True
. Maybe with an extension to TelemetryManager?
@winsmith I think I found a simple solution, created an extension for TelemetryManager
.
extension TelemetryManager {
// Don't send anything if enabled is false
public static func analytics(_ signalType: TelemetrySignalType, for clientUser: String? = nil, with additionalPayload: [String: String] = [:], _ enabled: Bool) {
if enabled {
TelemetryManager.shared.send(signalType, for: clientUser, with: additionalPayload)
} else { return }
}
}
Then called in my View
, I think it's going to suffice for the task I need.
struct SomeApp: App {
@StateObject var viewmodel = ViewModel()
init() {
// MARK: - Analytics
let configuration = TelemetryManagerConfiguration(appID: "APP_ID_HERE")
TelemetryManager.initialize(with: configuration)
}
var body: some Scene {
WindowGroup {
ContentView()
.onAppear(perform: {
TelemetryManager.analytics("appLaunchedRegularly", viewmodel.enabled)
})
}
}
}
That looks like a very good solution for now! :)
I'm releasing an application soon, I just noticed there's not the ability to disable since we're putting the initialization inside my App's
init()
in SwiftUI. I'd like to have an option for the user to disable if they so chose in my apps settings.