From 9be3cc8d3be05fb8235f6b20b867b33c7d96b588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 1 Sep 2025 12:04:46 +0200 Subject: [PATCH 1/4] Track event in the context of embed --- bun.lock | 4 ++-- package.json | 2 +- .../components/Embeddable/EmbeddableAIChat.tsx | 17 +++++++++++++++++ .../Embeddable/EmbeddableDocsPage.tsx | 2 ++ .../components/Insights/InsightsProvider.tsx | 2 ++ .../components/Insights/TrackPageViewEvent.tsx | 9 +++++++-- .../src/components/PageBody/PageBody.tsx | 7 ++++--- .../src/components/SitePage/SitePage.tsx | 7 ++++++- 8 files changed, 41 insertions(+), 9 deletions(-) diff --git a/bun.lock b/bun.lock index a71cf8bb7c..214682f2cc 100644 --- a/bun.lock +++ b/bun.lock @@ -295,7 +295,7 @@ "react-dom": "^19.0.0", }, "catalog": { - "@gitbook/api": "^0.137.0", + "@gitbook/api": "^0.138.0", "bidc": "^0.0.2", }, "packages": { @@ -661,7 +661,7 @@ "@fortawesome/fontawesome-svg-core": ["@fortawesome/fontawesome-svg-core@6.6.0", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="], - "@gitbook/api": ["@gitbook/api@0.137.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-3pTNbHI4kJT7ikqSdSLa2UCB0dOPOTzOUHcsCTLrk+rJVjTAFAJmTEW/Ax2prnwZ75ran2hz9/FhxUAGhp/8Mg=="], + "@gitbook/api": ["@gitbook/api@0.138.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-6jYH1R5IpmbFj3qUyDGuUBaABCinKQcqNCtTKtql0MSPaFp9KVnqyBsIaq/s9HohjCpyU1/EoddwzAaJWHWkkw=="], "@gitbook/browser-types": ["@gitbook/browser-types@workspace:packages/browser-types"], diff --git a/package.json b/package.json index 6bdf26c005..699eed2255 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "workspaces": { "packages": ["packages/*"], "catalog": { - "@gitbook/api": "^0.137.0", + "@gitbook/api": "^0.138.0", "bidc": "^0.0.2" } }, diff --git a/packages/gitbook/src/components/Embeddable/EmbeddableAIChat.tsx b/packages/gitbook/src/components/Embeddable/EmbeddableAIChat.tsx index ad30975466..969a00f283 100644 --- a/packages/gitbook/src/components/Embeddable/EmbeddableAIChat.tsx +++ b/packages/gitbook/src/components/Embeddable/EmbeddableAIChat.tsx @@ -7,6 +7,9 @@ import { AIChatDynamicIcon, AIChatSubtitle, } from '@/components/AIChat'; +import * as api from '@gitbook/api'; +import React from 'react'; +import { useTrackEvent } from '../Insights'; import { EmbeddableFrame, EmbeddableFrameBody, @@ -28,6 +31,20 @@ export function EmbeddableAIChat(props: { const chatController = useAIChatController(); const configuration = useEmbeddableConfiguration(); + // Track the view of the AI chat + const trackEvent = useTrackEvent(); + React.useEffect(() => { + trackEvent( + { + type: 'ask_view', + }, + { + pageId: null, + displayContext: api.SiteInsightsDisplayContext.Embed, + } + ); + }, [trackEvent]); + return ( diff --git a/packages/gitbook/src/components/Embeddable/EmbeddableDocsPage.tsx b/packages/gitbook/src/components/Embeddable/EmbeddableDocsPage.tsx index 685190629d..31d485aeb8 100644 --- a/packages/gitbook/src/components/Embeddable/EmbeddableDocsPage.tsx +++ b/packages/gitbook/src/components/Embeddable/EmbeddableDocsPage.tsx @@ -2,6 +2,7 @@ import { type PagePathParams, getSitePageData } from '@/components/SitePage'; import { PageBody } from '@/components/PageBody'; import type { GitBookSiteContext } from '@/lib/context'; +import { SiteInsightsDisplayContext } from '@gitbook/api'; import type { Metadata } from 'next'; import { Button } from '../primitives'; import { @@ -54,6 +55,7 @@ export async function EmbeddableDocsPage(props: EmbeddableDocsPageProps) { ancestors={ancestors} document={document} withPageFeedback={withPageFeedback} + insightsDisplayContext={SiteInsightsDisplayContext.Embed} /> diff --git a/packages/gitbook/src/components/Insights/InsightsProvider.tsx b/packages/gitbook/src/components/Insights/InsightsProvider.tsx index b36bccc7fc..8a176f6b64 100644 --- a/packages/gitbook/src/components/Insights/InsightsProvider.tsx +++ b/packages/gitbook/src/components/Insights/InsightsProvider.tsx @@ -16,6 +16,7 @@ export type InsightsEventName = api.SiteInsightsEvent['type']; * Context for an event on a page. */ export interface InsightsEventPageContext { + displayContext: api.SiteInsightsDisplayContext; pageId: string | null; } @@ -265,6 +266,7 @@ function transformEvents(input: { siteShareKey: input.context.siteShareKey ?? null, revision: input.context.revisionId, page: input.pageContext.pageId, + displayContext: input.pageContext.displayContext, }; return input.events.map((partialEvent) => { diff --git a/packages/gitbook/src/components/Insights/TrackPageViewEvent.tsx b/packages/gitbook/src/components/Insights/TrackPageViewEvent.tsx index c636a7fde1..c2cfbf0ec8 100644 --- a/packages/gitbook/src/components/Insights/TrackPageViewEvent.tsx +++ b/packages/gitbook/src/components/Insights/TrackPageViewEvent.tsx @@ -1,5 +1,6 @@ 'use client'; +import type { SiteInsightsDisplayContext } from '@gitbook/api'; import * as React from 'react'; import { useCurrentPage } from '../hooks'; @@ -8,7 +9,10 @@ import { useTrackEvent } from './InsightsProvider'; /** * Track a page view event. */ -export function TrackPageViewEvent() { +export function TrackPageViewEvent(props: { + displayContext: SiteInsightsDisplayContext; +}) { + const { displayContext } = props; const page = useCurrentPage(); const trackEvent = useTrackEvent(); @@ -19,9 +23,10 @@ export function TrackPageViewEvent() { }, { pageId: page?.pageId ?? null, + displayContext, } ); - }, [page, trackEvent]); + }, [page, trackEvent, displayContext]); return null; } diff --git a/packages/gitbook/src/components/PageBody/PageBody.tsx b/packages/gitbook/src/components/PageBody/PageBody.tsx index 10c4e7a1c5..0f9f4d4f95 100644 --- a/packages/gitbook/src/components/PageBody/PageBody.tsx +++ b/packages/gitbook/src/components/PageBody/PageBody.tsx @@ -1,5 +1,5 @@ import type { GitBookSiteContext } from '@/lib/context'; -import type { JSONDocument, RevisionPageDocument } from '@gitbook/api'; +import type { JSONDocument, RevisionPageDocument, SiteInsightsDisplayContext } from '@gitbook/api'; import React from 'react'; import { getSpaceLanguage } from '@/intl/server'; @@ -26,8 +26,9 @@ export function PageBody(props: { ancestors: AncestorRevisionPage[]; document: JSONDocument | null; withPageFeedback: boolean; + insightsDisplayContext: SiteInsightsDisplayContext; }) { - const { page, context, ancestors, document, withPageFeedback } = props; + const { page, context, ancestors, document, withPageFeedback, insightsDisplayContext } = props; const { customization } = context; const contentFullWidth = document ? hasFullWidthBlock(document) : false; @@ -117,7 +118,7 @@ export function PageBody(props: { } - + ); } diff --git a/packages/gitbook/src/components/SitePage/SitePage.tsx b/packages/gitbook/src/components/SitePage/SitePage.tsx index 5e40ac1551..a6956d57a0 100644 --- a/packages/gitbook/src/components/SitePage/SitePage.tsx +++ b/packages/gitbook/src/components/SitePage/SitePage.tsx @@ -1,6 +1,10 @@ import type { GitBookSiteContext } from '@/lib/context'; import { getPageDocument } from '@/lib/data'; -import { CustomizationHeaderPreset, CustomizationThemeMode } from '@gitbook/api'; +import { + CustomizationHeaderPreset, + CustomizationThemeMode, + SiteInsightsDisplayContext, +} from '@gitbook/api'; import type { Metadata, Viewport } from 'next'; import { notFound, redirect } from 'next/navigation'; import React from 'react'; @@ -65,6 +69,7 @@ export async function SitePage(props: SitePageProps) { ancestors={ancestors} document={document} withPageFeedback={withPageFeedback} + insightsDisplayContext={SiteInsightsDisplayContext.Site} /> From a91e9084c5863b65c01c52b2e1da67ff087f375e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 1 Sep 2025 12:05:14 +0200 Subject: [PATCH 2/4] Changeset --- .changeset/funny-experts-destroy.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/funny-experts-destroy.md diff --git a/.changeset/funny-experts-destroy.md b/.changeset/funny-experts-destroy.md new file mode 100644 index 0000000000..0e4057e391 --- /dev/null +++ b/.changeset/funny-experts-destroy.md @@ -0,0 +1,5 @@ +--- +"gitbook": minor +--- + +Track insight event when embedded assistant is displayed. From 85a5b69445d8ce703ceb9f0cd4588a37d2bc4578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 1 Sep 2025 12:07:02 +0200 Subject: [PATCH 3/4] Track view of the assistant in the app --- packages/gitbook/src/components/AIChat/AIChat.tsx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/gitbook/src/components/AIChat/AIChat.tsx b/packages/gitbook/src/components/AIChat/AIChat.tsx index 779004491d..90484bfd5c 100644 --- a/packages/gitbook/src/components/AIChat/AIChat.tsx +++ b/packages/gitbook/src/components/AIChat/AIChat.tsx @@ -20,6 +20,7 @@ import { EmbeddableFrameSubtitle, EmbeddableFrameTitle, } from '../Embeddable/EmbeddableFrame'; +import { useTrackEvent } from '../Insights'; import { useNow } from '../hooks'; import { Button } from '../primitives'; import { AIChatControlButton } from './AIChatControlButton'; @@ -54,6 +55,16 @@ export function AIChat(props: { trademark: boolean }) { [] ); + // Track the view of the AI chat + const trackEvent = useTrackEvent(); + React.useEffect(() => { + if (chat.opened) { + trackEvent({ + type: 'ask_view', + }); + } + }, [chat.opened, trackEvent]); + if (!chat.opened) { return null; } From beb97de575ecee480d560cf79f65066bb06eb02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 1 Sep 2025 12:09:21 +0200 Subject: [PATCH 4/4] Fix TS --- packages/gitbook/src/components/SitePage/SitePageNotFound.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gitbook/src/components/SitePage/SitePageNotFound.tsx b/packages/gitbook/src/components/SitePage/SitePageNotFound.tsx index 3ab4b3832b..0e64a73baf 100644 --- a/packages/gitbook/src/components/SitePage/SitePageNotFound.tsx +++ b/packages/gitbook/src/components/SitePage/SitePageNotFound.tsx @@ -3,6 +3,7 @@ import { TrackPageViewEvent } from '@/components/Insights'; import { t, useLanguage } from '@/intl/client'; import { tcls } from '@/lib/tailwind'; +import { SiteInsightsDisplayContext } from '@gitbook/api'; import { useRouter, useSearchParams } from 'next/navigation'; import { useEffect } from 'react'; import { useSpaceBasePath } from '../SpaceLayout/SpaceLayoutContext'; @@ -46,7 +47,7 @@ export function SitePageNotFound() { {/* Track the page not found as a page view */} - + );