11'use client' ;
22
3- import type { RequestSpaceTrackPageView } from '@gitbook/api' ;
3+ import type { RequestSiteTrackPageView , RequestSpaceTrackPageView } from '@gitbook/api' ;
44import cookies from 'js-cookie' ;
55import * as React from 'react' ;
66
77import { getVisitorId } from '@/lib/analytics' ;
8+ import { SiteContentPointer } from '@/lib/api' ;
89
910/**
1011 * Track the page view for the current page to integrations.
1112 */
1213export function TrackPageView ( props : {
1314 apiHost : string ;
15+ sitePointer ?: Pick < SiteContentPointer , 'siteId' | 'organizationId' > ;
1416 spaceId : string ;
1517 pageId : string | undefined ;
1618} ) {
17- const { apiHost, spaceId, pageId } = props ;
19+ const { apiHost, sitePointer , spaceId, pageId } = props ;
1820
1921 React . useEffect ( ( ) => {
20- trackPageView ( apiHost , spaceId , pageId ) ;
21- } , [ apiHost , spaceId , pageId ] ) ;
22+ trackPageView ( { apiHost, sitePointer , spaceId, pageId } ) ;
23+ } , [ apiHost , spaceId , pageId , sitePointer ] ) ;
2224
2325 return null ;
2426}
2527
28+ async function sendSpaceTrackPageViewRequest ( args : {
29+ apiHost : string ;
30+ spaceId : string ;
31+ body : RequestSpaceTrackPageView ;
32+ } ) {
33+ const { apiHost, spaceId, body } = args ;
34+ const url = new URL ( apiHost ) ;
35+ url . pathname = `/v1/spaces/${ spaceId } /insights/track_view` ;
36+
37+ await fetch ( url , {
38+ method : 'POST' ,
39+ headers : {
40+ 'Content-Type' : 'application/json' ,
41+ } ,
42+ body : JSON . stringify ( body ) ,
43+ } ) ;
44+ }
45+
46+ async function sendSiteTrackPageViewRequest ( args : {
47+ apiHost : string ;
48+ sitePointer : Pick < SiteContentPointer , 'siteId' | 'organizationId' > ;
49+ body : RequestSiteTrackPageView ;
50+ } ) {
51+ const { apiHost, sitePointer, body } = args ;
52+ const url = new URL ( apiHost ) ;
53+ url . pathname = `/v1/orgs/${ sitePointer . organizationId } /sites/${ sitePointer . siteId } /insights/track_view` ;
54+
55+ await fetch ( url , {
56+ method : 'POST' ,
57+ headers : {
58+ 'Content-Type' : 'application/json' ,
59+ } ,
60+ body : JSON . stringify ( body ) ,
61+ } ) ;
62+ }
63+
2664let latestPageId : string | undefined | null = null ;
2765
2866/**
2967 * Track the page view for the current page to GitBook.
3068 * We don't use the API client to avoid shipping 80kb of JS to the client.
3169 * And instead use a simple fetch.
3270 */
33- async function trackPageView ( apiHost : string , spaceId : string , pageId : string | undefined ) {
71+ async function trackPageView ( args : {
72+ apiHost : string ;
73+ sitePointer ?: Pick < SiteContentPointer , 'siteId' | 'organizationId' > ;
74+ spaceId : string ;
75+ pageId : string | undefined ;
76+ } ) {
77+ const { apiHost, sitePointer, pageId, spaceId } = args ;
3478 if ( pageId === latestPageId ) {
3579 // The hook can be called multiple times, we only want to track once.
3680 return ;
@@ -39,7 +83,7 @@ async function trackPageView(apiHost: string, spaceId: string, pageId: string |
3983 latestPageId = pageId ;
4084
4185 const visitorId = await getVisitorId ( ) ;
42- const body : RequestSpaceTrackPageView = {
86+ const sharedTrackedProps = {
4387 url : window . location . href ,
4488 pageId,
4589 visitor : {
@@ -51,17 +95,17 @@ async function trackPageView(apiHost: string, spaceId: string, pageId: string |
5195 referrer : document . referrer ,
5296 } ;
5397
54- const url = new URL ( apiHost ) ;
55- url . pathname = `/v1/spaces/${ spaceId } /insights/track_view` ;
56-
5798 try {
58- await fetch ( url , {
59- method : 'POST' ,
60- headers : {
61- 'Content-Type' : 'application/json' ,
62- } ,
63- body : JSON . stringify ( body ) ,
64- } ) ;
99+ sitePointer
100+ ? await sendSiteTrackPageViewRequest ( {
101+ apiHost,
102+ sitePointer,
103+ body : {
104+ ...sharedTrackedProps ,
105+ spaceId,
106+ } ,
107+ } )
108+ : await sendSpaceTrackPageViewRequest ( { apiHost, spaceId, body : sharedTrackedProps } ) ;
65109 } catch ( error ) {
66110 console . error ( 'Failed to track page view' , error ) ;
67111 }
0 commit comments