Cloudflare Web Analyticsの設定をREST APIで行う方法
2024年10月06日 日曜日 23時00分こんにちは。
Cloudflare PagesのWeb Analyticsを有効にしているプロジェクトがいくつかあり、Web Analyticsを無効化しようと、プロジェクトのページにアクセスしました。 しかし、今まで設定画面に表示されていたWeb Analyticsの設定ボタンがなくなっていました。
これは困った…
といことで、CloudflareのREST APIを確認し、API経由でWeb Analyticsを無効化することにしました。
以下は、作業メモです。
ドキュメント
以下のAPIを使います。
- https://developers.cloudflare.com/api/operations/pages-project-get-project
- https://developers.cloudflare.com/api/operations/pages-project-update-project
APIトークンの準備
APIトークンを作成し、検証します。
アクセス許可には Clodflare Pages 読み取り(read)
と Clodflare Pages 編集(edit)
を追加します。
APIトークンを作成

APIトークンの作成完了

APIトークンのテスト
画面に表示されたので、まずはAPIトークンのテストをします。
curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
-H "Authorization: Bearer TOKEN_123456789" \
-H "Content-Type:application/json" | jq -r . | tee "$(date +"%Y%m%d_%H%M%S").json"
レスポンス
{
"result": {
"id": "MY_ID_123456789",
"status": "active"
},
"success": true,
"errors": [],
"messages": [
{
"code": 10000,
"message": "This API Token is valid and active",
"type": null
}
]
}
APIトークンの準備ができました。
プロジェクトの詳細情報を取得
Web Analyticsを無効化したいプロジェクトの情報を取得します。ドキュメントには、以下のcurlのサンプルがあります。
curl --request GET \
--url https://api.cloudflare.com/client/v4/accounts/account_id/pages/projects/project_name \
--header 'Content-Type: application/json' \
--header 'X-Auth-Email: '
以下のように修正し、実行しました。
account_id="your_account_id"
project_name="your_project_name"
token="TOKEN_123456789"
curl --request GET \
--url "https://api.cloudflare.com/client/v4/accounts/${account_id}/pages/projects/${project_name}" \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' | jq -r . | tee "$(date +"%Y%m%d_%H%M%S").json"
以下は、ドキュメントのレスポンスです。
{
"errors": [],
"messages": [],
"success": true,
"result": {
"build_config": {
"build_caching": true,
"build_command": "npm run build",
"destination_dir": "build",
"root_dir": "/",
"web_analytics_tag": "cee1c73f6e4743d0b5e6bb1a0bcaabcc",
"web_analytics_token": "021e1057c18547eca7b79f2516f06o7x"
},
"canonical_deployment": {
"aliases": [
"https://branchname.projectname.pages.dev"
],
"build_config": {
"build_caching": true,
"build_command": "npm run build",
"destination_dir": "build",
"root_dir": "/",
"web_analytics_tag": "cee1c73f6e4743d0b5e6bb1a0bcaabcc",
"web_analytics_token": "021e1057c18547eca7b79f2516f06o7x"
},
"created_on": "2021-03-09T00:55:03.923456Z",
"deployment_trigger": {
"metadata": {
"branch": "main",
"commit_hash": "ad9ccd918a81025731e10e40267e11273a263421",
"commit_message": "Update index.html"
},
"type": "ad_hoc"
},
"env_vars": {
"BUILD_VERSION": {
"value": "3.3"
},
"ENV": {
"value": "STAGING"
}
},
"environment": "preview",
"id": "f64788e9-fccd-4d4a-a28a-cb84f88f6",
"is_skipped": true,
"latest_stage": {
"ended_on": "2021-03-09T00:58:59.045655",
"name": "deploy",
"started_on": "2021-03-09T00:55:03.923456Z",
"status": "success"
},
"modified_on": "2021-03-09T00:58:59.045655",
"project_id": "7b162ea7-7367-4d67-bcde-1160995d5",
"project_name": "ninjakittens",
"short_id": "f64788e9",
"source": {
"config": {
"deployments_enabled": true,
"owner": "string",
"path_excludes": [
"string"
],
"path_includes": [
"string"
],
"pr_comments_enabled": true,
"preview_branch_excludes": [
"string"
],
"preview_branch_includes": [
"string"
],
"preview_deployment_setting": "all",
"production_branch": "string",
"production_deployments_enabled": true,
"repo_name": "string"
},
"type": "string"
},
"stages": [
{
"ended_on": "2021-06-03T15:39:03.134378Z",
"name": "queued",
"started_on": "2021-06-03T15:38:15.608194Z",
"status": "active"
},
{
"ended_on": null,
"name": "initialize",
"started_on": null,
"status": "idle"
},
{
"ended_on": null,
"name": "clone_repo",
"started_on": null,
"status": "idle"
},
{
"ended_on": null,
"name": "build",
"started_on": null,
"status": "idle"
},
{
"ended_on": null,
"name": "deploy",
"started_on": null,
"status": "idle"
}
],
"url": "https://f64788e9.ninjakittens.pages.dev"
},
"created_on": "2017-01-01T00:00:00Z",
"deployment_configs": {
"preview": {
"ai_bindings": {
"AI_BINDING": {
"project_id": "some-project-id"
}
},
"analytics_engine_datasets": {
"ANALYTICS_ENGINE_BINDING": {
"dataset": "api_analytics"
}
},
"browsers": {
"BROWSER": {}
},
"compatibility_date": "2022-01-01",
"compatibility_flags": [
"url_standard"
],
"d1_databases": {
"D1_BINDING": {
"id": "445e2955-951a-43f8-a35b-a4d0c8138f63"
}
},
"durable_object_namespaces": {
"DO_BINDING": {
"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}
},
"env_vars": {
"property1": {
"type": "plain_text",
"value": "hello world"
},
"property2": {
"type": "plain_text",
"value": "hello world"
}
},
"hyperdrive_bindings": {
"HYPERDRIVE": {
"id": "a76a99bc342644deb02c38d66082262a"
}
},
"kv_namespaces": {
"KV_BINDING": {
"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}
},
"mtls_certificates": {
"MTLS": {
"certificate_id": "d7cdd17c-916f-4cb7-aabe-585eb382ec4e"
}
},
"placement": {
"mode": "smart"
},
"queue_producers": {
"QUEUE_PRODUCER_BINDING": {
"name": "some-queue"
}
},
"r2_buckets": {
"R2_BINDING": {
"name": "some-bucket"
}
},
"services": {
"SERVICE_BINDING": {
"entrypoint": "MyHandler",
"environment": "production",
"service": "example-worker"
}
},
"vectorize_bindings": {
"VECTORIZE": {
"index_name": "my_index"
}
}
},
"production": {
"ai_bindings": {
"AI_BINDING": {
"project_id": "some-project-id"
}
},
"analytics_engine_datasets": {
"ANALYTICS_ENGINE_BINDING": {
"dataset": "api_analytics"
}
},
"browsers": {
"BROWSER": {}
},
"compatibility_date": "2022-01-01",
"compatibility_flags": [
"url_standard"
],
"d1_databases": {
"D1_BINDING": {
"id": "445e2955-951a-43f8-a35b-a4d0c8138f63"
}
},
"durable_object_namespaces": {
"DO_BINDING": {
"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}
},
"env_vars": {
"property1": {
"type": "plain_text",
"value": "hello world"
},
"property2": {
"type": "plain_text",
"value": "hello world"
}
},
"hyperdrive_bindings": {
"HYPERDRIVE": {
"id": "a76a99bc342644deb02c38d66082262a"
}
},
"kv_namespaces": {
"KV_BINDING": {
"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}
},
"mtls_certificates": {
"MTLS": {
"certificate_id": "d7cdd17c-916f-4cb7-aabe-585eb382ec4e"
}
},
"placement": {
"mode": "smart"
},
"queue_producers": {
"QUEUE_PRODUCER_BINDING": {
"name": "some-queue"
}
},
"r2_buckets": {
"R2_BINDING": {
"name": "some-bucket"
}
},
"services": {
"SERVICE_BINDING": {
"entrypoint": "MyHandler",
"environment": "production",
"service": "example-worker"
}
},
"vectorize_bindings": {
"VECTORIZE": {
"index_name": "my_index"
}
}
}
},
"domains": [
"customdomain.com",
"customdomain.org"
],
"id": "7b162ea7-7367-4d67-bcde-1160995d5",
"latest_deployment": {
"aliases": [
"https://branchname.projectname.pages.dev"
],
"build_config": {
"build_caching": true,
"build_command": "npm run build",
"destination_dir": "build",
"root_dir": "/",
"web_analytics_tag": "cee1c73f6e4743d0b5e6bb1a0bcaabcc",
"web_analytics_token": "021e1057c18547eca7b79f2516f06o7x"
},
"created_on": "2021-03-09T00:55:03.923456Z",
"deployment_trigger": {
"metadata": {
"branch": "main",
"commit_hash": "ad9ccd918a81025731e10e40267e11273a263421",
"commit_message": "Update index.html"
},
"type": "ad_hoc"
},
"env_vars": {
"BUILD_VERSION": {
"value": "3.3"
},
"ENV": {
"value": "STAGING"
}
},
"environment": "preview",
"id": "f64788e9-fccd-4d4a-a28a-cb84f88f6",
"is_skipped": true,
"latest_stage": {
"ended_on": "2021-03-09T00:58:59.045655",
"name": "deploy",
"started_on": "2021-03-09T00:55:03.923456Z",
"status": "success"
},
"modified_on": "2021-03-09T00:58:59.045655",
"project_id": "7b162ea7-7367-4d67-bcde-1160995d5",
"project_name": "ninjakittens",
"short_id": "f64788e9",
"source": {
"config": {
"deployments_enabled": true,
"owner": "string",
"path_excludes": [
"string"
],
"path_includes": [
"string"
],
"pr_comments_enabled": true,
"preview_branch_excludes": [
"string"
],
"preview_branch_includes": [
"string"
],
"preview_deployment_setting": "all",
"production_branch": "string",
"production_deployments_enabled": true,
"repo_name": "string"
},
"type": "string"
},
"stages": [
{
"ended_on": "2021-06-03T15:39:03.134378Z",
"name": "queued",
"started_on": "2021-06-03T15:38:15.608194Z",
"status": "active"
},
{
"ended_on": null,
"name": "initialize",
"started_on": null,
"status": "idle"
},
{
"ended_on": null,
"name": "clone_repo",
"started_on": null,
"status": "idle"
},
{
"ended_on": null,
"name": "build",
"started_on": null,
"status": "idle"
},
{
"ended_on": null,
"name": "deploy",
"started_on": null,
"status": "idle"
}
],
"url": "https://f64788e9.ninjakittens.pages.dev"
},
"name": "NextJS Blog",
"production_branch": "main",
"source": {
"config": {
"deployments_enabled": true,
"owner": "string",
"path_excludes": [
"string"
],
"path_includes": [
"string"
],
"pr_comments_enabled": true,
"preview_branch_excludes": [
"string"
],
"preview_branch_includes": [
"string"
],
"preview_deployment_setting": "all",
"production_branch": "string",
"production_deployments_enabled": true,
"repo_name": "string"
},
"type": "string"
},
"subdomain": "helloworld.pages.dev"
}
}
私が取得したプロジェクトのレスポンスは以下のようなJSONでした。 キーは、最低限のみ表示しています。
{
"result": {
"id": "id-123456789",
"name": "your_project_name",
"subdomain": "your-subdomain.pages.dev",
"build_config": {
"build_command": "hugo",
"destination_dir": "public",
"root_dir": "my_blog",
"web_analytics_tag": "your_web_analytics_tag_value",
"web_analytics_token": "your_web_analytics_token_value"
}
},
"success": true,
"errors": [],
"messages": []
}
Web Analyticsを無効化するには web_analytics_tag
と web_analytics_token
を nullにすれば良いので ""
でアップデートすれば完了です。
Web Analyticsを無効化する
ドキュメントには、以下のcurlのサンプルがあります。
curl --request PATCH \
--url https://api.cloudflare.com/client/v4/accounts/account_id/pages/projects/project_name \
--header 'Content-Type: application/json' \
--header 'X-Auth-Email: ' \
--data '{
"build_config": {
"build_caching": true,
"build_command": "npm run build",
"destination_dir": "build",
"root_dir": "/",
"web_analytics_tag": "cee1c73f6e4743d0b5e6bb1a0bcaabcc",
"web_analytics_token": "021e1057c18547eca7b79f2516f06o7x"
},
"deployment_configs": {
"preview": {
"ai_bindings": {
"AI_BINDING": {
"project_id": "some-project-id"
}
},
"analytics_engine_datasets": {
"ANALYTICS_ENGINE_BINDING": {
"dataset": "api_analytics"
}
},
"browsers": {
"BROWSER": {}
},
"compatibility_date": "2022-01-01",
"compatibility_flags": [
"url_standard"
],
"d1_databases": {
"D1_BINDING": {
"id": "445e2955-951a-43f8-a35b-a4d0c8138f63"
}
},
"durable_object_namespaces": {
"DO_BINDING": {
"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}
},
"env_vars": {
"property1": {
"type": "plain_text",
"value": "hello world"
},
"property2": {
"type": "plain_text",
"value": "hello world"
}
},
"hyperdrive_bindings": {
"HYPERDRIVE": {
"id": "a76a99bc342644deb02c38d66082262a"
}
},
"kv_namespaces": {
"KV_BINDING": {
"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}
},
"mtls_certificates": {
"MTLS": {
"certificate_id": "d7cdd17c-916f-4cb7-aabe-585eb382ec4e"
}
},
"placement": {
"mode": "smart"
},
"queue_producers": {
"QUEUE_PRODUCER_BINDING": {
"name": "some-queue"
}
},
"r2_buckets": {
"R2_BINDING": {
"name": "some-bucket"
}
},
"services": {
"SERVICE_BINDING": {
"entrypoint": "MyHandler",
"environment": "production",
"service": "example-worker"
}
},
"vectorize_bindings": {
"VECTORIZE": {
"index_name": "my_index"
}
}
},
"production": {
"ai_bindings": {
"AI_BINDING": {
"project_id": "some-project-id"
}
},
"analytics_engine_datasets": {
"ANALYTICS_ENGINE_BINDING": {
"dataset": "api_analytics"
}
},
"browsers": {
"BROWSER": {}
},
"compatibility_date": "2022-01-01",
"compatibility_flags": [
"url_standard"
],
"d1_databases": {
"D1_BINDING": {
"id": "445e2955-951a-43f8-a35b-a4d0c8138f63"
}
},
"durable_object_namespaces": {
"DO_BINDING": {
"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}
},
"env_vars": {
"property1": {
"type": "plain_text",
"value": "hello world"
},
"property2": {
"type": "plain_text",
"value": "hello world"
},
"BUILD_VERSION": {
"value": "3.3"
},
"delete_this_env_var": null,
"secret_var": {
"type": "secret_text",
"value": "A_CMS_API_TOKEN"
}
},
"hyperdrive_bindings": {
"HYPERDRIVE": {
"id": "a76a99bc342644deb02c38d66082262a"
}
},
"kv_namespaces": {
"KV_BINDING": {
"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
}
},
"mtls_certificates": {
"MTLS": {
"certificate_id": "d7cdd17c-916f-4cb7-aabe-585eb382ec4e"
}
},
"placement": {
"mode": "smart"
},
"queue_producers": {
"QUEUE_PRODUCER_BINDING": {
"name": "some-queue"
}
},
"r2_buckets": {
"R2_BINDING": {
"name": "some-bucket"
}
},
"services": {
"SERVICE_BINDING": {
"entrypoint": "MyHandler",
"environment": "production",
"service": "example-worker"
}
},
"vectorize_bindings": {
"VECTORIZE": {
"index_name": "my_index"
}
}
}
},
"name": "NextJS Blog",
"production_branch": "main"
}'
以下のように修正し、実行しました。
account_id="your_account_id"
project_name="your_project_name"
token="TOKEN_123456789"
curl --request PATCH \
--url "https://api.cloudflare.com/client/v4/accounts/${account_id}/pages/projects/${project_name}" \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' \
--data '{
"build_config": {
"web_analytics_tag": "",
"web_analytics_token": ""
}
}' | jq -r . | tee "$(date +"%Y%m%d_%H%M%S").json"
このリクエストが成功すると、設定画面からWeb Analyticsの表示が消えますが、この時点では、HTMLに自動的に挿入されたコードは消えていません。
再ビルドする
再ビルドすることで、挿入されたコードは消えます。
再ビルド後に、Web Analytics用のコードがないことを確認してください。


分析とログ > Web Analytics から削除する
HTMLからコードは消えましたが、分析とログ > Web Analytics
メニューの一覧からは追加したサイトは消えないようです。
そのため、こちらのリストからも削除します。

APIトークンを更新する
先ほど作成したトークンのアクセス許可を修正して、更新します。
分析とログ > Web Analytics
を編集するには、アクセス許可に アカウント 設定 読み取り(read)
と アカウント 設定 読み取り(edit)
を追加します。

Web Analyticsの一覧を取得する
以下を参考に、リクエストします。
ドキュメントのサンプル
curl --request GET \
--url https://api.cloudflare.com/client/v4/accounts/account_id/rum/site_info/list \
--header 'Content-Type: application/json' \
--header 'X-Auth-Email: '
修正したコマンド
account_id="your_account_id"
token="TOKEN_123456789"
curl --request GET \
--url "https://api.cloudflare.com/client/v4/accounts/${account_id}/rum/site_info/list" \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' | jq -r . | tee "$(date +"%Y%m%d_%H%M%S").json"
レスポンス
{
"result": [
{
"site_tag": "your_site_tag",
"site_token": "your_site_token",
"host": "(komaki-dev.pages.dev|komaki.dev|www.komaki.dev)$",
"created": "2024-09-02T03:53:43.805572Z",
"snippet": "<!-- Cloudflare Web Analytics --><script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{\"token\": \"token\"}'></script><!-- End Cloudflare Web Analytics -->",
"is_host_regex": true,
"auto_install": false
}
],
"success": true,
"errors": null,
"messages": null,
"result_info": {
"page": 1,
"per_page": 10,
"count": 5,
"total_count": 5,
"total_pages": 1
}
}
Web Analyticsの一覧から削除する
以下を参考に、リクエストします。
ドキュメントのサンプル
curl --request DELETE \
--url https://api.cloudflare.com/client/v4/accounts/account_id/rum/site_info/site_id \
--header 'Content-Type: application/json' \
--header 'X-Auth-Email: '
修正したコマンド
account_id="your_account_id"
site_id="your_site_tag_value"
token="TOKEN_123456789"
curl --request DELETE \
--url "https://api.cloudflare.com/client/v4/accounts/${account_id}/rum/site_info/${site_id}" \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' | jq -r . | tee "$(date +"%Y%m%d_%H%M%S").json"
レスポンス
{
"result": {
"site_tag": "your_site_tag"
},
"success": true,
"errors": [],
"messages": []
}
分析とログ > Web Analytics から削除されていることを確認する
リストから削除されていることを確認しました。

APIトークンを削除する
不要になったら、APIトークンを削除します。
おわり
もし、間違っている部分がありましたら、コメント頂けると大変助かります。 よろしくお願いいたします。