@@ -6,35 +6,55 @@ import Gleap from "gleap"; // See https://gleap.io/docs/javascript/ and https://
6
6
import { withRouter } from 'react-router-dom' ;
7
7
import { useHistory } from '@docusaurus/router' ;
8
8
import useIsBrowser from '@docusaurus/useIsBrowser' ; // https://docusaurus.io/docs/advanced/ssg#useisbrowser
9
-
9
+ import { PostHogProvider } from 'posthog-js/react' ;
10
+ import posthog from 'posthog-js' ;
11
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext' ;
10
12
11
13
function Root ( { children, location } ) {
12
14
const isBrowser = useIsBrowser ( ) ;
13
-
14
15
const history = useHistory ( ) ;
16
+ const { siteConfig : { customFields} } = useDocusaurusContext ( ) ;
15
17
16
18
useEffect ( ( ) => {
17
- // pass message to dev.near.org (docs is embed there)
19
+ // Pass message to dev.near.org (docs is embedded there)
18
20
const sendMessage = url => parent . postMessage ( { type : 'urlChange' , url } , 'https://dev.near.org/' ) ;
19
21
sendMessage ( location . pathname ) ;
20
22
21
23
const unlisten = history . listen ( loc => sendMessage ( loc . pathname ) ) ;
22
24
return ( ) => { unlisten ( ) } ;
23
25
} , [ history ] ) ;
24
26
25
- if ( isBrowser ) {
26
- const { initRudderAnalytics, recordPageView } = require ( './scripts/rudderstack' ) ;
27
-
28
- Gleap . initialize ( 'K2v3kvAJ5XtPzNYSgk4Ulpe5ptgBkIMv' ) ;
29
-
30
- const rudderAnalytics = initRudderAnalytics ( ) ;
31
- recordPageView ( rudderAnalytics , location . pathname ) ;
32
- }
33
-
34
- return < > { children } </ > ;
27
+ useEffect ( ( ) => {
28
+ if ( isBrowser ) {
29
+ const { initRudderAnalytics, recordPageView } = require ( './scripts/rudderstack' ) ;
30
+
31
+ Gleap . initialize ( 'K2v3kvAJ5XtPzNYSgk4Ulpe5ptgBkIMv' ) ;
32
+
33
+ const rudderAnalytics = initRudderAnalytics ( ) ;
34
+ recordPageView ( rudderAnalytics , location . pathname ) ;
35
+
36
+ // Initialize PostHog
37
+ posthog . init ( customFields . REACT_APP_PUBLIC_POSTHOG_KEY , {
38
+ api_host : customFields . REACT_APP_PUBLIC_POSTHOG_HOST ,
39
+ } ) ;
40
+
41
+ // Track initial page view
42
+ posthog . capture ( '$pageview' ) ;
43
+
44
+ // Track page views on route changes
45
+ history . listen ( ( location ) => {
46
+ posthog . capture ( '$pageview' , { path : location . pathname } ) ;
47
+ } ) ;
48
+ }
49
+ } , [ isBrowser , history ] ) ;
50
+
51
+ return (
52
+ < PostHogProvider client = { posthog } >
53
+ { children }
54
+ </ PostHogProvider >
55
+ ) ;
35
56
}
36
57
37
58
const router = withRouter ( Root ) ;
38
59
39
-
40
- export default router ;
60
+ export default router ;
0 commit comments