Product
Updates to the Mailgun Stats API
We’re pleased to announce updates to our Stats API providing users the opportunity to capture more data about their emails. Read more...
PUBLISHED ON
We know Mailgun users want to know what’s going on with their email. Back in May, we made some significant updates to our dashboard allowing users to check the delivery performance of their email. Today, we’re pleased to announce updates to our Stats API providing users the opportunity to capture more data about their emails.
We’ve updated our stats API with new features that will help Mailgun users track the status of their emails at a more granular level. The new API can now return data in chronological order with a specified resolution. Monthly, daily and hourly resolutions are now supported.
We’ve also extended the tags API so it’s now possible to provide an optional description to a tag through the API. With this update, you can also list all tags.
The Stats API for each tag are now supported and show the number of unique clicks and opens.
Key benefits
With this update, Mailgun users will now be able to:
Request stats for a particular event and tag
Supply an optional description of a tag
View stats at a more granular level, including hourly resolution
Use case
We use the same API to display our main dashboard in the Mailgun control panel. You can read more about it here.
For example, if you want to draw a graph that we have on the dashboard page for a domain example.com here is an idea on how to get the data series:
import requests
def _get_stats(): """Call Mailgun API to get the domain stats.""" return requests.get( "https://api.mailgun.net/v3/example.com/stats/total", auth=("api", "YOUR_API_KEY"), params={"event": ["accepted", "delivered", "failed"], "duration": "30d"})
def get_series_for_graph(): """Returns the data series required for drawing a nice graph.""" # Make a request to the API. response = _get_stats()
# Prepare empty data series. series = {"Delivered": [], "Dropped": [], "Incoming": []}
# Iterate over all items in the response where # each item represents stats for a particular day. for item in response["stats"]: series["Delivered"].append(item["delivered"]["smtp"]) series["Dropped"].append(item["failed"]["permanent"]["total"]) series["Incoming"].append(item["accepted"]["incoming"])
return series
Additional Info
Check our out documentation and if you have a question, feel free to leave a comment!
https://documentation.mailgun.com/api-stats.html
https://documentation.mailgun.com/api-tags.html