MixpanelInstance

open class MixpanelInstance : CustomDebugStringConvertible, FlushDelegate, AEDelegate

The class that represents the Mixpanel Instance

  • apiToken string that identifies the project to track data to

    Declaration

    Swift

    open var apiToken: String
  • The a MixpanelDelegate object that gives control over Mixpanel network activity.

    Declaration

    Swift

    open var delegate: MixpanelDelegate?
  • distinctId string that uniquely identifies the current user.

    Declaration

    Swift

    open var distinctId: String
  • anonymousId string that uniquely identifies the device.

    Declaration

    Swift

    open var anonymousId: String?
  • userId string that identify is called with.

    Declaration

    Swift

    open var userId: String?
  • hadPersistedDistinctId is a boolean value which specifies that the stored distinct_id already exists in persistence

    Declaration

    Swift

    open var hadPersistedDistinctId: Bool?
  • alias string that uniquely identifies the current user.

    Declaration

    Swift

    open var alias: String?
  • Accessor to the Mixpanel People API object.

    Declaration

    Swift

    open var people: People!
  • Controls whether to show spinning network activity indicator when flushing data to the Mixpanel servers. Defaults to true.

    Declaration

    Swift

    open var showNetworkActivityIndicator: Bool
  • This allows enabling or disabling collecting common mobile events If this is not set, it will query the Autotrack settings from the Mixpanel server

    Declaration

    Swift

    open var trackAutomaticEventsEnabled: Bool?
  • Flush timer’s interval. Setting a flush interval of 0 will turn off the flush timer and you need to call the flush() API manually to upload queued data to the Mixpanel server.

    Declaration

    Swift

    open var flushInterval: Double { get set }
  • Control whether the library should flush data to Mixpanel when the app enters the background. Defaults to true.

    Declaration

    Swift

    open var flushOnBackground: Bool { get set }
  • Controls whether to automatically send the client IP Address as part of event tracking. With an IP address, the Mixpanel Dashboard will show you the users’ city. Defaults to true.

    Declaration

    Swift

    open var useIPAddressForGeoLocation: Bool { get set }
  • The base URL used for Mixpanel API requests. Useful if you need to proxy Mixpanel requests. Defaults to https://api.mixpanel.com.

    Declaration

    Swift

    open var serverURL: String { get set }
  • Declaration

    Swift

    open var debugDescription: String { get }
  • This allows enabling or disabling of all Mixpanel logs at run time.

    Note

    All logging is disabled by default. Usually, this is only required if you are running in to issues with the SDK and you need support.

    Declaration

    Swift

    open var loggingEnabled: Bool { get set }
  • A unique identifier for this MixpanelInstance

    Declaration

    Swift

    public let name: String
  • Controls whether to enable the visual editor for codeless on mixpanel.com You will be unable to edit codeless events with this disabled, however previously created codeless events will still be delivered.

    Declaration

    Swift

    open var enableVisualEditorForCodeless: Bool { get set }
  • Controls whether to automatically check for A/B test variants for the currently identified user when the application becomes active. Defaults to true.

    Declaration

    Swift

    open var checkForVariantsOnActive: Bool { get set }
  • Controls whether to automatically check for notifications for the currently identified user when the application becomes active. Defaults to true.

    Declaration

    Swift

    open var checkForNotificationOnActive: Bool { get set }
  • Controls whether to automatically check for and show in-app notifications for the currently identified user when the application becomes active. Defaults to true.

    Declaration

    Swift

    open var showNotificationOnActive: Bool { get set }
  • Determines the time, in seconds, that a mini notification will remain on the screen before automatically hiding itself. Defaults to 6 (seconds).

    Declaration

    Swift

    open var miniNotificationPresentationTime: Double { get set }
  • The minimum session duration (ms) that is tracked in automatic events. The default value is 10000 (10 seconds).

    Declaration

    Swift

    open var minimumSessionDuration: UInt64 { get set }
  • The maximum session duration (ms) that is tracked in automatic events. The default value is UINT64_MAX (no maximum session duration).

    Declaration

    Swift

    open var maximumSessionDuration: UInt64 { get set }

