Cloudflare’s robust infrastructure provides many useful metrics for website performance and security. Accessing these metrics via the Cloudflare API can provide more flexibility than the user interface, especially for developers who want to automate monitoring and store data for long-term analysis.
This article will guide you through the steps needed to authenticate using the Cloudflare API, extract essential metrics from the Analytics API, and visualize the data using Python. We’ll cover how to fetch and parse data such as pageviews, encrypted bytes, and cache usage, and plot relevant charts for easy analysis.
Fetching Account Information
Before making any API calls to Cloudflare, we need to authenticate using two critical pieces of information: your email address associated with the Cloudflare account and an API key (commonly the Global API key). By including these in the request headers, we can retrieve details such as the website’s Zone ID, which will be used in later steps.
Code Example: Fetching Account Information
pythonCopy codeimport requests
headers = {
'X-Auth-Email': '<your-email-address>',
'X-Auth-Key': '<Global API key>',
'Content-Type': 'application/json'
}
response = requests.request(
'GET',
'https://api.cloudflare.com/client/v4/zones',
headers=headers
)
data = response.json()
This code sends a request to the Cloudflare API, retrieving the list of zones in your account. The Zone ID
from this response will be used for making future requests to the Analytics API.
Making the Request to the Analytics API
The next step involves querying Cloudflare’s Analytics API, which now supports GraphQL queries to gather website performance metrics. We will request metrics such as pageviews, request counts, and cached data for a given time period (up to three days).
Code Example: Querying Analytics API
pythonCopy codeimport requests
headers = {
'X-Auth-Email': '<your-email-address>',
'X-Auth-Key': '<Global API key>',
'Content-Type': 'application/json'
}
data = """{
viewer {
zones(filter: {zoneTag: "<your-zone-id>"}) {
httpRequests1hGroups(limit: 100, filter: {datetime_geq: "2021-10-27T22:00:00Z", datetime_lt: "2021-10-28T20:02:00Z"}) {
sum {
pageViews
bytes
cachedBytes
requests
}
}
}
}
}"""
response = requests.request(
'POST',
'https://api.cloudflare.com/client/v4/graphql',
headers=headers,
json={'query': data}
)
result = response.json()
This request fetches metrics for a specific period, including total pageviews, the amount of data served, and cache statistics. You can also adjust the time range and filters as needed.
Parsing the Response Data
Once the response is received, the relevant data can be extracted and analyzed. Below is an example of how to parse metrics such as pageviews, requests, and cache information from the API response:
pythonCopy codepageviews = result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["pageViews"]
requests_cf = result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["requests"]
cached_bytes = result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["cachedBytes"]
These values can now be used to track website performance and optimize settings like caching policies.
Visualizing the Data
To make the insights easier to interpret, we can plot charts using Python’s matplotlib
library. Below are a few examples of visualizations based on the data obtained.
Response Status Codes
pythonCopy codeimport matplotlib.pyplot as plt
status_codes = [str(x["edgeResponseStatus"]) for x in result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["responseStatusMap"]]
requests = [x["requests"] for x in result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["responseStatusMap"]]
plt.bar(status_codes, requests)
plt.xlabel('Response Status Codes')
plt.ylabel('Number of Requests')
plt.title('Response Status Code Distribution')
plt.show()
Browser Distribution
pythonCopy codebrowser = [str(x["uaBrowserFamily"]) for x in result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["browserMap"]]
pageviews = [x["pageViews"] for x in result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["browserMap"]]
plt.bar(browser, pageviews)
plt.xlabel('Browser')
plt.ylabel('Page Views')
plt.title('Browser Distribution for Page Views')
plt.show()
Cache Performance
pythonCopy codebytes_served = result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["bytes"]
cached_bytes = result["data"]["viewer"]["zones"][0]["httpRequests1hGroups"][0]["sum"]["cachedBytes"]
labels = ['Total Bytes', 'Cached Bytes']
sizes = [bytes_served, cached_bytes]
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('Cache Performance: Cached vs. Total Bytes')
plt.show()
Conclusion
Cloudflare’s API offers a comprehensive way to track performance metrics and security insights for your website. By leveraging Python, you can automate the retrieval and analysis of this data, making it easier to monitor trends and improve performance. Whether you’re interested in pageviews, cache statistics, or browser breakdowns, Cloudflare’s GraphQL-powered Analytics API provides a wealth of information that can be used to optimize your site’s performance.
We hope this guide helps you get started with using Cloudflare’s API and Python for deeper website analytics!