Skip to content

Commit abded8b

Browse files
committed
Add script configuration
1 parent ee4306d commit abded8b

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

components/scripts.tsx

+59-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
'use client';
22

33
import inEU from '@segment/in-eu';
4+
import { usePathname, useSearchParams } from 'next/navigation';
45
import Script from 'next/script';
56
import { useEffect, useState } from 'react';
67

7-
export default function Scripts() {
8+
const isProd = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';
9+
10+
function Reo() {
11+
const reoClientId = process.env.NEXT_PUBLIC_REO_CLIENT_ID;
812
const [loadReo, setLoadReo] = useState(false);
913
const [afterLoad, setAfterLoad] = useState(false);
10-
const isProd = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';
1114

1215
useEffect(() => {
1316
if (window) {
@@ -19,18 +22,69 @@ export default function Scripts() {
1922
// @ts-ignore
2023
if (afterLoad && window.Reo) {
2124
// @ts-ignore
22-
window.Reo.init({ clientID: 'bf9727b30a874e3' });
25+
window.Reo.init({ clientID: reoClientId });
2326
}
2427
}, [afterLoad]);
2528

2629
return (
2730
<div>
28-
{isProd && loadReo && (
31+
{reoClientId && isProd && loadReo && (
2932
<Script
30-
src="https://static.reo.dev/bf9727b30a874e3/reo.js"
33+
src={`https://static.reo.dev/${reoClientId}/reo.js`}
3134
onLoad={() => setAfterLoad(true)}
35+
defer
3236
></Script>
3337
)}
3438
</div>
3539
);
3640
}
41+
42+
function HubSpot() {
43+
const hsId = process.env.NEXT_PUBLIC_HUBSPOT_ID;
44+
const [loadHs, setLoadHs] = useState(false);
45+
const [afterLoad, setAfterLoad] = useState(false);
46+
const pathname = usePathname();
47+
const searchParams = useSearchParams();
48+
49+
useEffect(() => {
50+
if (window) {
51+
setLoadHs(!inEU());
52+
}
53+
}, [loadHs]);
54+
55+
useEffect(() => {
56+
// @ts-ignore
57+
const hs = window?._hsq;
58+
if (afterLoad && pathname && hs) {
59+
let path = pathname;
60+
if (searchParams && searchParams.toString()) {
61+
path = path + `?${searchParams.toString()}`;
62+
}
63+
hs.push(['setPath', path]);
64+
hs.push(['trackPageView']);
65+
}
66+
}, [afterLoad, pathname, searchParams]);
67+
68+
return (
69+
<div>
70+
{hsId && isProd && loadHs && (
71+
<Script
72+
id="hs-script-loader"
73+
async
74+
defer
75+
src={`//js.hs-scripts.com/${hsId}.js`}
76+
onLoad={() => setAfterLoad(true)}
77+
></Script>
78+
)}
79+
</div>
80+
);
81+
}
82+
83+
export default function Scripts() {
84+
return (
85+
<div>
86+
<Reo />
87+
<HubSpot />
88+
</div>
89+
);
90+
}

0 commit comments

Comments
 (0)