Identity

  • Sets the distinct ID of the current user.

    Mixpanel uses a randomly generated persistent UUID as the default local distinct ID.

    If you want to use a unique persistent UUID, you can define the MIXPANEL_UNIQUE_DISTINCT_ID flag in your Active Compilation Conditions build settings. It then uses the IFV String (UIDevice.current().identifierForVendor) as the default local distinct ID. This ID will identify a user across all apps by the same vendor, but cannot be used to link the same user across apps from different vendors. If we are unable to get an IFV, we will fall back to generating a random persistent UUID.

    For tracking events, you do not need to call identify:. However, Mixpanel User profiles always requires an explicit call to identify:. If calls are made to set:, increment or other People methods prior to calling identify:, then they are queued up and flushed once identify: is called.

    If you’d like to use the default distinct ID for Mixpanel People as well (recommended), call identify: using the current distinct ID: mixpanelInstance.identify(mixpanelInstance.distinctId).

    Declaration

    Swift

    open func identify(distinctId: String, usePeople: Bool = true)

    Parameters

    distinctId

    string that uniquely identifies the current user

    usePeople

    boolean that controls whether or not to set the people distinctId to the event distinctId. This should only be set to false if you wish to prevent people profile updates for that user.

  • The alias method creates an alias which Mixpanel will use to remap one id to another. Multiple aliases can point to the same identifier.

    mixpanelInstance.createAlias("New ID", distinctId: mixpanelInstance.distinctId)

    You can add multiple id aliases to the existing id

    mixpanelInstance.createAlias("Newer ID", distinctId: mixpanelInstance.distinctId)

    Declaration

    Swift

    open func createAlias(_ alias: String, distinctId: String, usePeople: Bool = true)

    Parameters

    alias

    A unique identifier that you want to use as an identifier for this user.

    distinctId

    The current user identifier.

    usePeople

    boolean that controls whether or not to set the people distinctId to the event distinctId. This should only be set to false if you wish to prevent people profile updates for that user.

  • Clears all stored properties including the distinct Id. Useful if your app’s user logs out.

    Declaration

    Swift

    open func reset()

Persistence

  • Writes current project info including the distinct Id, super properties, and pending event and People record queues to disk.

    This state will be recovered when the app is launched again if the Mixpanel library is initialized with the same project token. The library listens for app state changes and handles persisting data as needed.

    Important

    You do not need to call this method.**

    Declaration

    Swift

    open func archive()
  • Writes current project info including the distinct Id, super properties, and pending event and People record queues to disk.

    This state will be recovered when the app is launched again if the Mixpanel library is initialized with the same project token. The library listens for app state changes and handles persisting data as needed.

    Important

    You do not need to call this method.**

Flush

  • Uploads queued data to the Mixpanel server.

    By default, queued data is flushed to the Mixpanel servers every minute (the default for flushInterval), and on background (since flushOnBackground is on by default). You only need to call this method manually if you want to force a flush at a particular moment.

    Declaration

    Swift

    open func flush(completion: (() -> Void)? = nil)

    Parameters

    completion

    an optional completion handler for when the flush has completed.

