Query API
Insights
Query saved Insights reports
GET
/
api
/
query
/
insights
Insights
curl --request GET \
--url https://mixpanel.com/api/query/insightsimport requests
url = "https://mixpanel.com/api/query/insights"
response = requests.get(url)
print(response.text)const options = {method: 'GET'};
fetch('https://mixpanel.com/api/query/insights', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://mixpanel.com/api/query/insights",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://mixpanel.com/api/query/insights"
req, _ := http.NewRequest("GET", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("https://mixpanel.com/api/query/insights")
.asString();require 'uri'
require 'net/http'
url = URI("https://mixpanel.com/api/query/insights")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
response = http.request(request)
puts response.read_body{
"computed_at": "<string>",
"date_range": {
"from_date": "<string>",
"to_date": "<string>"
},
"headers": [
{}
],
"series": {}
}Query Insights
Retrieve data from your saved Insights reports programmatically.Query Saved Report
integer
required
Your Mixpanel project ID
integer
The workspace ID containing the report
integer
required
The ID of your Insights report. Can be found from the URL:
https://mixpanel.com/project/<PROJECT_ID>/view/<WORKSPACE_ID>/app/boards#id=12345&editor-card-id="report-<BOOKMARK_ID>"Example Request
curl "https://mixpanel.com/api/query/insights?project_id=123&bookmark_id=456" \
-u SERVICE_ACCOUNT_USERNAME:SERVICE_ACCOUNT_SECRET
import requests
from requests.auth import HTTPBasicAuth
response = requests.get(
'https://mixpanel.com/api/query/insights',
auth=HTTPBasicAuth('SERVICE_ACCOUNT_USERNAME', 'SERVICE_ACCOUNT_SECRET'),
params={
'project_id': 123,
'bookmark_id': 456
}
)
data = response.json()
print(data)
const username = 'SERVICE_ACCOUNT_USERNAME';
const password = 'SERVICE_ACCOUNT_SECRET';
const auth = 'Basic ' + btoa(username + ':' + password);
fetch('https://mixpanel.com/api/query/insights?project_id=123&bookmark_id=456', {
headers: {
'Authorization': auth
}
})
.then(response => response.json())
.then(data => console.log(data));
Response
string
Timestamp when the query was computed
object
array
Array explaining the nested keys in the
series objectobject
Maps event names to date/value pairs. Each key is an event name, and the value is an object with dates as keys and event counts as values.
{
"computed_at": "2020-09-21T16:35:41.252314+00:00",
"date_range": {
"from_date": "2020-08-31T00:00:00-07:00",
"to_date": "2020-09-12T23:59:59.999000-07:00"
},
"headers": ["$event"],
"series": {
"Logged in": {
"2020-08-31T00:00:00-07:00": 9852,
"2020-09-07T00:00:00-07:00": 4325
},
"Viewed page": {
"2020-08-31T00:00:00-07:00": 10246,
"2020-09-07T00:00:00-07:00": 11432
}
}
}
Use Cases
Daily Metrics Dashboard
import requests
from requests.auth import HTTPBasicAuth
from datetime import datetime
def get_daily_metrics(project_id, bookmark_id):
"""Fetch daily event metrics from Insights"""
response = requests.get(
'https://mixpanel.com/api/query/insights',
auth=HTTPBasicAuth('USERNAME', 'SECRET'),
params={
'project_id': project_id,
'bookmark_id': bookmark_id
}
)
data = response.json()
# Process the series data
metrics = {}
for event_name, date_values in data['series'].items():
total = sum(date_values.values())
metrics[event_name] = {
'total': total,
'daily_breakdown': date_values
}
return metrics
# Usage
metrics = get_daily_metrics(123, 456)
print(f"Total Signups: {metrics['Signed Up']['total']}")
Trend Analysis
import requests
from requests.auth import HTTPBasicAuth
def analyze_trend(project_id, bookmark_id):
"""Calculate growth trends from Insights data"""
response = requests.get(
'https://mixpanel.com/api/query/insights',
auth=HTTPBasicAuth('USERNAME', 'SECRET'),
params={'project_id': project_id, 'bookmark_id': bookmark_id}
)
data = response.json()
for event_name, date_values in data['series'].items():
dates = sorted(date_values.keys())
values = [date_values[d] for d in dates]
if len(values) >= 2:
# Calculate week-over-week growth
current_week = sum(values[-7:])
previous_week = sum(values[-14:-7])
growth = ((current_week - previous_week) / previous_week) * 100
print(f"{event_name}: {growth:.1f}% WoW growth")
analyze_trend(123, 456)
Best Practices
Create dedicated reports for API queries
Create dedicated reports for API queries
Instead of querying general reports, create specific Insights reports optimized for API consumption with:
- Relevant date ranges
- Specific events needed
- Appropriate filters
Cache results appropriately
Cache results appropriately
Insights data doesn’t change frequently, so cache results:
import time
import json
def get_cached_insights(project_id, bookmark_id, cache_duration=3600):
cache_file = f'insights_{project_id}_{bookmark_id}.json'
# Check if cache exists and is fresh
try:
with open(cache_file, 'r') as f:
cache = json.load(f)
if time.time() - cache['timestamp'] < cache_duration:
return cache['data']
except FileNotFoundError:
pass
# Fetch fresh data
data = fetch_insights(project_id, bookmark_id)
# Save to cache
with open(cache_file, 'w') as f:
json.dump({
'timestamp': time.time(),
'data': data
}, f)
return data
Handle missing data gracefully
Handle missing data gracefully
Some dates might not have data:
series = data['series'].get('Event Name', {})
value = series.get('2024-01-15', 0) # Default to 0 if missing
Insights
curl --request GET \
--url https://mixpanel.com/api/query/insightsimport requests
url = "https://mixpanel.com/api/query/insights"
response = requests.get(url)
print(response.text)const options = {method: 'GET'};
fetch('https://mixpanel.com/api/query/insights', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://mixpanel.com/api/query/insights",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://mixpanel.com/api/query/insights"
req, _ := http.NewRequest("GET", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("https://mixpanel.com/api/query/insights")
.asString();require 'uri'
require 'net/http'
url = URI("https://mixpanel.com/api/query/insights")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
response = http.request(request)
puts response.read_body{
"computed_at": "<string>",
"date_range": {
"from_date": "<string>",
"to_date": "<string>"
},
"headers": [
{}
],
"series": {}
}