1
1
'use client' ;
2
2
3
- import type { RequestSpaceTrackPageView } from '@gitbook/api' ;
3
+ import type { RequestSiteTrackPageView , RequestSpaceTrackPageView } from '@gitbook/api' ;
4
4
import cookies from 'js-cookie' ;
5
5
import * as React from 'react' ;
6
6
7
7
import { getVisitorId } from '@/lib/analytics' ;
8
+ import { SiteContentPointer } from '@/lib/api' ;
8
9
9
10
/**
10
11
* Track the page view for the current page to integrations.
11
12
*/
12
13
export function TrackPageView ( props : {
13
14
apiHost : string ;
15
+ sitePointer ?: Pick < SiteContentPointer , 'siteId' | 'organizationId' > ;
14
16
spaceId : string ;
15
17
pageId : string | undefined ;
16
18
} ) {
17
- const { apiHost, spaceId, pageId } = props ;
19
+ const { apiHost, sitePointer , spaceId, pageId } = props ;
18
20
19
21
React . useEffect ( ( ) => {
20
- trackPageView ( apiHost , spaceId , pageId ) ;
21
- } , [ apiHost , spaceId , pageId ] ) ;
22
+ trackPageView ( { apiHost, sitePointer , spaceId, pageId } ) ;
23
+ } , [ apiHost , spaceId , pageId , sitePointer ] ) ;
22
24
23
25
return null ;
24
26
}
25
27
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
+
26
64
let latestPageId : string | undefined | null = null ;
27
65
28
66
/**
29
67
* Track the page view for the current page to GitBook.
30
68
* We don't use the API client to avoid shipping 80kb of JS to the client.
31
69
* And instead use a simple fetch.
32
70
*/
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 ;
34
78
if ( pageId === latestPageId ) {
35
79
// The hook can be called multiple times, we only want to track once.
36
80
return ;
@@ -39,7 +83,7 @@ async function trackPageView(apiHost: string, spaceId: string, pageId: string |
39
83
latestPageId = pageId ;
40
84
41
85
const visitorId = await getVisitorId ( ) ;
42
- const body : RequestSpaceTrackPageView = {
86
+ const sharedTrackedProps = {
43
87
url : window . location . href ,
44
88
pageId,
45
89
visitor : {
@@ -51,17 +95,17 @@ async function trackPageView(apiHost: string, spaceId: string, pageId: string |
51
95
referrer : document . referrer ,
52
96
} ;
53
97
54
- const url = new URL ( apiHost ) ;
55
- url . pathname = `/v1/spaces/${ spaceId } /insights/track_view` ;
56
-
57
98
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 } ) ;
65
109
} catch ( error ) {
66
110
console . error ( 'Failed to track page view' , error ) ;
67
111
}
0 commit comments