TelemetryDeck / SwiftSDK

Swift SDK for TelemetryDeck, a privacy-conscious analytics service for apps and websites.
https://telemetrydeck.com/
Other
149 stars 31 forks source link

Question: How to disable if users opts out of analytics in SwiftUI #53

Closed ca13ra1 closed 2 years ago

ca13ra1 commented 2 years ago

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.

init() {
    // MARK: ERROR Accessing StateObject's object without being installed on a View. This will create a new instance each time.
    /*
    if viewmodel.enabled {

    } else {

    }
     */
    // MARK: - Analytics
    let configuration = TelemetryManagerConfiguration(appID: "ID_HERE")
    TelemetryManager.initialize(with: configuration)
    TelemetryManager.send("appLaunchedRegularly")
}
ca13ra1 commented 2 years ago

Ah, I found it. There’s sendNewSessionBeganSignal

ca13ra1 commented 2 years ago

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
                })
        }
    }
}
winsmith commented 2 years ago

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?

ca13ra1 commented 2 years ago

@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)
                })
        }
    }
}
winsmith commented 2 years ago

That looks like a very good solution for now! :)