Skip to main content

Using Python and the Cloudflare API for Analytics Insights

|

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!


Daniel Dye

Daniel Dye is the President of NativeRank Inc., a premier digital marketing agency that has grown into a powerhouse of innovation under his leadership. With a career spanning decades in the digital marketing industry, Daniel has been instrumental in shaping the success of NativeRank and its impressive lineup of sub-brands, including MarineListings.com, LocalSEO.com, MarineManager.com, PowerSportsManager.com, NikoAI.com, and SearchEngineGuidelines.com. Before becoming President of NativeRank, Daniel served as the Executive Vice President at both NativeRank and LocalSEO for over 12 years. In these roles, he was responsible for maximizing operational performance and achieving the financial goals that set the foundation for the company’s sustained growth. His leadership has been pivotal in establishing NativeRank as a leader in the competitive digital marketing landscape. Daniel’s extensive experience includes his tenure as Vice President at GetAds, LLC, where he led digital marketing initiatives that delivered unprecedented performance. Earlier in his career, he co-founded Media Breakaway, LLC, demonstrating his entrepreneurial spirit and deep understanding of the digital marketing world. In addition to his executive experience, Daniel has a strong technical background. He began his career as a TAC 2 Noc Engineer at Qwest (now CenturyLink) and as a Human Interface Designer at 9MSN, where he honed his skills in user interface design and network operations. Daniel’s educational credentials are equally impressive. He holds an Executive MBA from the Quantic School of Business and Technology and has completed advanced studies in Architecture and Systems Engineering from MIT. His commitment to continuous learning is evident in his numerous certifications in Data Science, Machine Learning, and Digital Marketing from prestigious institutions like Columbia University, edX, and Microsoft. With a blend of executive leadership, technical expertise, and a relentless drive for innovation, Daniel Dye continues to propel NativeRank Inc. and its sub-brands to new heights, making a lasting impact in the digital marketing industry.

More Articles By Daniel Dye

Google’s AI Overviews are changing the SEO landscape. By providing instant answers directly in search results, users no longer need to visit individual websites to find information. While this innovation enhances the user experience, it has caused ripples across industries that rely on organic search traffic. A recent report by NerdWallet highlights how Google AI […]
The political landscape around TikTok is once again uncertain as former President Donald Trump’s potential return to the White House revives conversations about a ban on the app. TikTok, with its 170 million American users, has become a cornerstone of digital advertising and cultural trends. However, a renewed focus on its ties to China could […]
The 2024 election offered significant insights for advertisers, showcasing the importance of strategy, authenticity, and data-driven approaches in connecting with the public. The campaigns of Kamala Harris and Donald Trump highlighted key lessons that can help marketers refine their strategies and drive meaningful results. 1. Strategic Allocation of Budgets Kamala Harris’s campaign spent over $1 […]

Was this helpful?