The stats API gives your server the same numbers as the DataDad dashboard: visitors, page views, visits, bounce rate, visit duration and link clicks. Use it to show statistics in your own product, or to copy them into another tool.
Create an API key
- Open the app in DataDad and select Settings.
- Under Stats API, enter a name for the key, such as
Production server. - Select Create API key.
- Copy the key. DataDad shows it only once.
Each key reads one app. An app can have up to 10 keys. To stop a key, select Revoke next to it. Requests with a revoked key fail at once.
Make the first request
Send the key in the Authorization header. This request gets the totals for the last 30 days:
curl "https://data.dad/api/v1/stats/summary?range=30d" \
-H "Authorization: Bearer $DATADAD_API_KEY"const response = await fetch('https://data.dad/api/v1/stats/summary?range=30d', {
headers: { Authorization: `Bearer ${process.env.DATADAD_API_KEY}` },
});
const stats = await response.json();
console.log(stats.visitors, stats.pageviews);import os, requests
stats = requests.get(
"https://data.dad/api/v1/stats/summary",
params={"range": "30d"},
headers={"Authorization": f"Bearer {os.environ['DATADAD_API_KEY']}"},
timeout=30,
).json()
print(stats["visitors"], stats["pageviews"])$context = stream_context_create(['http' => [
'header' => 'Authorization: Bearer ' . getenv('DATADAD_API_KEY'),
]]);
$stats = json_decode(file_get_contents('https://data.dad/api/v1/stats/summary?range=30d', false, $context), true);
echo $stats['visitors'];The response:
{
"tenant": null,
"range": { "from": 1789430400, "to": 1792022400, "previous_from": 1786838400, "previous_to": 1789430400 },
"visitors": 1840, "pageviews": 2610, "link_clicks": 912,
"visits": 2093, "bounce_rate": 0.71, "visit_duration": 38,
"previous": { "visitors": 1502, "pageviews": 2177, "link_clicks": 701, "visits": 1730, "bounce_rate": 0.69, "visit_duration": 41 }
}
Choose a period
Every endpoint takes range. The default is 30d.
| Value | Period |
|---|---|
1h, 24h | The last hour or the last 24 hours |
7d, 30d, 90d | The last 7, 30 or 90 days |
1y, 3y | The last year or the last 3 years |
today, yesterday | The current or the previous UTC day |
wtd, mtd, ytd | The week, month or year to date |
For other dates, send from and to as YYYY-MM-DD instead of range. Both days are included. The period can be up to 3 years long. All days and times are UTC. Timestamps in responses are Unix seconds.
Errors
An error response has an HTTP status and a JSON body with a message and a code:
{ "error": "This API key is not valid.", "code": "unauthorized" }
| Status | Code | What to do |
|---|---|---|
| 400 | invalid_request | Correct the parameter that the message names. |
| 401 | unauthorized | Send a valid key that was not revoked. |
| 429 | rate_limited | Your app used its requests for this minute. Wait for the time in the Retry-After header. |
| 429 | busy | Many requests for your app are running now. Wait for the time in the Retry-After header. |
| 503 | unavailable | Try again after the time in Retry-After. |
Limits and caching
- Each app can get 60 new answers a minute. All keys of the app share this limit. An answer from the cache does not count.
- DataDad keeps each response for 60 seconds. The same request in that time gets the same answer. A period that ended before today is kept for one hour.
- Show your users numbers from your own cache when you can. Do not call the API on each page view of a public page.
Next steps
- See every endpoint in the stats API reference.
- Give each user of your platform their own statistics with per-user analytics.