Skip to content

Commit 56aeb19

Browse files
authored
Fix events tracked from the middleware missing some properties (#4242)
1 parent ebab818 commit 56aeb19

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

.changeset/warm-rabbits-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Fix events tracked from the middleware missing some properties.

packages/gitbook/src/middleware.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
CustomizationThemeMode,
3+
type PublishedSiteContent,
34
SiteInsightsDisplayContext,
5+
type SiteInsightsEventLocation,
46
SiteInsightsLLMSVariant,
57
} from '@gitbook/api';
68
import { isAIAgent } from '@vercel/agent-readability';
@@ -441,7 +443,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
441443
pathname,
442444
routeType: routeTypeFromPathname,
443445
events,
444-
} = encodePathInSiteContent(siteURLData.pathname, request);
446+
} = encodePathInSiteContent(siteURLData, request);
445447
routeType = routeTypeFromPathname ?? routeType;
446448

447449
if (events && events.length > 0) {
@@ -680,14 +682,23 @@ const PATH_ALIASES: Record<string, string> = {
680682
* Special paths are not encoded and passed to be handled by the route handlers.
681683
*/
682684
function encodePathInSiteContent(
683-
rawPathname: string,
685+
siteURLData: PublishedSiteContent,
684686
request: Request
685687
): {
686688
pathname: string;
687689
routeType?: 'static' | 'dynamic';
688690
events?: ServerInsightsEventInput[] | undefined;
689691
} {
690-
let pathname = removeLeadingSlash(removeTrailingSlash(rawPathname));
692+
let pathname = removeLeadingSlash(removeTrailingSlash(siteURLData.pathname));
693+
694+
const eventLocation: Partial<SiteInsightsEventLocation> = {
695+
siteSection: siteURLData.siteSection,
696+
siteSpace: siteURLData.siteSpace,
697+
siteShareKey: siteURLData.shareKey,
698+
space: siteURLData.space,
699+
revision: siteURLData.revision,
700+
displayContext: SiteInsightsDisplayContext.Server,
701+
};
691702

692703
if (pathname.match(/^~gitbook\/ogimage\/\S+$/)) {
693704
return { pathname };
@@ -702,9 +713,7 @@ function encodePathInSiteContent(
702713
events: [
703714
{
704715
type: 'rss_request',
705-
location: {
706-
displayContext: SiteInsightsDisplayContext.Server,
707-
},
716+
location: eventLocation,
708717
},
709718
],
710719
};
@@ -720,6 +729,7 @@ function encodePathInSiteContent(
720729
type: 'llms_request',
721730
llmsVariant: SiteInsightsLLMSVariant.Full,
722731
location: {
732+
siteSection: siteURLData.siteSection,
723733
displayContext: SiteInsightsDisplayContext.Server,
724734
},
725735
},

0 commit comments

Comments
 (0)