Skip to content

Commit

Permalink
Merge pull request #530 from igled7/feature/fix-data-race
Browse files Browse the repository at this point in the history
Allow setting server URL during initialization
  • Loading branch information
jaredmixpanel authored Apr 11, 2022
2 parents e3feb51 + e4e2304 commit 6916b30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
21 changes: 15 additions & 6 deletions Sources/Mixpanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ open class Mixpanel {
- parameter trackAutomaticEvents: Optional. Whether or not to collect common mobile events, it takes precedence over Autotrack settings from the Mixpanel server.
- 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 serverURL: Optional. Mixpanel cluster URL

- 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.
Expand All @@ -43,14 +44,16 @@ open class Mixpanel {
optOutTrackingByDefault: Bool = false,
trackAutomaticEvents: Bool? = nil,
useUniqueDistinctId: Bool = false,
superProperties: Properties? = nil) -> MixpanelInstance {
superProperties: Properties? = nil,
serverURL: String? = nil) -> MixpanelInstance {
return MixpanelManager.sharedInstance.initialize(token: apiToken,
flushInterval: flushInterval,
instanceName: ((instanceName != nil) ? instanceName! : apiToken),
optOutTrackingByDefault: optOutTrackingByDefault,
trackAutomaticEvents: trackAutomaticEvents,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties)
superProperties: superProperties,
serverURL: serverURL)
}
#else
/**
Expand All @@ -66,6 +69,7 @@ open class Mixpanel {
- 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 serverURL: Optional. Mixpanel cluster URL

- 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.
Expand All @@ -80,13 +84,15 @@ open class Mixpanel {
instanceName: String? = nil,
optOutTrackingByDefault: Bool = false,
useUniqueDistinctId: Bool = false,
superProperties: Properties? = nil) -> MixpanelInstance {
superProperties: Properties? = nil,
serverURL: String? = nil) -> MixpanelInstance {
return MixpanelManager.sharedInstance.initialize(token: apiToken,
flushInterval: flushInterval,
instanceName: ((instanceName != nil) ? instanceName! : apiToken),
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties)
superProperties: superProperties,
serverURL: serverURL)
}
#endif // os(OSX)

Expand Down Expand Up @@ -158,7 +164,9 @@ class MixpanelManager {
optOutTrackingByDefault: Bool = false,
trackAutomaticEvents: Bool? = nil,
useUniqueDistinctId: Bool = false,
superProperties: Properties? = nil) -> MixpanelInstance {
superProperties: Properties? = nil,
serverURL: String? = nil
) -> MixpanelInstance {
instanceQueue.sync {
var instance: MixpanelInstance?
if let instance = instances[instanceName] {
Expand All @@ -171,7 +179,8 @@ class MixpanelManager {
optOutTrackingByDefault: optOutTrackingByDefault,
trackAutomaticEvents: trackAutomaticEvents,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties)
superProperties: superProperties,
serverURL: serverURL)
readWriteLock.write {
instances[instanceName] = instance!
mainInstance = instance!
Expand Down
7 changes: 6 additions & 1 deletion Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,15 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
#endif // DECIDE

init(apiToken: String?, flushInterval: Double, name: String, optOutTrackingByDefault: Bool = false,
trackAutomaticEvents: Bool? = nil, useUniqueDistinctId: Bool = false, superProperties: Properties? = nil) {
trackAutomaticEvents: Bool? = nil, useUniqueDistinctId: Bool = false, superProperties: Properties? = nil,
serverURL: String? = nil) {
if let apiToken = apiToken, !apiToken.isEmpty {
self.apiToken = apiToken
}
if let serverURL = serverURL {
self.serverURL = serverURL
BasePath.namedBasePaths[name] = serverURL
}
let label = "com.mixpanel.\(self.apiToken)"
trackingQueue = DispatchQueue(label: "\(label).tracking)", qos: .utility)
networkQueue = DispatchQueue(label: "\(label).network)", qos: .utility)
Expand Down

0 comments on commit 6916b30

Please sign in to comment.