File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ "use client" ;
2+
3+ import { usePathname } from "next/navigation" ;
4+ import { useEffect } from "react" ;
5+
6+ export function DocsPageViewTracker ( ) {
7+ const pathname = usePathname ( ) ;
8+
9+ useEffect ( ( ) => {
10+ if ( ! pathname ) return ;
11+
12+ const key = `pv_reported:${ pathname } ` ;
13+ if ( sessionStorage . getItem ( key ) ) return ;
14+
15+ sessionStorage . setItem ( key , "1" ) ;
16+
17+ const token =
18+ typeof window !== "undefined" ? localStorage . getItem ( "satoken" ) : null ;
19+ const headers : Record < string , string > = {
20+ "Content-Type" : "application/json" ,
21+ } ;
22+ if ( token ) headers [ "x-satoken" ] = token ;
23+
24+ fetch ( "/api/analytics" , {
25+ method : "POST" ,
26+ headers,
27+ body : JSON . stringify ( {
28+ eventType : "page_view" ,
29+ eventData : { path : pathname , title : document . title } ,
30+ } ) ,
31+ } ) . catch ( ( ) => { } ) ;
32+ } , [ pathname ] ) ;
33+
34+ return null ;
35+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import type { ReactNode } from "react";
55import { DocsRouteFlag } from "@/app/components/RouteFlags" ;
66import type { PageTree } from "fumadocs-core/server" ;
77import { CopyTracking } from "@/app/components/CopyTracking" ;
8+ import { DocsPageViewTracker } from "@/app/components/DocsPageViewTracker" ;
89
910function pruneEmptyFolders ( root : PageTree . Root ) : PageTree . Root {
1011 const transformNode = ( node : PageTree . Node ) : PageTree . Node | null => {
@@ -71,6 +72,7 @@ export default async function Layout({ children }: { children: ReactNode }) {
7172 { /* Add a class on <html> while in docs to adjust global backgrounds */ }
7273 < CopyTracking />
7374 < DocsRouteFlag />
75+ < DocsPageViewTracker />
7476 < DocsLayout
7577 tree = { tree }
7678 { ...options }
You can’t perform that action at this time.
0 commit comments