Automate analytics reporting with Cloudflare Workers and email routing
In this tutorial, you will create a Cloudflare Worker ↗ that fetches analytics data about your account from Cloudflare's GraphQL Analytics API ↗. You will be able to view the account analytics data in your browser and receive a scheduled email report.
You will learn:
- How to create a Worker using the
c3
CLI. - How to fetch analytics data from Cloudflare's GraphQL Analytics API.
- How to send an email with a Worker.
- How to schedule the Worker to run at a specific time.
- How to store secrets and environment variables in your Worker.
- How to test the Worker locally.
- How to deploy the Worker to Cloudflare's edge network.
Before you start, make sure you:
- Sign up for a Cloudflare account ↗.
- Install
Node.js
↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0
or later.
- Add a domain to your Cloudflare account.
- Enable Email Routing for your domain.
- Create Cloudflare's Analytics API token.
While you can create a Worker using the Cloudflare dashboard, creating a Worker using the c3
CLI is recommended as it provides a more streamlined development experience and allows you to test your Worker locally.
First, use the c3
CLI to create a new Cloudflare Workers project.
In this tutorial, name your Worker as account-analytics
.
For setup, select the following options:
- For What would you like to start with?, choose
Hello World example
. - For Which template would you like to use?, choose
Hello World Worker
. - For Which language do you want to use?, choose
JavaScript
. - For Do you want to use git for version control?, choose
Yes
. - For Do you want to deploy your application?, choose
No
(we will be making some changes before deploying).
Now, the Worker is set up. Move into your project directory:
To continue with this tutorial, install the mimetext
↗ package:
wrangler.toml
↗ is the configuration file for your Worker. It was created when you ran c3
CLI. Open wrangler.toml
in your code editor and update it with the following configuration:
Before you continue, update the following:
destination_address
: Enter the email address where you want to receive the analytics report.[VARS]
: Enter the environment variable values you want to use in your Worker.
You will now add the code step by step to the src/index.js
file. This tutorial will explain each part of the code.
While you are in your project directory, open src/index.js
in your code editor and update it with the following code:
The code above defines two Worker Handlers:
fetch
: This Handler executes when the Worker is accessed via HTTP. It fetches the analytics data, formats it and returns it as a response.scheduled
: This Handler executes at the scheduled time. It fetches the analytics data, formats it and sends an email with the analytics data.
Add the following function to the src/index.js
file, below the Handlers:
In the code above, the fetchAnalytics
function fetches analytics data from Cloudflare's GraphQL Analytics API. The fetchAnalytics
function calculates yesterday's date, formats the date for display, and sends a GraphQL query to the Analytics API to fetch the analytics data.
This function returns the raw data for the previous day, including:
- Traffic overview data (Total requests, Page views and Blocked threats)
- Bandwidth data (Total bandwidth, Encrypted bandwidth and Cached bandwidth)
- Caching and Encryption data (Encrypted requests, Cached requests, Encryption rate and Cache rate)
- Browser data (Page views by browser)
- HTTP status code data (Requests by status code)
- HTTP version data (Requests by HTTP version)
This data will be used to generate the analytics report. In the following step, you will add the function that formats this data.
Add the following function to the src/index.js
file, below the fetchAnalytics
function:
At this point, you have defined the fetchAnalytics
function that fetches raw analytics data from Cloudflare's GraphQL Analytics API and the formatContent
function that formats the analytics data into a human-readable report.
Add the following function to the src/index.js
file, below the formatContent
function:
This function sends an email with the formatted analytics data to the specified recipient email address using Cloudflare's Email Routing service.
Now that you have updated the Worker code, you can test it locally using the wrangler dev
command. This command starts a local server that runs your Worker code.
Before you run the Worker, you need to add two Worker secrets:
CF_API_TOKEN
: Cloudflare GraphQL Analytics API token you created earlier.CF_ACCOUNT_ID
: Your Cloudflare account ID. You can find your account ID in the Cloudflare dashboard under the Workers & Pages Overview tab.
Create a .dev.vars
file in the root of your project directory and add the following:
Now, run the Worker locally:
Open the http://localhost:8787
URL on your browser. The browser will display analytics data.
Once you have tested the Worker locally, you can deploy your Worker to Cloudflare's edge network:
CLI command will output the URL where your Worker is deployed. Before you can use this URL in your browser to view the analytics data, you need to add two Worker secrets you already have locally to your deployed Worker:
Replace <secret>
with the name of the secret you want to add. Repeat this command for CF_API_TOKEN
and CF_ACCOUNT_ID
secrets.
Once you put the secrets, preview your analytics data at account-analytics.<YOUR_SUBDOMAIN>.workers.dev
. You will also receive an email report to the specified recipient email address every day at 10:00 AM.
If you want to disable a public URL for your Worker, you can do so by following these steps:
-
Log in to the Cloudflare dashboard ↗.
-
In Account Home, select Workers & Pages, then select
account-analytics
Worker. -
Go to Settings > Domains & Routes.
-
Select Disable to disable the public
account-analytics.<YOUR_SUBDOMAIN>.workers.dev
URL.
You have successfully created, tested and deployed a Worker that fetches analytics data from Cloudflare's GraphQL Analytics API and sends an email report via Email Routing.
To build more with Workers, refer to Tutorials.
If you have any questions, need assistance, or would like to share your project, join the Cloudflare Developer community on Discord ↗ to connect with other developers and the Cloudflare team.