Scripts

Cloudflare Web Analytics

Cloudflare Web Analytics with Nuxt is a great privacy analytics solution. It offers free, privacy-centric analytics for your website. It doesn't gather personal data from your visitors, yet provides detailed insights into your web pages' performance as experienced by your visitors.

Cloudflare Web Analytics

View source

Nuxt Config Setup

Add this to your nuxt.config.ts to load Cloudflare Web Analytics globally. Alternatively you can use the useScriptCloudflareWebAnalytics composable for more control.

export default defineNuxtConfig({
  scripts: {
    registry: {
      cloudflareWebAnalytics: {
        token: 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4',
        trigger: 'onNuxtReady',
      }
    }
  }
})

This config automatically enables first-party mode (bundle + proxy). See below to customise.

useScriptCloudflareWebAnalytics()

The useScriptCloudflareWebAnalytics composable lets you have fine-grain control over when and how Cloudflare Web Analytics is loaded on your site.

const { proxy } = useScriptCloudflareWebAnalytics()

// automatic page view tracking

Please follow the Registry Scripts guide to learn more about advanced usage.

First-Party Mode: Privacy Focused Proxy

No extra config needed. The script is bundled from your domain (faster loads, no extra DNS lookup) and runtime requests are reverse-proxied through your server with automatic anonymisation (user IPs stay hidden from Cloudflare Web Analytics, works with ad blockers). Learn more.

Mode
Bundle Proxy Partytown
Privacy
User IP addresses are anonymised. Other request data passes through.
export default defineNuxtConfig({
  scripts: {
    // ✅ First-party mode: bundled + proxied
    registry: {
      cloudflareWebAnalytics: {
        token: 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4',
        trigger: 'onNuxtReady',
      },
    },
  },
})

Example

Using Cloudflare Web Analytics in a component with the proxy to send events .

<script setup lang="ts">
const { proxy } = useScriptCloudflareWebAnalytics()

// noop in development, ssr
// just works in production, client
function handleAction() {
  // automatic page view tracking
}
</script>

<template>
  <div>
    <button @click="handleAction">
      Send Event
    </button>
  </div>
</template>

The composable comes with the following defaults:

  • Trigger: Client Script will load when the Nuxt is hydrating to keep web vital metrics accurate.

Loading in app.vue

Loading Cloudflare Web Analytics through the app.vue when Nuxt is ready.

app.vue
<script setup lang="ts">
useScriptCloudflareWebAnalytics({
  token: '12ee46bf598b45c2868bbc07a3073f58',
  scriptOptions: {
    trigger: 'onNuxtReady'
  }
})
</script>

The Cloudflare Web Analytics composable injects a window.__cfBeacon object into the global scope. If you need to access this you can do by awaiting the script.

const { onLoaded } = useScriptCloudflareWebAnalytics()
onLoaded(({ __cfBeacon }) => {
  console.log(__cfBeacon)
})
tokenstring required

The Cloudflare Web Analytics token.

spaboolean = true

Cloudflare Web Analytics enables measuring SPAs automatically by overriding the History API's pushState function and listening to the onpopstate. Hash-based router is not supported.