Can you give me full example of usage Raw event export API in python?
Hi Sava M., 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
I'll provide you with a full example of using the Raw Event Export API in Python based on the knowledge sources.
Using the Mixpanel Utils Module
The recommended approach is to use the mixpanel-utils Python module, which provides a convenient wrapper around the Raw Export API:
```
from mixpanel_utils import MixpanelUtils
mputils = MixpanelUtils( 'ServiceAccountSecretHere', token='ProjectTokenHere', service_account_username='my-user.12345.mp-service-account', project_id=1695321, )
mputils.export_events( 'event_export.txt', { 'from_date': '2016-01-01', 'to_date': '2016-01-01', 'event': '["App Install"]' } )
More Advanced Example with Export and Import
Here's a more comprehensive example from the hot shard limits documentation that exports, transforms, and re-imports events:
```
import glob import gzip import json from mixpanel_utils import MixpanelUtils
SETTINGS = { "PROJECT_ID": "<REPLACE YOUR PROJECT ID> ", "TOKEN": "<REPLACE PROJECT TOKEN> ", "SA_USERNAME": "<REPLACE SERVICE ACCOUNT USERNAME> ", "SA_PASSWORD": "<REPLACE SERVICE ACCOUNT PASSWORD> ", "EU": False, # set to TRUE if your project is in the EU "EXPORT_FOLDER": "exported_files", # make sure to create the folder if it does not exist "FROM": "2023-09-01", "TO": "2023-09-15", }
mputils = MixpanelUtils( SETTINGS["SA_PASSWORD"], token=SETTINGS["TOKEN"], service_account_username=SETTINGS["SA_USERNAME"], eu=SETTINGS["EU"], project_id=SETTINGS["PROJECT_ID"] )
def flush_events(events): global mputils if(len(events) == 0): return False mputils.import_events(events, timezone_offset=0) return True
def transform_event(event): try: data = json.loads(event) data["event"] = data["properties"]["mp_original_event_name"] del data["properties"]["mp_original_event_name"] del data["properties"]["mp_original_distinct_id"] return data except: return False
mputils.export_events( f'{SETTINGS["EXPORT_FOLDER"]}/events.json', { "from_date": SETTINGS["FROM"], "to_date": SETTINGS["TO"], "event": '["$hotshard_events"]' }, add_gzip_header=True, request_per_day=True, raw_stream=True )
exported_files = glob.glob(f'{SETTINGS["EXPORT_FOLDER"]}/*.json.gz') for file_name in exported_files: events = [] event_queue_max = 50_000 with gzip.open(file_name, 'rt') as file: for line in file: event = transform_event(line) if(event == False): continue events.append(event) if(len(events) > = event_queue_max): flush_events(events) events = []
# Flush remaining events
if(len(events) > 0):
flush_events(events)
events = []
Using Direct API Calls
If you prefer to use the Raw Event Export API directly with curl or HTTP requests:
```
curl --request GET --url 'https://data.mixpanel.com/api/2.0/export?from_date=2024-07-23&to_date=2024-07-24&limit=100&event=sample_event' --header 'accept: text/plain' --header 'authorization: Basic YOUR_TOKEN'
LBKS**Important Notes:**
- The Raw Export API has a [rate limit of 60 queries per hour, 3 queries per second, and a maximum of 100 concurrent queries](https://developer.mixpanel.com/reference/raw-event-export)
- For EU projects, use `residency='eu'` when initializing MixpanelUtils
- The exported data is in JSONL format (one JSON object per line)
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.
