Skip to content

Commit 72c3b80

Browse files
authored
Pass visitor auth token to site insights events (#2662)
1 parent 8126a83 commit 72c3b80

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

packages/gitbook/src/components/Insights/InsightsProvider.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ const InsightsContext = React.createContext<TrackEventCallback>(() => {});
6464
interface InsightsProviderProps extends InsightsEventContext {
6565
enabled: boolean;
6666
apiHost: string;
67+
visitorAuthToken: string | null;
6768
children: React.ReactNode;
6869
}
6970

7071
/**
7172
* Wrap the content of the app with the InsightsProvider to track events.
7273
*/
7374
export function InsightsProvider(props: InsightsProviderProps) {
74-
const { enabled, apiHost, children, ...context } = props;
75+
const { enabled, apiHost, visitorAuthToken, children, ...context } = props;
7576

7677
const visitorIdRef = React.useRef<string | null>(null);
7778
const eventsRef = React.useRef<{
@@ -124,6 +125,7 @@ export function InsightsProvider(props: InsightsProviderProps) {
124125
pageContext: eventsForPathname.pageContext,
125126
visitorId,
126127
sessionId: session.id,
128+
visitorAuthToken,
127129
}),
128130
);
129131

@@ -255,6 +257,7 @@ function transformEvents(input: {
255257
pageContext: InsightsEventPageContext;
256258
visitorId: string;
257259
sessionId: string;
260+
visitorAuthToken: string | null;
258261
}): api.SiteInsightsEvent[] {
259262
const session: api.SiteInsightsEventSession = {
260263
sessionId: input.sessionId,
@@ -263,6 +266,7 @@ function transformEvents(input: {
263266
language: window.navigator.language,
264267
cookies: cookies.get(),
265268
referrer: document.referrer || null,
269+
visitorAuthToken: input.visitorAuthToken ?? null,
266270
};
267271

268272
const location: api.SiteInsightsEventLocation = {

packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import React from 'react';
1313
import { Footer } from '@/components/Footer';
1414
import { CompactHeader, Header } from '@/components/Header';
1515
import { CONTAINER_STYLE } from '@/components/layout';
16-
import { ColorDebugger } from '@/components/primitives/ColorDebugger';
1716
import { SearchModal } from '@/components/Search';
1817
import { TableOfContents } from '@/components/TableOfContents';
1918
import { api, ContentTarget, type SectionsList, SiteContentPointer } from '@/lib/api';
2019
import { ContentRefContext } from '@/lib/references';
2120
import { tcls } from '@/lib/tailwind';
2221
import { shouldTrackEvents } from '@/lib/tracking';
22+
import { getCurrentVisitorToken } from '@/lib/visitor-token';
2323

2424
import { SpacesDropdown } from '../Header/SpacesDropdown';
2525
import { InsightsProvider } from '../Insights';
@@ -69,6 +69,7 @@ export function SpaceLayout(props: {
6969
<InsightsProvider
7070
enabled={shouldTrackEvents()}
7171
apiHost={api().client.endpoint}
72+
visitorAuthToken={getCurrentVisitorToken()}
7273
{...content}
7374
>
7475
{/* <ColorDebugger /> */}

packages/gitbook/src/lib/visitor-token.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { headers } from 'next/headers';
12
import type { NextRequest } from 'next/server';
23
import hash from 'object-hash';
34

@@ -89,6 +90,14 @@ export function normalizeVisitorAuthURL(url: URL): URL {
8990
return withoutVAParam;
9091
}
9192

93+
/**
94+
* Get the visitor token from the request context.
95+
*/
96+
export function getCurrentVisitorToken(): string | null {
97+
const visitorToken = headers().get('x-gitbook-visitor-token');
98+
return visitorToken;
99+
}
100+
92101
/**
93102
* Get all possible basePaths for a given URL. This is used to find the visitor
94103
* authentication cookie token.

packages/gitbook/src/middleware.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export type LookupResult = PublishedContentWithCache & {
8080
apiEndpoint?: string;
8181
/** Cookies to store on the response */
8282
cookies?: LookupCookies;
83+
/** Visitor authentication token */
84+
visitorToken?: string;
8385
};
8486

8587
/**
@@ -269,6 +271,10 @@ export async function middleware(request: NextRequest) {
269271
headers.set('x-gitbook-api', apiEndpoint);
270272
}
271273

274+
if (resolved.visitorToken) {
275+
headers.set('x-gitbook-visitor-token', resolved.visitorToken);
276+
}
277+
272278
const target = new URL(rewritePathname, request.nextUrl.toString());
273279
target.search = url.search;
274280

@@ -300,7 +306,6 @@ export async function middleware(request: NextRequest) {
300306
setMiddlewareHeader(response, 'x-gitbook-cache-control', cacheControl);
301307
}
302308
}
303-
// }
304309

305310
if (resolved.cacheTags && resolved.cacheTags.length > 0) {
306311
const headerCacheTag = resolved.cacheTags.join(',');
@@ -407,6 +412,7 @@ async function lookupSiteInSingleMode(url: URL): Promise<LookupResult> {
407412
basePath: '',
408413
pathname: url.pathname,
409414
apiToken,
415+
visitorToken: undefined,
410416
};
411417
}
412418

@@ -440,6 +446,7 @@ async function lookupSiteInMultiMode(request: NextRequest, url: URL): Promise<Lo
440446
...('basePath' in lookup && visitorAuthToken
441447
? getLookupResultForVisitorAuth(lookup.basePath, visitorAuthToken)
442448
: {}),
449+
visitorToken: visitorAuthToken?.token,
443450
};
444451
}
445452

@@ -667,6 +674,7 @@ async function lookupSiteInMultiPathMode(request: NextRequest, url: URL): Promis
667674
...('basePath' in lookup && visitorAuthToken
668675
? getLookupResultForVisitorAuth(lookup.basePath, visitorAuthToken)
669676
: {}),
677+
visitorToken: visitorAuthToken?.token,
670678
};
671679
}
672680

0 commit comments

Comments
 (0)