From 1f7b30a94648cb10304b4e8cff1a7320ad7e5b79 Mon Sep 17 00:00:00 2001 From: Siarhei Hleb Date: Thu, 21 Jul 2022 14:08:10 +0300 Subject: [PATCH] Added the ability to not make a new instance the primary and the ability to receive all instances --- Sources/Mixpanel.swift | 54 +++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/Sources/Mixpanel.swift b/Sources/Mixpanel.swift index 45a2db8b..32f1b416 100644 --- a/Sources/Mixpanel.swift +++ b/Sources/Mixpanel.swift @@ -28,9 +28,11 @@ open class Mixpanel { 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 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 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 + + - parameter isMain: Whether to make the new instance primary. - 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. @@ -46,7 +48,8 @@ open class Mixpanel { trackAutomaticEvents: Bool? = nil, useUniqueDistinctId: Bool = false, superProperties: Properties? = nil, - serverURL: String? = nil) -> MixpanelInstance { + serverURL: String? = nil, + isMain: Bool = true) -> MixpanelInstance { #if DEBUG didDebugInit( distinctId: apiToken, @@ -61,7 +64,8 @@ open class Mixpanel { trackAutomaticEvents: trackAutomaticEvents, useUniqueDistinctId: useUniqueDistinctId, superProperties: superProperties, - serverURL: serverURL) + serverURL: serverURL, + isMain: isMain) } #else /** @@ -76,9 +80,11 @@ open class Mixpanel { - 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 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 + + - parameter isMain: Whether to make the new instance primary. - 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. @@ -94,7 +100,8 @@ open class Mixpanel { optOutTrackingByDefault: Bool = false, useUniqueDistinctId: Bool = false, superProperties: Properties? = nil, - serverURL: String? = nil) -> MixpanelInstance { + serverURL: String? = nil, + isMain: Bool = true) -> MixpanelInstance { #if DEBUG didDebugInit( distinctId: apiToken, @@ -108,7 +115,8 @@ open class Mixpanel { optOutTrackingByDefault: optOutTrackingByDefault, useUniqueDistinctId: useUniqueDistinctId, superProperties: superProperties, - serverURL: serverURL) + serverURL: serverURL, + isMain: isMain) } #endif // os(OSX) @@ -122,6 +130,15 @@ open class Mixpanel { open class func getInstance(name: String) -> MixpanelInstance? { return MixpanelManager.sharedInstance.getInstance(name: name) } + + /** + Gets all mixpanel instances + + - returns: returns the array of mixpanel instances + */ + public class func getAllInstances() -> [MixpanelInstance]? { + return MixpanelManager.sharedInstance.getAllInstances() + } /** Returns the main instance that was initialized. @@ -227,25 +244,30 @@ class MixpanelManager { trackAutomaticEvents: Bool? = nil, useUniqueDistinctId: Bool = false, superProperties: Properties? = nil, - serverURL: String? = nil + serverURL: String? = nil, + isMain: Bool ) -> MixpanelInstance { instanceQueue.sync { var instance: MixpanelInstance? if let instance = instances[instanceName] { - mainInstance = instance + if isMain || mainInstance == nil { + mainInstance = instance + } return } instance = MixpanelInstance(apiToken: apiToken, - flushInterval: flushInterval, - name: instanceName, - optOutTrackingByDefault: optOutTrackingByDefault, - trackAutomaticEvents: trackAutomaticEvents, - useUniqueDistinctId: useUniqueDistinctId, - superProperties: superProperties, - serverURL: serverURL) + flushInterval: flushInterval, + name: instanceName, + optOutTrackingByDefault: optOutTrackingByDefault, + trackAutomaticEvents: trackAutomaticEvents, + useUniqueDistinctId: useUniqueDistinctId, + superProperties: superProperties, + serverURL: serverURL) readWriteLock.write { instances[instanceName] = instance! - mainInstance = instance! + if isMain || mainInstance == nil { + mainInstance = instance! + } } } return mainInstance!