From ccbcab88224976705fb932debc97052b3e98ad10 Mon Sep 17 00:00:00 2001 From: Valentino Hudhra <2587839+valentin0h@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:10:28 +0200 Subject: [PATCH] Control tracking of page views with env/header variables (#2380) --- .../src/components/PageBody/PageBody.tsx | 5 +++-- packages/gitbook/src/lib/tracking.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 packages/gitbook/src/lib/tracking.ts diff --git a/packages/gitbook/src/components/PageBody/PageBody.tsx b/packages/gitbook/src/components/PageBody/PageBody.tsx index 3dca70556..d8c2c437e 100644 --- a/packages/gitbook/src/components/PageBody/PageBody.tsx +++ b/packages/gitbook/src/components/PageBody/PageBody.tsx @@ -13,6 +13,7 @@ import { ContentPointer, ContentTarget, SiteContentPointer, api } from '@/lib/ap import { hasFullWidthBlock, isNodeEmpty } from '@/lib/document'; import { ContentRefContext, resolveContentRef } from '@/lib/references'; import { tcls } from '@/lib/tailwind'; +import { shouldTrackPageViews } from '@/lib/tracking'; import { PageBodyBlankslate } from './PageBodyBlankslate'; import { PageCover } from './PageCover'; @@ -141,14 +142,14 @@ export function PageBody(props: { ) : null} - + {shouldTrackPageViews() ? ( - + ) : null} ); } diff --git a/packages/gitbook/src/lib/tracking.ts b/packages/gitbook/src/lib/tracking.ts new file mode 100644 index 000000000..513bdb191 --- /dev/null +++ b/packages/gitbook/src/lib/tracking.ts @@ -0,0 +1,17 @@ +import { headers } from 'next/headers'; + +/** + * Return true if a space track page views. + */ +export function shouldTrackPageViews(): boolean { + const headerSet = headers(); + + if ( + process.env.GITBOOK_BLOCK_PAGE_VIEWS_TRACKING && + !headerSet.has('x-gitbook-track-page-views') + ) { + return false; + } + + return true; +}