Skip to main content

Installation

pip install mixpanel

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
})
mp.people_set_once('12345', {
    'First Purchase': '2024-01-01'
}, 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
})
mp.group_set_once('company', 'Acme Inc', {
    'founded': '2010-01-01'
})

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'
})

Platform Considerations

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

Build docs developers (and LLMs) love