From 6ca03e53b4ad9eb3c697179b807ef5d59a05383f Mon Sep 17 00:00:00 2001 From: Nolann Biron Date: Fri, 14 Aug 2026 14:32:36 +0200 Subject: [PATCH 1/2] Attribute markdown and ask events to their space `page_markdown_request` and the `ask_question` the middleware emits for `?ask=` carried only a displayContext, so they were stored with an empty spaceId and an unparseable siteSpaceId. Any space- or section-scoped query dropped them, and the markdown traffic of a space read as zero. Reuse the location the middleware already builds, as `rss_request` does. The llms branches keep theirs: llms.txt spans every section and site-space, so a single spaceId would attribute a site-wide document to one of them. `ask_question` also gains the goal, which was parsed off the query string to be encoded in the rewrite and then dropped from the event. The pageId the TODO asked for is not in the URL, and resolving it in the middleware costs a request per document served; it is left to the API to return alongside the space. --- packages/gitbook/src/middleware.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 8736966f26..1774c83f32 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -753,12 +753,14 @@ function encodePathInSiteContent( } { let pathname = removeLeadingSlash(removeTrailingSlash(siteURLData.pathname)); + // The optional fields are coerced to null, otherwise they are dropped from the JSON payload + // instead of being sent as the `string | null` the API expects. const eventLocation: Partial = { - siteSection: siteURLData.siteSection, + siteSection: siteURLData.siteSection ?? null, siteSpace: siteURLData.siteSpace, - siteShareKey: siteURLData.shareKey, + siteShareKey: siteURLData.shareKey ?? null, space: siteURLData.space, - revision: siteURLData.revision, + revision: siteURLData.revision ?? null, displayContext: SiteInsightsDisplayContext.Server, }; @@ -881,24 +883,19 @@ function encodePathInSiteContent( }` : `~gitbook/markdown/${encodePagePath(pagePathWithoutMD)}`, routeType: 'static', - // TODO: track pageId / spaceId when possible - // We don't do it at the moment as we can't easily extract it from the URL. events: ask ? [ { type: 'ask_question', query: ask, - location: { - displayContext: SiteInsightsDisplayContext.Server, - }, + ...(goal ? { goal } : {}), + location: eventLocation, }, ] : [ { type: 'page_markdown_request', - location: { - displayContext: SiteInsightsDisplayContext.Server, - }, + location: eventLocation, }, ], }; From 1cfc0c1f993f3200299136fc2ea27d82d6d83d81 Mon Sep 17 00:00:00 2001 From: Nolann Biron Date: Fri, 14 Aug 2026 14:32:43 +0200 Subject: [PATCH 2/2] changeset --- .changeset/markdown-events-space-attribution.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/markdown-events-space-attribution.md diff --git a/.changeset/markdown-events-space-attribution.md b/.changeset/markdown-events-space-attribution.md new file mode 100644 index 0000000000..633e1ab7d4 --- /dev/null +++ b/.changeset/markdown-events-space-attribution.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Attribute the markdown and ask events tracked from the middleware to their space, and record the goal passed alongside a question.