Track

  • Tracks an event with properties. Properties are optional and can be added only if needed.

    Properties will allow you to segment your events in your Mixpanel reports. Property keys must be String objects and the supported value types need to conform to MixpanelType. MixpanelType can be either String, Int, UInt, Double, Float, Bool, [MixpanelType], [String: MixpanelType], Date, URL, or NSNull. If the event is being timed, the timer will stop and be added as a property.

    Declaration

    Swift

    open func track(event: String?, properties: Properties? = nil)

    Parameters

    event

    event name

    properties

    properties dictionary

  • Tracks an event with properties and to specific groups. Properties and groups are optional and can be added only if needed.

    Properties will allow you to segment your events in your Mixpanel reports. Property and group keys must be String objects and the supported value types need to conform to MixpanelType. MixpanelType can be either String, Int, UInt, Double, Float, Bool, [MixpanelType], [String: MixpanelType], Date, URL, or NSNull. If the event is being timed, the timer will stop and be added as a property.

    Declaration

    Swift

    open func trackWithGroups(event: String?, properties: Properties? = nil, groups: Properties?)

    Parameters

    event

    event name

    properties

    properties dictionary

    groups

    groups dictionary

  • Undocumented

    Declaration

    Swift

    open func getGroup(groupKey: String, groupID: MixpanelType) -> Group
  • Starts a timer that will be stopped and added as a property when a corresponding event is tracked.

    This method is intended to be used in advance of events that have a duration. For example, if a developer were to track an “Image Upload” event she might want to also know how long the upload took. Calling this method before the upload code would implicitly cause the track call to record its duration.

    Precondition

    // begin timing the image upload: mixpanelInstance.time(event:“Image Upload”) // upload the image: self.uploadImageWithSuccessHandler() { _ in // track the event mixpanelInstance.track(“Image Upload”) }

    Declaration

    Swift

    open func time(event: String)

    Parameters

    event

    the event name to be timed

  • Retrieves the time elapsed for the named event since time(event:) was called.

    Declaration

    Swift

    open func eventElapsedTime(event: String) -> Double

    Parameters

    event

    the name of the event to be tracked that was passed to time(event:)

  • Clears all current event timers.

    Declaration

    Swift

    open func clearTimedEvents()
  • Clears the event timer for the named event.

    Declaration

    Swift

    open func clearTimedEvent(event: String)

    Parameters

    event

    the name of the event to clear the timer for

  • Returns the currently set super properties.

    Declaration

    Swift

    open func currentSuperProperties() -> [String : Any]

    Return Value

    the current super properties

  • Clears all currently set super properties.

    Declaration

    Swift

    open func clearSuperProperties()
  • Registers super properties, overwriting ones that have already been set.

    Super properties, once registered, are automatically sent as properties for all event tracking calls. They save you having to maintain and add a common set of properties to your events. Property keys must be String objects and the supported value types need to conform to MixpanelType. MixpanelType can be either String, Int, UInt, Double, Float, Bool, [MixpanelType], [String: MixpanelType], Date, URL, or NSNull.

    Declaration

    Swift

    open func registerSuperProperties(_ properties: Properties)

    Parameters

    properties

    properties dictionary

  • Registers super properties without overwriting ones that have already been set, unless the existing value is equal to defaultValue. defaultValue is optional.

    Property keys must be String objects and the supported value types need to conform to MixpanelType. MixpanelType can be either String, Int, UInt, Double, Float, Bool, [MixpanelType], [String: MixpanelType], Date, URL, or NSNull.

    Declaration

    Swift

    open func registerSuperPropertiesOnce(_ properties: Properties,
                                            defaultValue: MixpanelType? = nil)

    Parameters

    properties

    properties dictionary

    defaultValue

    Optional. overwrite existing properties that have this value

  • Removes a previously registered super property.

    As an alternative to clearing all properties, unregistering specific super properties prevents them from being recorded on future events. This operation does not affect the value of other super properties. Any property name that is not registered is ignored. Note that after removing a super property, events will show the attribute as having the value undefined in Mixpanel until a new value is registered.

    Declaration

    Swift

    open func unregisterSuperProperty(_ propertyName: String)

    Parameters

    propertyName

    array of property name strings to remove

  • Convenience method to set a single group the user belongs to.

    Declaration

    Swift

    open func setGroup(groupKey: String, groupID: MixpanelType)

    Parameters

    groupKey

    The property name associated with this group type (must already have been set up).

    groupID

    The group the user belongs to.

  • Set the groups this user belongs to.

    Declaration

    Swift

    open func setGroup(groupKey: String, groupIDs: [MixpanelType])

    Parameters

    groupKey

    The property name associated with this group type (must already have been set up).

    groupIDs

    The list of groups the user belongs to.

  • Add a group to this user’s membership for a particular group key

    Declaration

    Swift

    open func addGroup(groupKey: String, groupID: MixpanelType)

    Parameters

    groupKey

    The property name associated with this group type (must already have been set up).

    groupID

    The new group the user belongs to.

  • Remove a group from this user’s membership for a particular group key

    Declaration

    Swift

    open func removeGroup(groupKey: String, groupID: MixpanelType)

    Parameters

    groupKey

    The property name associated with this group type (must already have been set up).

    groupID

    The group value to remove.

  • Opt out tracking.

    This method is used to opt out tracking. This causes all events and people request no longer to be sent back to the Mixpanel server.

    Declaration

    Swift

    open func optOutTracking()
  • Opt in tracking.

    Use this method to opt in an already opted out user from tracking. People updates and track calls will be sent to Mixpanel after using this method.

    This method will internally track an opt in event to your project.

    Declaration

    Swift

    open func optInTracking(distinctId: String? = nil, properties: Properties? = nil)

    Parameters

    distintId

    an optional string to use as the distinct ID for events

    properties

    an optional properties dictionary that could be passed to add properties to the opt-in event that is sent to Mixpanel

  • Returns if the current user has opted out tracking.

    Declaration

    Swift

    open func hasOptedOutTracking() -> Bool

    Return Value

    the current super opted out tracking status

A/B Testing

  • Join any experiments (A/B tests) that are available for the current user.

    Mixpanel will check for A/B tests automatically when your app enters the foreground. Call this method if you would like to to check for, and join, any experiments are newly available for the current user.

    Declaration

    Swift

    open func joinExperiments(callback: (() -> Void)? = nil)

    Parameters

    callback

    Optional callback for after the experiments have been loaded and applied

In App Notifications

  • Shows a notification if one is available.

    Note

    You do not need to call this method on the main thread.

    Declaration

    Swift

    open func showNotification()
  • Shows a notification with the given type if one is available.

    Note

    You do not need to call this method on the main thread.

    Declaration

    Swift

    open func showNotification(type: String)

    Parameters

    type

    The type of notification to show, either “mini” or “takeover”

  • Shows a notification with the given ID

    Note

    You do not need to call this method on the main thread.

    Declaration

    Swift

    open func showNotification(ID: Int)

    Parameters

    ID

    The notification ID you want to present

  • Returns the payload of a notification if available

    Note

    You do not need to call this method on the main thread.

    Declaration

    Swift

    open func fetchNotificationPayload(completion: @escaping ([String : AnyObject]?) -> Void)