Cloudflare Web Analytics -- Privacy-First Analytics
In this tutorial, you'll learn about Cloudflare Web Analytics. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
This tutorial explains how Cloudflare Web Analytics provides privacy-first website analytics without cookies, tracking scripts, or GDPR consent popups, measuring real user metrics directly from the edge network.
Why Privacy-First Web Analytics Matters
Traditional web analytics tools like Google Analytics rely on cookies and JavaScript tracking scripts that collect personal data, trigger GDPR and CCPA Compliance requirements, and slow page loads. Cloudflare Web Analytics takes a different approach: it measures page views, visit duration, and core web vitals directly from Cloudflare's edge network without any client-side tracking code. No cookies, no fingerprinting, no data collection from visitors. This eliminates the need for cookie consent banners for analytics, improves page speed by removing tracking scripts, and provides accurate metrics regardless of ad blockers.
Real-world use: Doda Browser replaced Google Analytics with Cloudflare Web Analytics across all products. The switch eliminated cookie consent popups on tutorial pages, improved page load time by 300ms, and provided more accurate Visitor counts because ad blockers no longer block the analytics.
Web Analytics Architecture
flowchart LR A[Visitor requests page] --> B[Cloudflare edge] B --> C[Serve page HTML + JS beacon] C --> D[Browser sends beacon] D --> E[Edge processes metrics] E --> F[Web Analytics dashboard] B --> G[Metrics aggregated at edge] G --> F style B fill:#f90,color:#fff style E fill:#f90,color:#fff style F fill:#f90,color:#fff
Setting Up Web Analytics
Cloudflare Web Analytics can be set up on any website, even those not using Cloudflare as a DNS provider. You place a small JavaScript snippet in your site head and the analytics start collecting data immediately.
<!-- Web Analytics snippet - place in the <head> of your site -->
<script defer src="https://static.cloudflareinsights.com/beacon.min.js"
data-cf-beacon='{"token": "YOUR_TOKEN"}'>
</script>
The beacon script is 2KB (minified and gzipped) -- roughly 10 times smaller than Google Analytics. It collects only page views, visit duration, and performance metrics. No cookies are set, and no personal data is transmitted.
Understanding Core Web Vitals
Web Analytics measures the three Core Web Vitals metrics that Google uses for search ranking: Largest Contentful Paint (LCP), First Input Delay (FID), and Cumulative Layout Shift (CLS).
// Programmatically access Web Analytics data via the API
const response = await fetch(
'https://api.cloudflare.com/client/v4/accounts/ACCOUNT_ID/web_analytics/sites/SITE_ID',
{
headers: { 'Authorization': 'Bearer API_TOKEN' }
}
);
const data = await response.json();
console.log(data.result.metrics);
// Expected output:
// {
// "pageviews": 45200,
// "visits": 23100,
// "lcp": { "good": 0.72, "needs_improvement": 0.18, "poor": 0.10 },
// "fid": { "good": 0.88, "needs_improvement": 0.08, "poor": 0.04 },
// "cls": { "good": 0.81, "needs_improvement": 0.12, "poor": 0.07 },
// "visit_duration_avg": 145
// }
The dashboard shows what percentage of visits fall into good, needs improvement, and poor categories for each metric. The example shows 72 percent of visits have good LCP (under 2.5 seconds), while 10 percent experience poor LCP (over 4 seconds).
Filtering and Segmenting Data
Web Analytics supports filtering by path, country, browser, and operating system. This helps identify specific pages or user groups that need optimization.
# Fetch analytics filtered by country
curl -s "https://api.cloudflare.com/client/v4/accounts/ACCOUNT_ID/web_analytics/sites/SITE_ID" \
-H "Authorization: Bearer API_TOKEN" \
--data '{"filter":{"country":"DE"}}' | jq '.result.metrics'
# Expected output:
# {
# "pageviews": 4230,
# "visits": 2100,
# "visit_duration_avg": 168,
# "lcp": {"good": 0.68, "needs_improvement": 0.22, "poor": 0.10}
# }
# Filter by specific page path
curl -s "https://api.cloudflare.com/client/v4/accounts/ACCOUNT_ID/web_analytics/sites/SITE_ID" \
-H "Authorization: Bearer API_TOKEN" \
--data '{"filter":{"path":"/tutorials/cloudflare"}}' | jq '.result.metrics'
# Expected output:
# {
# "pageviews": 12500,
# "visits": 6800,
# "visit_duration_avg": 245,
# "lcp": {"good": 0.75, "needs_improvement": 0.15, "poor": 0.10}
# }
Filtering reveals performance differences across regions. In the example, German visitors have slightly worse LCP than average, suggesting edge performance could be improved in European data centers.
Comparing Web Analytics vs Traditional Analytics
Cloudflare Web Analytics focuses on essential metrics without the complexity of traditional tools. It covers page views, visits, visit duration, and core web vitals -- the metrics that actually matter for most site owners.
# Traditional analytics approach - requires cookies and consent
# Code size: ~45KB (Google Analytics)
# Data collected: IP, user agent, referral, behavioral tracking
# Compliance: requires GDPR consent banner
# Blocked by ad blockers: yes
# Cloudflare Web Analytics approach - privacy first
# Code size: ~2KB
# Data collected: page URL, referrer, performance metrics
# Compliance: no consent needed (no personal data)
# Blocked by ad blockers: no
This comparison shows why privacy-first analytics is becoming the standard. You get the data you need to improve your site without compromising visitor privacy or page speed.
FAQ
Practice Questions
- How does Cloudflare Web Analytics collect data without using cookies?
- What are the three Core Web Vitals metrics displayed in the Web Analytics dashboard?
- Why does Web Analytics provide more accurate Visitor counts compared to traditional analytics tools?
Summary
Cloudflare Web Analytics provides essential website metrics without cookies, tracking scripts, or privacy Compliance overhead. A 2KB beacon script collects page views, visit duration, and Core Web Vitals from real users, with data aggregated at the edge. No personal data is collected, no consent banners are required, and ad blockers cannot interfere with the measurements. For site owners who want actionable analytics without compromising privacy, it is the ideal solution.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro -- security-first tools for the modern web.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro