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.
This commit is contained in:
Nolann Biron
2026-09-15 18:43:53 +02:00
parent 426c58671d
commit ad49a7f91d
7 changed files with 15 additions and 7 deletions
+1 -1
View File
@@ -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.
@@ -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
@@ -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
+2
View File
@@ -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,
+6
View File
@@ -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.
*/
+2 -2
View File
@@ -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;
}
+2 -4
View File
@@ -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);