Installation
CocoaPods
Carthage
Swift Package Manager
Install CocoaPods
gem install cocoapods
pod setup
Add Mixpanel
target 'MyApp' do
pod 'Mixpanel'
end
Add to your Cartfile:github "mixpanel/mixpanel-iphone"
- In Xcode: File > Add Packages…
- Enter:
https://github.com/mixpanel/mixpanel-iphone
- Select version v4.0.0+
Initialize
In AppDelegate.m:
#import "Mixpanel/Mixpanel.h"
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Mixpanel sharedInstanceWithToken:@"YOUR_PROJECT_TOKEN"
trackAutomaticEvents:NO];
return YES;
}
Access the instance:
Mixpanel *mixpanel = [Mixpanel sharedInstance];
Track Events
Mixpanel *mixpanel = [Mixpanel sharedInstance];
[mixpanel track:@"Video Watched"
properties:@{
@"video_title": @"iOS Development",
@"duration": @120,
@"quality": @"HD"
}];
Timing Events
[mixpanel timeEvent:@"Image Upload"];
// 20 seconds later
[self uploadImageWithSuccessHandler:^{
[mixpanel track:@"Image Upload"];
}];
Flush Events
// Flush immediately
[mixpanel flush];
// Adjust flush interval (seconds)
[Mixpanel sharedInstance].flushInterval = 120;
Identify Users
Mixpanel *mixpanel = [Mixpanel sharedInstance];
[mixpanel track:@"sign in"];
[mixpanel identify:@"12345"];
Reset on Logout
[mixpanel track:@"log out"];
[mixpanel reset];
User Profiles
Mixpanel *mixpanel = [Mixpanel sharedInstance];
[mixpanel identify:@"12345"];
[mixpanel.people set:@{
@"Plan": @"Premium",
@"$email": @"[email protected]",
@"$name": @"John Doe"
}];
setOnce:
increment:by:
append:
union:
[mixpanel.people setOnce:@{
@"First Login": [NSDate date]
}];
[mixpanel.people increment:@"age" by:@1];
[mixpanel.people increment:@"login_count" by:@1];
[mixpanel.people append:@{
@"roles": @"admin"
}];
// Add without duplicates
[mixpanel.people union:@{
@"skills": @"iOS Development"
}];
Super Properties
Mixpanel *mixpanel = [Mixpanel sharedInstance];
[mixpanel registerSuperProperties:@{
@"App Version": @"2.0.1",
@"Platform": @"iOS"
}];
// Register without overwriting
[mixpanel registerSuperPropertiesOnce:@{
@"First Launch": [NSDate date]
}];
Group Analytics
Mixpanel *mixpanel = [Mixpanel sharedInstance];
// Assign to group
[mixpanel setGroup:@"company" groupID:@"Acme Inc"];
// Track event
[mixpanel track:@"feature_used"];
// Set group properties
[[mixpanel getGroup:@"company" groupID:@"Acme Inc"] set:@{
@"industry": @"Technology",
@"employees": @500
}];
Privacy Controls
Opt Out
[mixpanel optOutTracking];
// Initialize with opt-out
Mixpanel *mixpanel = [Mixpanel sharedInstanceWithToken:@"YOUR_PROJECT_TOKEN"
trackAutomaticEvents:NO
optOutTrackingByDefault:YES];
EU/India Data Residency
Mixpanel *mixpanel = [Mixpanel sharedInstance];
// EU
mixpanel.serverURL = @"https://api-eu.mixpanel.com";
// India
mixpanel.serverURL = @"https://api-in.mixpanel.com";
Disable Geolocation
[Mixpanel sharedInstance].useIPAddressForGeoLocation = NO;
Debug Mode
[Mixpanel sharedInstance].enableLogging = YES;
Or add to build settings Preprocessor Macros:
MIXPANEL_DEBUG=1
MIXPANEL_ERROR=1
- Events flush every 60 seconds or on app background
- Requires iOS 9.0+
- Uses IFV (Identifier for Vendor) for distinct_id
- Super properties persist in local storage
- Supports push notification tracking
Since v3.6.2, Mixpanel no longer uses IDFA by default.
Resources