Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to not make a new instance the primary and the abil… #552

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions Sources/Mixpanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ 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 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

- 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.
Expand All @@ -46,7 +49,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,
Expand All @@ -61,7 +65,8 @@ open class Mixpanel {
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
serverURL: serverURL)
serverURL: serverURL,
isMain: isMain)
}
#else
/**
Expand All @@ -76,9 +81,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.
Expand All @@ -94,7 +101,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,
Expand All @@ -109,7 +117,8 @@ open class Mixpanel {
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
serverURL: serverURL)
serverURL: serverURL,
isMain: isMain)
}
#endif // os(OSX)

Expand All @@ -123,6 +132,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.
Expand Down Expand Up @@ -233,25 +251,30 @@ class MixpanelManager {
optOutTrackingByDefault: Bool = false,
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,
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
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!
Expand Down