iPhone
Updated Dec. 7, 2011If you want to track user behavior on your iPhone or iPad application, the first thing to do is download the latest iPhone library or clone the git repository.
The repository has four directories:
- MPLib - The source code for the Mixpanel API and its dependencies
- MixpanelEventSample - A sample application that tracks events using Mixpanel.
- Docs - Documentation for the project.
Contents
Setup
Adding Mixpanel to your Xcode project is as easy as:
- Drag and drop the MPLib folder into your project.
- Check the “Copy items into destination Group’s folder” and select 'Recursively create groups for any added folders'.
Once you've done that, you're ready to go. Your project should look something like this:

Initializing Mixpanel
The next thing you need to do in order to use Mixpanel is to initialize the MixpanelAPI object. We recommend doing this in applicationDidFinishLaunching: of application:didFinishLaunchingWithOptions in your Application delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)
launchOptions {
// Override point for customization after application launch.
mixpanel = [MixpanelAPI sharedAPIWithToken:MIXPANEL_TOKEN];
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
Tracking events
After initializing the MixpanelAPI object, you are ready to track events. This can be done with the following code snippet:
- (IBAction) registerEvent:(id)sender {
MixpanelAPI *mixpanel = [MixpanelAPI sharedAPI];
[mixpanel track:@"Player Create"];
}
If you want to add properties to the event you can do the following:
- (IBAction) registerEvent:(id)sender {
MixpanelAPI *mixpanel = [MixpanelAPI sharedAPI];
[mixpanel track:@"Player Create"
properties:[NSDictionary dictionaryWithObjectsAndKeys:
[genderControl titleForSegmentAtIndex:genderControl.selectedSegmentIndex], @"gender",
[weaponControl titleForSegmentAtIndex:weaponControl.selectedSegmentIndex], @"weapon", nil
]
];
}
This code snippet is taken from the MixpanelEventSample Project. Line 15-20 of the MixpanelEventSampleViewController.m file.
Storing data with super properties
Sometimes you know something about a user that you want to send with every event - perhaps the app version the user is using, or the gender of the user. Super Properties make this really easy - they are global, persistent properties that are attached to every event request. You can learn more about them here.
If you want to register a super property for your current user, you can do it as follows:
MixpanelAPI *mixpanel = [MixpanelAPI sharedAPI];
[mixpanel registerSuperProperties:
[NSDictionary dictionaryWithObject:@"Paid" forKey:@"User Type"]
eventType:kMPLibEventTypeAll];
This call will register the “User Type” property with a value of @”Paid” for the current user.
If the “User Type” property was codeviously registered, its value will be overwritten. If you do not want to overwrite existing values of a super property you can use the following call:
MixpanelAPI *mixpanel = [MixpanelAPI sharedAPI];
[mixpanel registerSuperPropertiesOnce:
[NSDictionary dictionaryWithObject:@"Paid" forKey:@"User Type"]
eventType:kMPLibEventTypeAll];
After this call, the value of the “User Type” property will be written only if it did not exist. This is useful when you don't want to overwrite a property - such as setting a unique ID or adding someone to a test cohort.
Technical overview
At this point you may be wondering how this thing works. The basic workflow is this:
- You track some events
- We queue them up, and send them as a batch after a (configurable) time interval
- The user closes the app, and we flush events again
API Specification
So far we've just looked at the most important functions provided by the iPhone library. Here's some more comprehensive information: Mixpanel iPhone API.
