Aaron Presley


Integrating Mixpanel into a Swift iOS Project April 24, 2015

Mixpanel is a great service, but their iOS Integration page doesn't even mention the word swift, let alone how to get it integrated into a new project. There are a few resources on how to get Swift and Objective-C to work together, but it seems really cumbersome and annoying.

Finally, I came across this gist by Sam Soffes. It's very basic, but works really well. I've made a few small updates to add a few more properties to track.

Note: Sam seems to continue to update / make improvements on his original script. I've moved the current version to my own gist to prevent confusion. Please check out his gist for the latest code.

Step 1: Mixpanel.swift

Copy-paste the code from this gist and put it into a file in your project. I named mine Mixpanel.swift.

Step 2: Enter Your Auth Token

From your Mixpanel account, click the gear icon in the bottom-left of the screen. In the following popup you'll see your Token. Copy-paste that into your Mixpanel.swift file (at the bottom).

Step 3: Track Events

That's it! You can now track any even in your iOS app with the following code:

mixpanel.track("Bar Visit", parameters: [
    "Bar Name": bar.name,
])