Installation
Initialize
from mixpanel import Mixpanel
mp = Mixpanel("YOUR_PROJECT_TOKEN")
Configuration
import mixpanel
mp = mixpanel.Mixpanel(
"YOUR_PROJECT_TOKEN",
consumer=mixpanel.Consumer(retry_limit=2)
)
Track Events
You must provide a distinct_id for all events.
mp.track('12345', 'Purchase', {
'product': 'Premium Subscription',
'amount': 49.99,
'currency': 'USD',
'ip': '192.168.0.1'
})
Import Historical Events
For events older than 5 days:
mp.import_data(
api_key='', # Deprecated but required
distinct_id='12345',
event_name='Old Purchase',
timestamp=1634528073,
properties={'amount': 100},
api_secret='YOUR_API_SECRET'
)
User Profiles
mp.people_set('12345', {
'name': 'John Doe',
'$email': '[email protected]',
'plan': 'Premium'
}, meta={
'$ignore_time': True, # Don't update $last_seen
'$ip': 0 # Disable geolocation
})
people_set_once()
people_increment()
people_append()
people_union()
mp.people_set_once('12345', {
'First Purchase': '2024-01-01'
}, meta={'$ip': 0})
mp.people_increment('12345', {'login_count': 1})
mp.people_increment('12345', {'age': 1})
mp.people_append('12345', {
'transactions': 'purchase_123'
}, meta={'$ip': 0})
mp.people_union('12345', {
'skills': ['Python', 'Django']
}, meta={'$ip': 0})
Group Analytics
Send Events
mp.track('12345', 'Feature Used', {
'company': 'Acme Inc',
'feature': 'Reports'
})
Set Group Properties
mp.group_set('company', 'Acme Inc', {
'name': 'Acme Inc',
'industry': 'Technology',
'employees': 500
})
group_set_once()
group_unset()
group_union()
group_remove()
mp.group_set_once('company', 'Acme Inc', {
'founded': '2010-01-01'
})
mp.group_unset('company', 'Acme Inc', [
'temp_property'
])
mp.group_union('company', 'Acme Inc', {
'features': ['Reports', 'Analytics']
})
mp.group_remove('company', 'Acme Inc', {
'features': ['Beta Feature']
})
Privacy Controls
EU Data Residency
from mixpanel import Mixpanel
import mixpanel
mp = Mixpanel(
"YOUR_PROJECT_TOKEN",
consumer=mixpanel.Consumer(api_host="api-eu.mixpanel.com")
)
India Data Residency
mp = Mixpanel(
"YOUR_PROJECT_TOKEN",
consumer=mixpanel.Consumer(api_host="api-in.mixpanel.com")
)
Geolocation
The Python SDK sets IP to 0 by default, meaning geolocation is not tracked unless explicitly included.
# Explicitly set IP for geolocation
mp.track('12345', 'event', {
'ip': '192.168.0.1'
})
All server-side calls originate from your server’s IP. Set $ip to 0 to disable geolocation or provide user’s actual IP.
- You manage
distinct_id yourself
- No automatic identity management
- IP defaults to
0 (no geolocation)
- Use
.import_data() for historical events
- Designed for scripting and batch operations
Resources