From ad49a7f91dcc5adedc45f88f67dc2e9bcb040018 Mon Sep 17 00:00:00 2001 From: Nolann Biron Date: Tue, 15 Sep 2026 18:43:53 +0200 Subject: [PATCH] Keep preview deployment traffic out of the sites' analytics Gating on `url` mode also caught whatever else reaches a deployment through /url/. A build flag set only on preview deployments is the actual intent and leaves production untouched. --- .changeset/relative-insights-visitor-urls.md | 2 +- .github/composite/deploy-cloudflare/action.yaml | 1 + .github/composite/deploy-vercel/action.yaml | 1 + packages/gitbook/src/app/~gitbook/env/route.ts | 2 ++ packages/gitbook/src/lib/env/globals.ts | 6 ++++++ packages/gitbook/src/lib/tracking.ts | 4 ++-- packages/gitbook/src/middleware.ts | 6 ++---- 7 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.changeset/relative-insights-visitor-urls.md b/.changeset/relative-insights-visitor-urls.md index d0a5fb9a8..aec780a60 100644 --- a/.changeset/relative-insights-visitor-urls.md +++ b/.changeset/relative-insights-visitor-urls.md @@ -4,4 +4,4 @@ Fix analytics and adaptive content silently breaking on sites served from a different host than the one configured (apex vs www, domain alias, CDN). The insights and visitor-claims endpoints are now requested relative to the page's own origin instead of the configured host, which a prerendered page cannot know. -Stop tracking events when a site is served through `/url/:url` on GitBook's own host, the access mode used by local development and preview deployments. That traffic is not the site's and no longer reaches its analytics. +Keep the traffic of GitBook's own preview deployments out of the sites' analytics, now that those requests reach the events endpoint. diff --git a/.github/composite/deploy-cloudflare/action.yaml b/.github/composite/deploy-cloudflare/action.yaml index 77bc5689d..a000568b1 100644 --- a/.github/composite/deploy-cloudflare/action.yaml +++ b/.github/composite/deploy-cloudflare/action.yaml @@ -69,6 +69,7 @@ runs: GITBOOK_RUNTIME: cloudflare GITBOOK_BLOCK_SEARCH_INDEXATION: ${{ inputs.environment == 'preview' && 'true' || '' }} GITBOOK_ALLOW_CUSTOMIZATION_OVERRIDE: ${{ inputs.environment == 'preview' && 'true' || '' }} + GITBOOK_DISABLE_INSIGHTS: ${{ inputs.environment == 'preview' && 'true' || '' }} shell: bash - name: Upload the DO worker diff --git a/.github/composite/deploy-vercel/action.yaml b/.github/composite/deploy-vercel/action.yaml index 4aa9fbbfe..76968b3e2 100644 --- a/.github/composite/deploy-vercel/action.yaml +++ b/.github/composite/deploy-vercel/action.yaml @@ -75,6 +75,7 @@ runs: echo "GITBOOK_RUNTIME=vercel" >> .vercel/.env.${{ inputs.environment }}.local echo "GITBOOK_BLOCK_SEARCH_INDEXATION=true" >> .vercel/.env.${{ inputs.environment }}.local echo "GITBOOK_ALLOW_CUSTOMIZATION_OVERRIDE=true" >> .vercel/.env.${{ inputs.environment }}.local + echo "GITBOOK_DISABLE_INSIGHTS=true" >> .vercel/.env.${{ inputs.environment }}.local echo "--- .vercel/.env.${{ inputs.environment }}.local after inject ---" cat .vercel/.env.${{ inputs.environment }}.local - name: Build Project Artifacts diff --git a/packages/gitbook/src/app/~gitbook/env/route.ts b/packages/gitbook/src/app/~gitbook/env/route.ts index 15a08167c..80eec2e02 100644 --- a/packages/gitbook/src/app/~gitbook/env/route.ts +++ b/packages/gitbook/src/app/~gitbook/env/route.ts @@ -6,6 +6,7 @@ import { GITBOOK_API_URL, GITBOOK_APP_URL, GITBOOK_ASSETS_URL, + GITBOOK_DISABLE_INSIGHTS, GITBOOK_DISABLE_TRACKING, GITBOOK_FONTS_URL, GITBOOK_ICONS_URL, @@ -35,6 +36,7 @@ export async function GET(_req: NextRequest) { GITBOOK_INTEGRATIONS_HOST, GITBOOK_INTEGRATIONS_CONTENT_HOST, GITBOOK_DISABLE_TRACKING, + GITBOOK_DISABLE_INSIGHTS, // Secret envs GITBOOK_SECRET: !!GITBOOK_SECRET, diff --git a/packages/gitbook/src/lib/env/globals.ts b/packages/gitbook/src/lib/env/globals.ts index b7d97778b..67883253b 100644 --- a/packages/gitbook/src/lib/env/globals.ts +++ b/packages/gitbook/src/lib/env/globals.ts @@ -78,6 +78,12 @@ export const GITBOOK_DISABLE_TRACKING = Boolean( !!process.env.GITBOOK_DISABLE_TRACKING || process.env.NODE_ENV !== 'production' ); +/** + * Whether insights events must not reach the sites' analytics, while the pages otherwise behave + * like production. Set on the preview deployments, whose traffic is e2e runs, not the sites'. + */ +export const GITBOOK_DISABLE_INSIGHTS = process.env.GITBOOK_DISABLE_INSIGHTS === 'true'; + /** * Hostname serving the integrations. */ diff --git a/packages/gitbook/src/lib/tracking.ts b/packages/gitbook/src/lib/tracking.ts index 22dfd14d6..fa561ecc3 100644 --- a/packages/gitbook/src/lib/tracking.ts +++ b/packages/gitbook/src/lib/tracking.ts @@ -3,7 +3,7 @@ import type { headers as nextHeaders } from 'next/headers'; import type * as api from '@gitbook/api'; import { apiClient } from './data/api'; -import { GITBOOK_DISABLE_TRACKING } from './env'; +import { GITBOOK_DISABLE_INSIGHTS, GITBOOK_DISABLE_TRACKING } from './env'; import { getLogger } from './logger'; /** @@ -83,7 +83,7 @@ export async function trackServerInsightsEvents(args: { `Tracking ${args.events.length} events at ${request.url} for site ${args.siteId} (enabled=${!GITBOOK_DISABLE_TRACKING})` ); - if (GITBOOK_DISABLE_TRACKING) { + if (GITBOOK_DISABLE_TRACKING || GITBOOK_DISABLE_INSIGHTS) { return; } diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index ed2daf0aa..d7479dcb8 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -34,7 +34,7 @@ import { normalizeRequestURL, throwIfDataError, } from '@/lib/data'; -import { isGitBookAssetsHostURL, isGitBookHostURL } from '@/lib/env'; +import { GITBOOK_DISABLE_INSIGHTS, isGitBookAssetsHostURL, isGitBookHostURL } from '@/lib/env'; import { getImageResizingContextId } from '@/lib/images'; import { isAITrainingOrIndexingRequest } from '@/lib/indexing-crawlers'; import { MiddlewareHeaders } from '@/lib/middleware'; @@ -185,9 +185,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { //Forwards analytics events if (siteRequestURL.pathname.endsWith('/~gitbook/__evt')) { - // `url` mode only serves `/url/:url` on GitBook's own host — local dev and preview - // deployments. That traffic is not the site's, so it stays out of its analytics. - if (mode === 'url') { + if (GITBOOK_DISABLE_INSIGHTS) { return new Response(null, { status: 204 }); } return await serveProxyAnalyticsEvent(request);