Getting Started with Mixpanel SDKs for iOS (Swift)

To get started with Mixpanel's Swift library, please refer to our comprehensive Quickstart Guide. The Full API Reference, Library Source Code, and an Example Application are all documented in our GitHub repo.

Installing the Mixpanel Library

You can install the Swift library using CocoaPods, Carthage, or Swift Package Manager. To use CocoaPods, follow these steps:

  • Install CocoaPods by typing the following command in your terminal:

`

gem install cocoapods

`

  • After installing CocoaPods, create a local CocoaPods spec mirror by running the following command:

`

pod setup

`

  • Navigate to your Xcode project directory and create a Podfile by running the following command:

`

pod init

`

  • Open the Podfile that was generated and add the Mixpanel library to your dependencies:

`swift

target 'MyApp' do

pod 'Mixpanel-swift'

end

`

  • Install the Mixpanel library and create a new Xcode workspace by running the following command in the Xcode project directory:

`

pod install

`

Initializing Mixpanel

After installing the library, import Mixpanel into your AppDelegate.swift file and initialize Mixpanel within the application:didFinishLaunchingWithOptions: method using your project token.

`swift

import Mixpanel

func application(_ application: UIApplication,

didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// Initialize Mixpanel with your project token

Mixpanel.initialize(token: "YOUR_PROJECT_TOKEN",

trackAutomaticEvents: false)

}

`

Customizing the Mixpanel Instance

The Mixpanel instance can be customized with different configurations. The .initialize() method accepts arguments that set some configurations on your instance upon initialization.

`swift

import Mixpanel

func application(_ application: UIApplication,

didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// Set the instance name to "currentInstance" at initialization

Mixpanel.initialize(token: "YOUR_PROJECT_TOKEN",

trackAutomaticEvents: false,

instanceName: "currentInstance")

// Enable debug log after initialization

Mixpanel.mainInstance().loggingEnabled = true

}

`

Sending Events

Use .track(event:properties:) to send an event by providing the event name and any event properties. This will trigger a request to the /track API endpoint to ingest the event into your project.

`swift

import Mixpanel

func trackSomeEvent() {

// Track "some_event" with "plan" event prop set to "Premium"

Mixpanel.mainInstance().track(event: "some_event",

properties: ["Plan": "Premium"])

}

`

Timing Events

You can track the time it took for an action to occur, such as an image upload or a comment post, using .time(event:). This will mark the “start” of your action, which you can then finish with a track call. The time duration is then recorded in the “Duration” property.

`swift

import Mixpanel

func trackImageUpload() {

// Start the timer for the event "Image Upload"

Mixpanel.mainInstance().time(event: "Image Upload")

// 20 seconds later...

// Track "Image Upload" event with "Duration" event prop set to 20

Mixpanel.mainInstance().track(event: "Image Upload")

}

`

Flushing Events

To preserve battery life and customer bandwidth, the Mixpanel library doesn’t send the events you record immediately. Instead, it sends batches to the Mixpanel servers every 60 seconds while your application is running, as well as when the application transitions to the background.

Call .flush() manually if you want to force a flush at a particular moment.

`swift

import Mixpanel

func flushEvents() {

// Flush batched events for ingestion immediately

Mixpanel.mainInstance().flush()

}

`

Flush Interval

You can update the default flush interval from 60 seconds to another interval using flushInterval.

`swift

import Mixpanel

func setFlushInterval() {

// Set the flush interval to every 120 seconds

Mixpanel.mainInstance().flushInterval = 120

}

`

Importing Historical Events

The Swift SDK is a tracking SDK designed for real-time tracking in a client-side environment. Calling .track(event:properties:) triggers a request to our /track API endpoint, which will validate for events with a timestamp that is within the last 5 days of the request. Events older than 5 days will not be ingested.

For bulk import of historical events older than 5 days, we recommend using the Python SDK and mixpanel-utils module, which both leverage the /import API for event ingestion.

Setting Super Properties

Super properties are global event properties that you define once and apply to all events. To register super properties, call .registerSuperProperties(_).

`swift

import Mixpanel

func setSuperProperty() {

// Register a "name" super property

Mixpanel.mainInstance().registerSuperProperties(["name": "Sam"])

// Track "some_event"

// "name" is automatically added as an event prop

Mixpanel.mainInstance().track(event: "some_event")

}

`

Note

As creating properties involves an async operation to local storage, if you will create multiple properties at once, it’s best to send a single call to the register function with all properties at once, to avoid possible issues with race conditions overwriting each other.