mixpanel / mixpanel-swift

Official iOS (Swift) Tracking Library for Mixpanel Analytics
https://mixpanel.com
Apache License 2.0
434 stars 234 forks source link

Feature request: Ability to pass in URLSession to Mixpanel #619

Open RamblinWreck77 opened 11 months ago

RamblinWreck77 commented 11 months ago

Generally speaking the fewer URLSession's the better, and some apps need to go beyond URLSession.shared to access advanced configuration that benefits their use case.

Currently, inside Network.swift tasks are queued up on URLSession.shared.dataTask { }

While this makes perfect sense as the default, it would be nice to be able to optionally provide your app's custom URLSession on mixpanel init and reuse that.

Perhaps something like:

    @discardableResult
    open class func initialize(token apiToken: String,
                               trackAutomaticEvents: Bool,
                               flushInterval: Double = 60,
                               instanceName: String? = nil,
                               optOutTrackingByDefault: Bool = false,
                               useUniqueDistinctId: Bool = false,
                               superProperties: Properties? = nil,
                               urlSession: URLSession = URLSession.shared,                  // By not passing in, .shared is used
                               serverURL: String? = nil) -> MixpanelInstance {
        return MixpanelManager.sharedInstance.initialize(token: apiToken,
                                                         flushInterval: flushInterval,
                                                         instanceName: ((instanceName != nil) ? instanceName! : apiToken),
                                                         trackAutomaticEvents: trackAutomaticEvents,
                                                         optOutTrackingByDefault: optOutTrackingByDefault,
                                                         useUniqueDistinctId: useUniqueDistinctId,
                                                         superProperties: superProperties,
                                                         serverURL: serverURL)
    }