Skip to content

Commit 7f5a44f

Browse files
committed
rewording and style fixes
1 parent 505456b commit 7f5a44f

File tree

1 file changed

+27
-29
lines changed
  • src/content/docs/workers/tutorials/automated-analytics-reporting

1 file changed

+27
-29
lines changed

src/content/docs/workers/tutorials/automated-analytics-reporting/index.mdx

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Before you start, make sure you:
3636
4. [Enable Email Routing](/email-routing/get-started/enable-email-routing/) for your domain.
3737
5. Create Cloudflare's [Analytics API token](/analytics/graphql-api/getting-started/authentication/api-token-auth/).
3838

39-
## Create a Worker
39+
## 1. Create a Worker
4040

4141
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.
4242

@@ -48,7 +48,7 @@ First, use the `c3` CLI to create a new Cloudflare Workers project.
4848
args={"account-analytics"}
4949
/>
5050

51-
In this tutorial, we will name our Worker `account-analytics`.
51+
In this tutorial, name your Worker as `account-analytics`.
5252

5353
<Render
5454
file="c3-post-run-steps"
@@ -60,13 +60,13 @@ In this tutorial, we will name our Worker `account-analytics`.
6060
}}
6161
/>
6262

63-
Now, the Worker is set up. Move into that project directory.
63+
Now, the Worker is set up. Move into your project directory:
6464

6565
```sh
6666
cd account-analytics
6767
```
6868

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:
7070

7171
<Tabs> <TabItem label="pnpm">
7272

@@ -88,7 +88,7 @@ yarn add mimetext
8888

8989
</TabItem> </Tabs>
9090

91-
## Update wrangler.toml
91+
## 2. Update wrangler.toml file
9292

9393
[`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:
9494

@@ -129,22 +129,20 @@ EMAIL_SUBJECT = "Cloudflare Analytics Report"
129129

130130
Before you continue, update the following:
131131

132-
1. `compatibility_date` with the current date.
133-
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.
135135

136136
:::note[IMPORTANT]
137137
`destination_address` and `RECIPIENT_EMAIL` **must** contain [Email Routing verified email](/email-routing/get-started/enable-email-routing/) address.
138138

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.
140140

141-
1. Is added to your Cloudflare domain
142-
2. Has Email Routing enabled.
143-
:::
141+
:::
144142

145-
## Update the Worker code
143+
## 3. Update the Worker code
146144

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.
148146

149147
### Add the required modules and Handlers
150148

@@ -193,7 +191,7 @@ export default {
193191
};
194192
```
195193

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/):
197195

198196
- `fetch`: This Handler executes when the Worker is accessed via HTTP. It fetches the analytics data, formats it and returns it as a response.
199197
- `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) {
271269
}
272270
```
273271

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.
275273

276274
:::note
277275
`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:
281279

282280
- Traffic overview data (Total requests, Page views and Blocked threats)
283281
- 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)
285283
- Browser data (Page views by browser)
286284
- HTTP status code data (Requests by status code)
287285
- HTTP version data (Requests by HTTP version)
288286

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.
290288

291289
### Add the data formatting function
292290

@@ -410,16 +408,16 @@ This function sends an email with the formatted analytics data to the specified
410408
`sendEmail` function uses multiple environment variables that are set in the `wrangler.toml` file.
411409
:::
412410

413-
## Test the Worker
411+
## 4. Test the Worker
414412

415413
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.
416414

417415
Before you run the Worker, you need to add two Worker secrets:
418416

419417
- `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.
421419

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:
423421

424422
```sh
425423
CF_API_TOKEN=YOUR_CLOUDFLARE_API_TOKEN
@@ -432,35 +430,35 @@ Now, run the Worker locally:
432430
npx wrangler dev --remote
433431
```
434432

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.
436434

437-
## Deploy the Worker and Worker secrets
435+
## 5. Deploy the Worker and Worker secrets
438436

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:
440438

441439
```sh
442440
npx wrangler deploy
443441
```
444442

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:
446444

447445
```sh
448446
npx wrangler secret put <secret>
449447
```
450448

451449
Replace `<secret>` with the name of the secret you want to add. Repeat this command for `CF_API_TOKEN` and `CF_ACCOUNT_ID` secrets.
452450

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.
454452

455453
If you want to disable a public URL for your Worker, you can do so by following these steps:
456454

457455
1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com).
458456

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.
460458

461-
3. Go to **Settings** - **Domains & Routes**
459+
3. Go to **Settings** > **Domains & Routes**.
462460

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.
464462

465463
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.
466464

0 commit comments

Comments
 (0)