You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/workers/tutorials/automated-analytics-reporting/index.mdx
+27-29Lines changed: 27 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ Before you start, make sure you:
36
36
4.[Enable Email Routing](/email-routing/get-started/enable-email-routing/) for your domain.
37
37
5. Create Cloudflare's [Analytics API token](/analytics/graphql-api/getting-started/authentication/api-token-auth/).
38
38
39
-
## Create a Worker
39
+
## 1. Create a Worker
40
40
41
41
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.
42
42
@@ -48,7 +48,7 @@ First, use the `c3` CLI to create a new Cloudflare Workers project.
48
48
args={"account-analytics"}
49
49
/>
50
50
51
-
In this tutorial, we will name our Worker `account-analytics`.
51
+
In this tutorial, name your Worker as`account-analytics`.
52
52
53
53
<Render
54
54
file="c3-post-run-steps"
@@ -60,13 +60,13 @@ In this tutorial, we will name our Worker `account-analytics`.
60
60
}}
61
61
/>
62
62
63
-
Now, the Worker is set up. Move into that project directory.
63
+
Now, the Worker is set up. Move into your project directory:
64
64
65
65
```sh
66
66
cd account-analytics
67
67
```
68
68
69
-
Before we continue with the tutorial, let's install [`mimetext`](https://www.npmjs.com/package/mimetext) package.
69
+
To continue with this tutorial, install the [`mimetext`](https://www.npmjs.com/package/mimetext) package:
70
70
71
71
<Tabs> <TabItemlabel="pnpm">
72
72
@@ -88,7 +88,7 @@ yarn add mimetext
88
88
89
89
</TabItem> </Tabs>
90
90
91
-
## Update wrangler.toml
91
+
## 2. Update wrangler.toml file
92
92
93
93
[`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/) 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:
2.`destination_address` with the email address where you want to receive the analytics report.
134
-
3.`[VARS]` with the environment variable values you want to use in your Worker.
132
+
1.`compatibility_date`: Enter the current date.
133
+
2.`destination_address`: Enter the email address where you want to receive the analytics report.
134
+
3.`[VARS]`: Enter the environment variable values you want to use in your Worker.
135
135
136
136
:::note[IMPORTANT]
137
137
`destination_address` and `RECIPIENT_EMAIL`**must** contain [Email Routing verified email](/email-routing/get-started/enable-email-routing/) address.
138
138
139
-
`SENDER_EMAIL`**must** be address on a domain that:
139
+
`SENDER_EMAIL`**must** be an email address on a domain that is added to your Cloudflare domain and has Email Routing enabled.
140
140
141
-
1. Is added to your Cloudflare domain
142
-
2. Has Email Routing enabled.
143
-
:::
141
+
:::
144
142
145
-
## Update the Worker code
143
+
## 3. Update the Worker code
146
144
147
-
You will now add the code step by step to the `src/index.js` file. We will explain each part of the code as we go along.
145
+
You will now add the code step by step to the `src/index.js` file. This tutorial will explain each part of the code.
148
146
149
147
### Add the required modules and Handlers
150
148
@@ -193,7 +191,7 @@ export default {
193
191
};
194
192
```
195
193
196
-
In the code above, we have defined two [Worker Handlers](/workers/runtime-apis/handlers/):
194
+
The code above defines two [Worker Handlers](/workers/runtime-apis/handlers/):
197
195
198
196
-`fetch`: This Handler executes when the Worker is accessed via HTTP. It fetches the analytics data, formats it and returns it as a response.
199
197
-`scheduled`: This Handler executes at the scheduled time. It fetches the analytics data, formats it and sends an email with the analytics data.
@@ -271,7 +269,7 @@ async function fetchAnalytics(env) {
271
269
}
272
270
```
273
271
274
-
In the code above, we have defined a function `fetchAnalytics`that fetches analytics data from Cloudflare's GraphQL Analytics API. The function calculates yesterday's date, formats it for display, and sends a GraphQL query to the Analytics API to fetch the analytics data.
272
+
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.
275
273
276
274
:::note
277
275
`env.CF_API_TOKEN` and `env.CF_ACCOUNT_ID` are [Worker Secrets](/workers/configuration/secrets/). These variables are used to authenticate the request and fetch the analytics data for the specified account. You will add these secrets to your Worker after the code is complete.
@@ -281,12 +279,12 @@ This function returns the **raw** data for the previous day, including:
281
279
282
280
- Traffic overview data (Total requests, Page views and Blocked threats)
283
281
- Bandwidth data (Total bandwidth, Encrypted bandwidth and Cached bandwidth)
284
-
- Caching & Encryption data (Encrypted requests, Cached requests, Encryption rate and Cache rate)
282
+
- Caching and Encryption data (Encrypted requests, Cached requests, Encryption rate and Cache rate)
285
283
- Browser data (Page views by browser)
286
284
- HTTP status code data (Requests by status code)
287
285
- HTTP version data (Requests by HTTP version)
288
286
289
-
This data will be used to generate the analytics report. Let's add the function that formats this data.
287
+
This data will be used to generate the analytics report. In the following step, you will add the function that formats this data.
290
288
291
289
### Add the data formatting function
292
290
@@ -410,16 +408,16 @@ This function sends an email with the formatted analytics data to the specified
410
408
`sendEmail` function uses multiple environment variables that are set in the `wrangler.toml` file.
411
409
:::
412
410
413
-
## Test the Worker
411
+
## 4. Test the Worker
414
412
415
413
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.
416
414
417
415
Before you run the Worker, you need to add two Worker secrets:
418
416
419
417
-`CF_API_TOKEN`: Cloudflare GraphQL Analytics API token you created earlier.
420
-
-`CF_ACCOUNT_ID`: Your Cloudflare account ID. Can be found in the Cloudflare dashboard under the Workers & Pages Overview tab.
418
+
-`CF_ACCOUNT_ID`: Your Cloudflare account ID. You can find your account ID in the Cloudflare dashboard under the **Workers & Pages** Overview tab.
421
419
422
-
Let's create`.dev.vars` file in the root of your project directory and add the following:
420
+
Create a`.dev.vars` file in the root of your project directory and add the following:
423
421
424
422
```sh
425
423
CF_API_TOKEN=YOUR_CLOUDFLARE_API_TOKEN
@@ -432,35 +430,35 @@ Now, run the Worker locally:
432
430
npx wrangler dev --remote
433
431
```
434
432
435
-
Open your browser and visit `http://localhost:8787`. You should see the analytics data displayed in your browser.
433
+
Open the `http://localhost:8787` URL on your browser. The browser will display analytics data.
436
434
437
-
## Deploy the Worker and Worker secrets
435
+
## 5. Deploy the Worker and Worker secrets
438
436
439
-
Once you have tested the Worker locally, you can deploy it to Cloudflare's edge network:
437
+
Once you have tested the Worker locally, you can deploy your Worker to Cloudflare's edge network:
440
438
441
439
```sh
442
440
npx wrangler deploy
443
441
```
444
442
445
-
CLI command will output the URL where your Worker is deployed. Before you can visit 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:
443
+
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:
446
444
447
445
```sh
448
446
npx wrangler secret put <secret>
449
447
```
450
448
451
449
Replace `<secret>` with the name of the secret you want to add. Repeat this command for `CF_API_TOKEN` and `CF_ACCOUNT_ID` secrets.
452
450
453
-
Once you put the secrets, visit the `account-analytics.<YOUR_SUBDOMAIN>.workers.dev` to view the analytics data. You will also receive an email report to the specified recipient email address every day at 10:00 AM.
451
+
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.
454
452
455
453
If you want to disable a public URL for your Worker, you can do so by following these steps:
456
454
457
455
1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com).
458
456
459
-
2. In Account Home, select **Workers & Pages**, and then select `account-analytics` Worker.
457
+
2. In Account Home, select **Workers & Pages**, then select `account-analytics` Worker.
460
458
461
-
3. Go to **Settings**-**Domains & Routes**
459
+
3. Go to **Settings**>**Domains & Routes**.
462
460
463
-
4.Click 'Disable' to disable the public `account-analytics.<YOUR_SUBDOMAIN>.workers.dev` URL.
461
+
4.Select **Disable** to disable the public `account-analytics.<YOUR_SUBDOMAIN>.workers.dev` URL.
464
462
465
463
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.
0 commit comments