How can I use the query engage api via python to get a user disticnt id by email?
Hi Yaniv K., Please stand by as I search the Mixpanel knowledge base for an AI-powered answer. I normally take about 15 seconds or so. In case AI cannot answer your question, feel free to reach out to Mixpanel support at mixpanel.com/get-support. If you have follow-up questions for this thread, please tag me again with your question: @kapa.ai <follow-up question> 👋
Feel free to ask a follow-up question once I've answered by mentioning me with @kapa.ai
To use the Query Engage API via Python to get a user's distinct ID by email, you can utilize the query_engage()
function from the mixpanel-utils library. Here's how you can do it:LBKSimport mputilsLBKS# Set up your Mixpanel credentials mputils.setup('YOUR_API_SECRET')LBKS# Query the Engage API params = { 'where': 'properties["$email"] == "user@[example.com](http://example.com)"', 'output_properties': ['$distinct_id'] }LBKSresults = mputils.query_engage(params)LBKSif results: distinct_id = results[0]['$distinct_id'] print(f"Distinct ID: {distinct_id}") else: print("No user found with that email.")LBKS
This code snippet does the following:1. It uses the mputils.setup()
function to authenticate with your Mixpanel project.
2. It sets up the query parameters to filter for the specific email and request only the $distinct_id
property.
3. It calls the query_engage()
function with these parameters.
4. If a result is found, it extracts the distinct ID from the first (and likely only) result.Note that you need to replace 'YOUR_API_SECRET' with your actual Mixpanel API secret, and 'user@example.com' with the email you're searching for.This approach uses the mixpanel-utils library, which provides a convenient wrapper around the Mixpanel API. The underlying API being used is the Engage API, as described in the Query API - Engage documentation.Remember that the distinct ID returned will be the same as the one used when tracking events for this user, as mentioned in the Managing User Identity documentation.Mixpanel-utils Module
kapa.ai is still learning and improving, please let me know how I did by reacting below.
ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.
try again this is not a real lib mputils
Let me check the knowledge base for you again. One moment! 📚
I apologize for the confusion in my previous response. You're right, and I made a mistake by referring to a library that doesn't exist. Let me provide you with the correct information based on the actual Mixpanel Python SDK and API.To query the Engage API using Python to get a user's distinct ID by email, you can use the requests library to make an HTTP POST request to the Mixpanel Engage API. Here's how you can do it:LBKSimport requests import jsonLBKS# Your Mixpanel API credentials API_SECRET = "YOUR_API_SECRET" PROJECT_ID = "YOUR_PROJECT_ID"LBKS# The email you want to search for email_to_search = "user@[example.com](http://example.com)"LBKS# Engage API endpoint url = "[https://mixpanel.com/api/2.0/engage](https://mixpanel.com/api/2.0/engage)"LBKS# Query parameters params = { "project_id": PROJECT_ID, "where": f'properties["$email"] == "{email_to_search}"', "output_properties": '["$distinct_id"]' }LBKS# Make the API request response = [requests.post](http://requests.post)(url, auth=(API_SECRET, ''), params=params)LBKSif response.status_code == 200: data = response.json() if data["results"]: distinct_id = data["results"][0]["$distinct_id"] print(f"Distinct ID: {distinct_id}") else: print("No user found with that email.") else: print(f"Error: {response.status_code}, {response.text}")LBKS
This code does the following:1. It sets up the API credentials and the email to search for.
2. It defines the Engage API endpoint URL.
3. It sets up the query parameters, including the project ID, a where clause to filter by email, and specifies that we only want the $distinct_id in the output.
4. It makes a POST request to the API using the requests library.
5. If successful, it parses the JSON response and extracts the distinct ID.Make sure to replace 'YOUR_API_SECRET' and 'YOUR_PROJECT_ID' with your actual Mixpanel API secret and project ID.This approach directly uses the Mixpanel Engage API as described in the Query API - Engage documentation.Exporting Profiles documentationRemember that the distinct ID returned will be the same as the one used when tracking events for this user, as mentioned in the Managing User Identity documentation.
kapa.ai is still learning and improving, please let me know how I did by reacting below.
ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.