diff --git a/packages/gitbook/src/components/Ads/Ad.tsx b/packages/gitbook/src/components/Ads/Ad.tsx index 81fb4f64e..f6cbd8e44 100644 --- a/packages/gitbook/src/components/Ads/Ad.tsx +++ b/packages/gitbook/src/components/Ads/Ad.tsx @@ -12,7 +12,7 @@ import * as React from 'react'; import { t, useLanguage } from '@/intl/client'; import { type ClassValue, tcls } from '@/lib/tailwind'; -import { useTrackEvent } from '../Insights'; +import { toSlimTrackEvent, useTrackEvent } from '../Insights'; import { useHasBeenInViewport } from '../hooks/useHasBeenInViewport'; import { Link } from '../primitives'; import { renderAd } from './renderAd'; @@ -137,10 +137,10 @@ function AdSponsoredLink(props: { spaceId: string }) { target="_blank" href={viaUrl.toString()} className={tcls('hover:underline')} - insights={{ + insights={toSlimTrackEvent({ type: 'trademark_click', placement: SiteInsightsTrademarkPlacement.Ad, - }} + })} > {t(language, 'sponsored_via_gitbook')} diff --git a/packages/gitbook/src/components/DocumentView/BlockContentRef.tsx b/packages/gitbook/src/components/DocumentView/BlockContentRef.tsx index 7640a66e2..c1f4f9f19 100644 --- a/packages/gitbook/src/components/DocumentView/BlockContentRef.tsx +++ b/packages/gitbook/src/components/DocumentView/BlockContentRef.tsx @@ -2,6 +2,7 @@ import { type DocumentBlockContentRef, SiteInsightsLinkPosition } from '@gitbook import { Card } from '@/components/primitives'; import { type ResolvedContentRef, resolveContentRef } from '@/lib/references'; +import { toSlimTrackEvent } from '../Insights'; import type { BlockProps } from './Block'; @@ -34,13 +35,13 @@ export async function BlockContentRef(props: BlockProps href={resolved.href} title={resolved.text} style={style} - insights={{ + insights={toSlimTrackEvent({ type: 'link_click', link: { target: block.data.ref, position: SiteInsightsLinkPosition.Content, }, - }} + })} /> ); } diff --git a/packages/gitbook/src/components/DocumentView/InlineButton.tsx b/packages/gitbook/src/components/DocumentView/InlineButton.tsx index a36cd7452..2517ee1f8 100644 --- a/packages/gitbook/src/components/DocumentView/InlineButton.tsx +++ b/packages/gitbook/src/components/DocumentView/InlineButton.tsx @@ -1,5 +1,6 @@ import { resolveContentRef } from '@/lib/references'; import * as api from '@gitbook/api'; +import { toSlimTrackEvent } from '../Insights'; import { Button } from '../primitives'; import type { InlineProps } from './Inline'; @@ -25,13 +26,13 @@ export async function InlineButton(props: InlineProps) // TODO: use a variant specifically for user-defined buttons. variant={inline.data.kind} className="leading-normal" - insights={{ + insights={toSlimTrackEvent({ type: 'link_click', link: { target: inline.data.ref, position: api.SiteInsightsLinkPosition.Content, }, - }} + })} /> ); diff --git a/packages/gitbook/src/components/DocumentView/InlineLink.tsx b/packages/gitbook/src/components/DocumentView/InlineLink.tsx index f5bd7f798..5981d74ac 100644 --- a/packages/gitbook/src/components/DocumentView/InlineLink.tsx +++ b/packages/gitbook/src/components/DocumentView/InlineLink.tsx @@ -2,6 +2,7 @@ import { type DocumentInlineLink, SiteInsightsLinkPosition } from '@gitbook/api' import { resolveContentRef } from '@/lib/references'; import { Icon } from '@gitbook/icons'; +import { toSlimTrackEvent } from '../Insights'; import { StyledLink } from '../primitives'; import type { InlineProps } from './Inline'; import { InlineLinkTooltip } from './InlineLinkTooltip'; @@ -35,13 +36,13 @@ export async function InlineLink(props: InlineProps) { ) { return ( {resolved.text} diff --git a/packages/gitbook/src/components/DocumentView/Table/RecordCard.tsx b/packages/gitbook/src/components/DocumentView/Table/RecordCard.tsx index c32fb01e9..c5d3f4059 100644 --- a/packages/gitbook/src/components/DocumentView/Table/RecordCard.tsx +++ b/packages/gitbook/src/components/DocumentView/Table/RecordCard.tsx @@ -8,6 +8,7 @@ import { LinkBox, LinkOverlay } from '@/components/primitives'; import { Image } from '@/components/utils'; import { resolveContentRef } from '@/lib/references'; import { type ClassValue, tcls } from '@/lib/tailwind'; +import { toSlimTrackEvent } from '../../Insights'; import { RecordColumnValue } from './RecordColumnValue'; import type { TableRecordKV, TableViewProps } from './Table'; @@ -174,13 +175,13 @@ export async function RecordCard( {body} diff --git a/packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx b/packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx index 50dc0d827..e28a3d165 100644 --- a/packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx +++ b/packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx @@ -15,6 +15,7 @@ import { getSimplifiedContentType } from '@/lib/files'; import { resolveContentRef } from '@/lib/references'; import { tcls } from '@/lib/tailwind'; import { filterOutNullable } from '@/lib/typescript'; +import { toSlimTrackEvent } from '../../Insights'; import type { BlockProps } from '../Block'; import { Blocks } from '../Blocks'; @@ -275,13 +276,13 @@ export async function RecordColumnValue( {resolved.text} diff --git a/packages/gitbook/src/components/Footer/FooterLinksGroup.tsx b/packages/gitbook/src/components/Footer/FooterLinksGroup.tsx index dd2de59eb..90c464ac5 100644 --- a/packages/gitbook/src/components/Footer/FooterLinksGroup.tsx +++ b/packages/gitbook/src/components/Footer/FooterLinksGroup.tsx @@ -7,6 +7,7 @@ import type { GitBookAnyContext } from '@v2/lib/context'; import { resolveContentRef } from '@/lib/references'; import { tcls } from '@/lib/tailwind'; +import { toSlimTrackEvent } from '../Insights'; import { Link } from '../primitives'; @@ -58,13 +59,13 @@ async function FooterLink(props: { link: CustomizationContentLink; context: GitB 'links-accent:decoration-primary-subtle', 'links-accent:decoration-[3px]' )} - insights={{ + insights={toSlimTrackEvent({ type: 'link_click', link: { target: link.to, position: SiteInsightsLinkPosition.Footer, }, - }} + })} > {link.title} diff --git a/packages/gitbook/src/components/Insights/index.ts b/packages/gitbook/src/components/Insights/index.ts index 792440a5c..d2baacc70 100644 --- a/packages/gitbook/src/components/Insights/index.ts +++ b/packages/gitbook/src/components/Insights/index.ts @@ -3,3 +3,4 @@ export * from './visitorId'; export * from './cookies'; export * from './TrackPageViewEvent'; export * from './useVisitedPages'; +export * from './slimTrackEvent'; diff --git a/packages/gitbook/src/components/Insights/slimTrackEvent.ts b/packages/gitbook/src/components/Insights/slimTrackEvent.ts new file mode 100644 index 000000000..731f3ff59 --- /dev/null +++ b/packages/gitbook/src/components/Insights/slimTrackEvent.ts @@ -0,0 +1,99 @@ +import type * as api from '@gitbook/api'; +import type { InsightsEventName, TrackEventInput } from './InsightsProvider'; + +export type SlimTrackEventInput = + | { t: 'pv' } + | { t: 'so' } + | { t: 'stq'; q: string } + | { t: 'sor'; q: string; r: { s: string; p: string } } + | { t: 'ppf'; f: { r: api.PageFeedbackRating } } + | { t: 'ppfc'; f: { r: api.PageFeedbackRating; c: string } } + | { t: 'aq'; q: string } + | { t: 'lc'; l: { t: api.ContentRef; p: api.SiteInsightsLinkPosition } } + | { t: 'aco'; o: api.OpenAPIOperationPointer } + | { t: 'acr'; o: api.OpenAPIOperationPointer } + | { t: 'tc'; p: api.SiteInsightsTrademarkPlacement } + | { t: 'ac'; a: api.SiteInsightsAd } + | { t: 'ad'; a: api.SiteInsightsAd }; + +export function expandSlimTrackEvent( + input: SlimTrackEventInput +): TrackEventInput { + switch (input.t) { + case 'pv': + return { type: 'page_view' }; + case 'so': + return { type: 'search_open' }; + case 'stq': + return { type: 'search_type_query', query: input.q }; + case 'sor': + return { + type: 'search_open_result', + query: input.q, + result: { spaceId: input.r.s, pageId: input.r.p }, + }; + case 'ppf': + return { type: 'page_post_feedback', feedback: { rating: input.f.r } }; + case 'ppfc': + return { + type: 'page_post_feedback_comment', + feedback: { rating: input.f.r, comment: input.f.c }, + }; + case 'aq': + return { type: 'ask_question', query: input.q }; + case 'lc': + return { + type: 'link_click', + link: { target: input.l.t, position: input.l.p }, + }; + case 'aco': + return { type: 'api_client_open', operation: input.o }; + case 'acr': + return { type: 'api_client_request', operation: input.o }; + case 'tc': + return { type: 'trademark_click', placement: input.p }; + case 'ac': + return { type: 'ad_click', ad: input.a }; + case 'ad': + return { type: 'ad_display', ad: input.a }; + default: + throw new Error('Unknown slim event'); + } +} + +export function toSlimTrackEvent(event: TrackEventInput): SlimTrackEventInput { + switch (event.type) { + case 'page_view': + return { t: 'pv' }; + case 'search_open': + return { t: 'so' }; + case 'search_type_query': + return { t: 'stq', q: event.query }; + case 'search_open_result': + return { + t: 'sor', + q: event.query, + r: { s: event.result.spaceId, p: event.result.pageId }, + }; + case 'page_post_feedback': + return { t: 'ppf', f: { r: event.feedback.rating } }; + case 'page_post_feedback_comment': + return { t: 'ppfc', f: { r: event.feedback.rating, c: event.feedback.comment } }; + case 'ask_question': + return { t: 'aq', q: event.query }; + case 'link_click': + return { t: 'lc', l: { t: event.link.target, p: event.link.position } }; + case 'api_client_open': + return { t: 'aco', o: event.operation }; + case 'api_client_request': + return { t: 'acr', o: event.operation }; + case 'trademark_click': + return { t: 'tc', p: event.placement }; + case 'ad_click': + return { t: 'ac', a: event.ad }; + case 'ad_display': + return { t: 'ad', a: event.ad }; + default: + throw new Error('Unknown event type'); + } +} diff --git a/packages/gitbook/src/components/PageBody/PageBodyBlankslate.tsx b/packages/gitbook/src/components/PageBody/PageBodyBlankslate.tsx index 86187cea5..60cc2f2c3 100644 --- a/packages/gitbook/src/components/PageBody/PageBodyBlankslate.tsx +++ b/packages/gitbook/src/components/PageBody/PageBodyBlankslate.tsx @@ -8,6 +8,7 @@ import type { GitBookAnyContext } from '@v2/lib/context'; import { Card } from '@/components/primitives'; import { resolveContentRef } from '@/lib/references'; import { tcls } from '@/lib/tailwind'; +import { toSlimTrackEvent } from '../Insights'; import { PageIcon } from '../PageIcon'; @@ -48,13 +49,13 @@ export async function PageBodyBlankslate(props: { leadingIcon={icon} title={child.title} href={resolved.href} - insights={{ + insights={toSlimTrackEvent({ type: 'link_click', link: { target: child.target, position: SiteInsightsLinkPosition.Content, }, - }} + })} /> ); } diff --git a/packages/gitbook/src/components/PageFeedback/PageFeedbackForm.tsx b/packages/gitbook/src/components/PageFeedback/PageFeedbackForm.tsx index 2c15f5283..2a0e00963 100644 --- a/packages/gitbook/src/components/PageFeedback/PageFeedbackForm.tsx +++ b/packages/gitbook/src/components/PageFeedback/PageFeedbackForm.tsx @@ -32,9 +32,7 @@ export function PageFeedbackForm(props: { trackEvent({ type: 'page_post_feedback', - feedback: { - rating, - }, + feedback: { rating }, }); }; @@ -43,10 +41,7 @@ export function PageFeedbackForm(props: { trackEvent({ type: 'page_post_feedback_comment', - feedback: { - rating, - comment, - }, + feedback: { rating, comment }, }); }; diff --git a/packages/gitbook/src/components/Search/SearchPageResultItem.tsx b/packages/gitbook/src/components/Search/SearchPageResultItem.tsx index aa97e621a..b837e7be8 100644 --- a/packages/gitbook/src/components/Search/SearchPageResultItem.tsx +++ b/packages/gitbook/src/components/Search/SearchPageResultItem.tsx @@ -2,6 +2,7 @@ import { tcls } from '@/lib/tailwind'; import { Icon, type IconName } from '@gitbook/icons'; import React from 'react'; +import { toSlimTrackEvent } from '../Insights'; import { Link } from '../primitives'; import { HighlightQuery } from './HighlightQuery'; import type { ComputedPageResult } from './server-actions'; @@ -45,14 +46,11 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt ? ['is-active', 'bg-primary', 'text-contrast-primary', 'hover:bg-primary-hover'] : null )} - insights={{ + insights={toSlimTrackEvent({ type: 'search_open_result', query, - result: { - pageId: item.pageId, - spaceId: item.spaceId, - }, - }} + result: { pageId: item.pageId, spaceId: item.spaceId }, + })} >
, keyof NextLinkProps> & @@ -14,11 +14,7 @@ type BaseLinkProps = Omit, keyof N export type LinkInsightsProps = { /** Event to track when the link is clicked. */ - insights?: - | TrackEventInput<'ad_click'> - | TrackEventInput<'link_click'> - | TrackEventInput<'trademark_click'> - | TrackEventInput<'search_open_result'>; + insights?: SlimTrackEventInput; }; export type LinkProps = Omit & @@ -44,7 +40,7 @@ export const Link = React.forwardRef(function Link( const onClick = (event: React.MouseEvent) => { if (insights) { - trackEvent(insights, undefined, { + trackEvent(expandSlimTrackEvent(insights), undefined, { immediate: isExternal, }); }