11'use client' ;
22
33import inEU from '@segment/in-eu' ;
4+ import { usePathname , useSearchParams } from 'next/navigation' ;
5+ import { Router } from 'next/router' ;
46import Script from 'next/script' ;
7+ import posthog from 'posthog-js' ;
58import { useEffect , useState } from 'react' ;
69
7- export default function Scripts ( ) {
10+ const isProd = process . env . NEXT_PUBLIC_VERCEL_ENV === 'production' ;
11+
12+ function Reo ( ) {
13+ const reoClientId = process . env . NEXT_PUBLIC_REO_CLIENT_ID ;
814 const [ loadReo , setLoadReo ] = useState ( false ) ;
915 const [ afterLoad , setAfterLoad ] = useState ( false ) ;
10- const isProd = process . env . NEXT_PUBLIC_VERCEL_ENV === 'production' ;
1116
1217 useEffect ( ( ) => {
1318 if ( window ) {
@@ -19,18 +24,97 @@ export default function Scripts() {
1924 // @ts -ignore
2025 if ( afterLoad && window . Reo ) {
2126 // @ts -ignore
22- window . Reo . init ( { clientID : 'bf9727b30a874e3' } ) ;
27+ window . Reo . init ( { clientID : reoClientId } ) ;
2328 }
2429 } , [ afterLoad ] ) ;
2530
2631 return (
2732 < div >
28- { isProd && loadReo && (
33+ { reoClientId && isProd && loadReo && (
34+ < Script
35+ src = { `https://static.reo.dev/${ reoClientId } /reo.js` }
36+ onLoad = { ( ) => setAfterLoad ( true ) }
37+ defer
38+ > </ Script >
39+ ) }
40+ </ div >
41+ ) ;
42+ }
43+
44+ function HubSpot ( ) {
45+ const hsId = process . env . NEXT_PUBLIC_HUBSPOT_ID ;
46+ const [ loadHs , setLoadHs ] = useState ( false ) ;
47+ const [ afterLoad , setAfterLoad ] = useState ( false ) ;
48+ const pathname = usePathname ( ) ;
49+ const searchParams = useSearchParams ( ) ;
50+
51+ useEffect ( ( ) => {
52+ if ( window ) {
53+ setLoadHs ( ! inEU ( ) ) ;
54+ }
55+ } , [ loadHs ] ) ;
56+
57+ useEffect ( ( ) => {
58+ // @ts -ignore
59+ const hs = window ?. _hsq ;
60+ if ( afterLoad && pathname && hs ) {
61+ let path = pathname ;
62+ if ( searchParams && searchParams . toString ( ) ) {
63+ path = path + `?${ searchParams . toString ( ) } ` ;
64+ }
65+ hs . push ( [ 'setPath' , path ] ) ;
66+ hs . push ( [ 'trackPageView' ] ) ;
67+ }
68+ } , [ afterLoad , pathname , searchParams ] ) ;
69+
70+ return (
71+ < div >
72+ { hsId && isProd && loadHs && (
2973 < Script
30- src = "https://static.reo.dev/bf9727b30a874e3/reo.js"
74+ id = "hs-script-loader"
75+ async
76+ defer
77+ src = { `//js.hs-scripts.com/${ hsId } .js` }
3178 onLoad = { ( ) => setAfterLoad ( true ) }
3279 > </ Script >
3380 ) }
3481 </ div >
3582 ) ;
3683}
84+
85+ function Posthog ( ) {
86+ useEffect ( ( ) => {
87+ if ( inEU ( ) || ! process . env . NEXT_PUBLIC_POSTHOG_KEY ) {
88+ return ;
89+ }
90+
91+ posthog . init ( process . env . NEXT_PUBLIC_POSTHOG_KEY ?? '' , {
92+ api_host : isProd ? '/ingest' : process . env . NEXT_PUBLIC_POSTHOG_HOST , // See Posthog rewrites in next config
93+ ui_host : process . env . NEXT_PUBLIC_POSTHOG_HOST ,
94+ person_profiles : 'always' ,
95+ loaded : ( posthog ) => {
96+ if ( process . env . NODE_ENV === 'development' ) posthog . debug ( ) ;
97+ } ,
98+ } ) ;
99+
100+ const handleRouteChange = ( ) => posthog ?. capture ( '$pageview' ) ;
101+
102+ Router . events . on ( 'routeChangeComplete' , handleRouteChange ) ;
103+
104+ return ( ) => {
105+ Router . events . off ( 'routeChangeComplete' , handleRouteChange ) ;
106+ } ;
107+ } , [ ] ) ;
108+
109+ return < > </ > ;
110+ }
111+
112+ export default function Scripts ( ) {
113+ return (
114+ < div >
115+ < Reo />
116+ < HubSpot />
117+ < Posthog />
118+ </ div >
119+ ) ;
120+ }
0 commit comments