Enable Web Analytics Though Api

Hello,

I am trying to replicate what we can do though the web interface at the following location:
Cloudflare → Analytics → Web Analytics → Set up hostname → Enter your website’s hostname or select from your existing websites on Cloudflare.

How can we activate analytics and enter the website hostname though the api?

Mike

I’d like to do the same, is there a way API to access web analytics for a website not proxied through Cloudflare?

POST /api/v4/accounts/:account_id/rum/site_info might work - with a request body like {"host":"example.com"}.

It’ll return a snippet for you to use.

{
  "result": {
    "site_tag": "<snip>",
    "site_token": "<snip>",
    "host": "example.com",
    "created": "2022-06-12T17:34:08.305308Z",
    "snippet": "\u003c!-- Cloudflare Web Analytics --\u003e\u003cscript defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{\"token\": \"<snip>\"}'\u003e\u003c/script\u003e\u003c!-- End Cloudflare Web Analytics --\u003e",
    "auto_install": false
  },
  "success": true,
  "errors": [],
  "messages": []
}

I don’t actually know if this is available on the public API (https://api.cloudflare.com/client/v4/)

1 Like

The following works for existing zones.

POST https://api.cloudflare.com/client/v4/accounts/:account_id/rum/site_info with body {"zone_tag":":ZONE_ID","auto_install":true}

Response

{
    "result": {
        "site_tag": "<snip>",
        "site_token": "<snip>",
        "created": "2022-10-18T13:52:53.044131Z",
        "snippet": "<!-- Cloudflare Web Analytics --><script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{\"token\": \"<snip>\"}'></script><!-- End Cloudflare Web Analytics -->",
        "auto_install": true,
        "ruleset": {
            "zone_tag": "<snip>",
            "zone_name": "<snip>",
            "enabled": true,
            "id": "<snip>"
        },
        "rules": [
            {
                "host": "*",
                "paths": [
                    "*"
                ],
                "inclusive": true,
                "created": "2022-10-18T13:52:53.042723Z",
                "is_paused": false,
                "priority": 1000,
                "id": "<snip>"
            }
        ]
    },
    "success": true,
    "errors": [],
    "messages": []
}

Note: only tested on Enterprise.

I could not find this API endpoint in the public API, and instead inspected the post action when clicking the “save” button in the UI.