Official Mixpanel library for Python providing server-side analytics tracking
—
Identity management functionality handles user identification across different stages of the user lifecycle. It enables linking anonymous users to identified users and merging user profiles when multiple identities refer to the same person.
Create one-way mappings between user identifiers to associate anonymous sessions with identified users.
def alias(alias_id: str, original: str, meta: dict = None):
"""
Creates an alias which Mixpanel will use to remap one id to another.
Parameters:
- alias_id (str): A distinct_id to be merged with the original distinct_id. Each alias can only map to one distinct_id.
- original (str): A distinct_id to be merged with alias_id.
- meta (dict, optional): Overrides Mixpanel special properties
Returns:
None
Note: This method always results in a synchronous HTTP request to Mixpanel servers, regardless of any custom consumer.
"""Usage Example:
from mixpanel import Mixpanel
mp = Mixpanel("YOUR_PROJECT_TOKEN")
# Link anonymous user to identified user after signup/login
# Anonymous user was tracked as "anonymous_12345"
# After signup, user gets permanent ID "user_67890"
mp.alias("user_67890", "anonymous_12345")
# Now all future events for "user_67890" will be associated with
# the historical events from "anonymous_12345"Merge two user profiles when they represent the same person, combining all historical data and properties.
def merge(api_key: str, distinct_id1: str, distinct_id2: str, meta: dict = None, api_secret: str = None):
"""
Merges the two given distinct_ids.
Parameters:
- api_key (str): (DEPRECATED) Your Mixpanel project's API key
- distinct_id1 (str): The first distinct_id to merge
- distinct_id2 (str): The second (other) distinct_id to merge
- meta (dict, optional): Overrides Mixpanel special properties
- api_secret (str, optional): Your Mixpanel project's API secret (recommended over api_key)
Returns:
None
Note: The api_key parameter is deprecated. Use api_secret instead. This operation requires API credentials and combines all profile data from both identities.
"""Usage Example:
from mixpanel import Mixpanel
mp = Mixpanel("YOUR_PROJECT_TOKEN")
# Merge two user profiles that represent the same person
# This might happen when a user signs up with different methods
# (email vs social login) or when duplicate accounts are discovered
mp.merge(
api_key="DEPRECATED_API_KEY", # Will be removed in future versions
api_secret="YOUR_API_SECRET", # Recommended approach
distinct_id1="user_email_123",
distinct_id2="user_social_456"
)
# After merging, both IDs will reference the same combined profile
# All events and properties from both profiles are preservedThis is the most common identity management scenario where anonymous users become identified after registration or login.
from mixpanel import Mixpanel
import uuid
mp = Mixpanel("YOUR_PROJECT_TOKEN")
# 1. Track anonymous user events
anonymous_id = str(uuid.uuid4())
mp.track(anonymous_id, "page_viewed", {"page": "homepage"})
mp.track(anonymous_id, "button_clicked", {"button": "signup"})
# 2. User signs up and gets a permanent ID
permanent_id = "user_12345"
# 3. Create alias to link anonymous behavior to permanent identity
mp.alias(permanent_id, anonymous_id)
# 4. Continue tracking with permanent ID
mp.track(permanent_id, "signup_completed", {"method": "email"})
mp.people_set(permanent_id, {
"$first_name": "John",
"$email": "john@example.com"
})When duplicate profiles need to be merged into a single identity.
from mixpanel import Mixpanel
mp = Mixpanel("YOUR_PROJECT_TOKEN")
# Scenario: User has two accounts that need to be consolidated
primary_id = "user_primary_123"
duplicate_id = "user_duplicate_456"
# Set properties on both profiles (simulating existing data)
mp.people_set(primary_id, {"plan": "premium", "signup_date": "2024-01-01"})
mp.people_set(duplicate_id, {"phone": "+1234567890", "verified": True})
# Merge the profiles - all data from both will be combined
mp.merge(
api_secret="YOUR_API_SECRET",
distinct_id1=primary_id,
distinct_id2=duplicate_id
)
# Continue using either ID - they now reference the same profile
mp.track(primary_id, "profile_merged", {"merge_reason": "duplicate_cleanup"})Identity operations may fail due to network issues or API restrictions:
from mixpanel import Mixpanel, MixpanelException
mp = Mixpanel("YOUR_PROJECT_TOKEN")
try:
mp.alias("new_user_123", "anonymous_456")
except MixpanelException as e:
print(f"Alias operation failed: {e}")
# Implement retry logic or fallback handling
try:
mp.merge(
api_secret="YOUR_API_SECRET",
distinct_id1="user_1",
distinct_id2="user_2"
)
except MixpanelException as e:
print(f"Merge operation failed: {e}")
# Handle merge failure appropriatelyalias method always makes synchronous HTTP requests regardless of the consumer configurationmerge operation requires API credentials (secret or key)Install with Tessl CLI
npx tessl i tessl/pypi-mixpanel