Skip to content

Commit 9c86728

Browse files
committed
dynamic import site analytics
1 parent 7bc27bb commit 9c86728

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

docs/site/components/analytics/analytics-scripts.tsx

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1-
import * as siteAnalyticsModule from "../../lib/site-analytics";
1+
"use client";
22

3-
console.log("TESTING siteAnalyticsModule", siteAnalyticsModule);
3+
import { useState, useEffect, type JSX } from "react";
4+
5+
type SiteAnalytics = {
6+
analytics: any;
7+
consent: any;
8+
};
9+
10+
function useSiteAnalytics(): SiteAnalytics | null {
11+
const [result, setResult] = useState<SiteAnalytics | null>(null);
12+
13+
useEffect(() => {
14+
let didCancel = false;
15+
16+
import("@vercel/site-analytics")
17+
.then((mod) => {
18+
console.log("Vercel Site Analytics module loaded successfully.", mod);
19+
})
20+
.catch(() => {
21+
if (!didCancel) setResult(null);
22+
});
23+
24+
return () => {
25+
didCancel = true;
26+
};
27+
});
28+
29+
return result;
30+
}
431

532
export function AnalyticsScripts({
633
children,
734
}: {
835
children?: React.ReactNode;
936
}): JSX.Element {
37+
useSiteAnalytics();
38+
1039
return (
1140
<>
1241
<div>ANALYTICS</div>

0 commit comments

Comments
 (0)