Skip to content

Commit

Permalink
added comment for macos initialize function and separated MixpanelIns…
Browse files Browse the repository at this point in the history
…tance creation for ProxyServerConfig & ServerURL
  • Loading branch information
mohitanand-cred committed Feb 9, 2024
1 parent 91c16b9 commit c432bc8
Showing 1 changed file with 59 additions and 21 deletions.
80 changes: 59 additions & 21 deletions Sources/Mixpanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,29 @@ open class Mixpanel {
serverURL: serverURL)
}

/**
Initializes an instance of the API with the given project token (MAC OS ONLY).

Returns a new Mixpanel instance API object. This allows you to create more than one instance
of the API object, which is convenient if you'd like to send data to more than
one Mixpanel project from a single app.

- parameter token: your project token
- parameter flushInterval: Optional. Interval to run background flushing
- parameter instanceName: Optional. The name you want to uniquely identify the Mixpanel Instance.
It is useful when you want more than one Mixpanel instance under the same project token.
- parameter optOutTrackingByDefault: Optional. Whether or not to be opted out from tracking by default
- parameter useUniqueDistinctId: Optional. whether or not to use the unique device identifier as the distinct_id
- parameter superProperties: Optional. Super properties dictionary to register during initialization
- parameter proxyServerConfig: Optional. Setup for proxy server.

- important: If you have more than one Mixpanel instance, it is beneficial to initialize
the instances with an instanceName. Then they can be reached by calling getInstance with name.

- returns: returns a mixpanel instance if needed to keep throughout the project.
You can always get the instance by calling getInstance(name)
*/

@discardableResult
open class func initialize(token apiToken: String,
flushInterval: Double = 60,
Expand Down Expand Up @@ -237,35 +260,50 @@ final class MixpanelManager {
optOutTrackingByDefault: Bool = false,
useUniqueDistinctId: Bool = false,
superProperties: Properties? = nil,
serverURL: String? = nil,
proxyServerConfig: ProxyServerConfig? = nil
serverURL: String? = nil
) -> MixpanelInstance {
return dequeueInstance(instanceName: instanceName) {
return MixpanelInstance(apiToken: apiToken,
flushInterval: flushInterval,
name: instanceName,
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
serverURL: serverURL)
}
}

func initialize(token apiToken: String,
flushInterval: Double,
instanceName: String,
trackAutomaticEvents: Bool,
optOutTrackingByDefault: Bool = false,
useUniqueDistinctId: Bool = false,
superProperties: Properties? = nil,
proxyServerConfig: ProxyServerConfig
) -> MixpanelInstance {
return dequeueInstance(instanceName: instanceName) {
return MixpanelInstance(apiToken: apiToken,
flushInterval: flushInterval,
name: instanceName,
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
proxyServerConfig: proxyServerConfig)
}
}

private func dequeueInstance(instanceName: String, instanceCreation: () -> MixpanelInstance) -> MixpanelInstance {
instanceQueue.sync {
var instance: MixpanelInstance?
if let instance = instances[instanceName] {
mainInstance = instance
return
}

if let proxyServerConfig = proxyServerConfig {
instance = MixpanelInstance(apiToken: apiToken,
flushInterval: flushInterval,
name: instanceName,
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
proxyServerConfig: proxyServerConfig)
} else {
instance = MixpanelInstance(apiToken: apiToken,
flushInterval: flushInterval,
name: instanceName,
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
serverURL: serverURL)
}
instance = instanceCreation()
readWriteLock.write {
instances[instanceName] = instance!
mainInstance = instance!
Expand Down

0 comments on commit c432bc8

Please sign in to comment.