From 60c565410bbb99d25188fa1c05cd57f1fdfda90a Mon Sep 17 00:00:00 2001 From: moshiur01 Date: Thu, 19 Nov 2026 00:52:33 +0600 Subject: [PATCH] feat(docs): add Cloudflare Web Analytics Load the Cloudflare Web Analytics beacon when the NEXT_PUBLIC_CF_WEB_ANALYTICS_TOKEN build variable is set, and only load Vercel Analytics on Vercel builds, where its script exists. Co-Authored-By: Claude Opus 5.5 --- apps/docs/app/layout.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/docs/app/layout.tsx b/apps/docs/app/layout.tsx index 16f0ee9..62fca52 100644 --- a/apps/docs/app/layout.tsx +++ b/apps/docs/app/layout.tsx @@ -1,4 +1,5 @@ import type { Metadata, Viewport } from 'next'; +import Script from 'next/script'; import { Analytics } from '@vercel/analytics/next'; import { DocsRoot, isAssistantEnabled } from 'docora'; import docsConfig from '../docs.config'; @@ -12,6 +13,14 @@ export const metadata: Metadata = rootMetadata(); export const viewport: Viewport = rootViewport; +// Vercel sets VERCEL=1 at build time. Its analytics script only exists on +// Vercel, so a Cloudflare build would request it and get a 404. +const isVercel = Boolean(process.env.VERCEL); + +// Cloudflare Web Analytics site token, from Web Analytics → the site → snippet. +// It is public (it ships in the page), so it is read as a build variable. +const cloudflareAnalyticsToken = process.env.NEXT_PUBLIC_CF_WEB_ANALYTICS_TOKEN; + const RootLayout = async ({ children }: Readonly<{ children: React.ReactNode }>) => { return ( ) assistantEnabled={isAssistantEnabled(docsConfig.assistant)} > {children} - + {isVercel && } + {cloudflareAnalyticsToken && ( +