Skip to content

feat(analytics): draft: dynamic import of @vercel/site-analytics #10274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
45 changes: 45 additions & 0 deletions docs/site/components/analytics/analytics-scripts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use client";

import { useState, useEffect, type JSX } from "react";

type SiteAnalytics = {
analytics: any;
consent: any;
};

function useSiteAnalytics(): SiteAnalytics | null {
const [result, setResult] = useState<SiteAnalytics | null>(null);

useEffect(() => {
let didCancel = false;

import("@vercel/site-analytics")
.then((mod) => {
console.log("Vercel Site Analytics module loaded successfully.", mod);
})
.catch(() => {
if (!didCancel) setResult(null);
});

return () => {
didCancel = true;
};
});

return result;
}

export function AnalyticsScripts({
children,
}: {
children?: React.ReactNode;
}): JSX.Element {
useSiteAnalytics();

return (
<>
<div>ANALYTICS</div>
{children}
</>
);
}
17 changes: 10 additions & 7 deletions docs/site/components/root-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import type { ReactNode } from "react";
import { SearchDialog } from "@/components/search-dialog";
import { LocalStorageProvider } from "./local-storage-hook";
import { TopLevelMobileMenuProvider } from "./docs-layout/use-mobile-menu-context";
import { AnalyticsScripts } from "./analytics/analytics-scripts";

export function RootProvider({
children,
}: {
children: ReactNode;
}): JSX.Element {
return (
<TopLevelMobileMenuProvider>
<LocalStorageProvider>
<FumaRootProvider search={{ SearchDialog }}>
{children}
</FumaRootProvider>
</LocalStorageProvider>
</TopLevelMobileMenuProvider>
<AnalyticsScripts>
<TopLevelMobileMenuProvider>
<LocalStorageProvider>
<FumaRootProvider search={{ SearchDialog }}>
{children}
</FumaRootProvider>
</LocalStorageProvider>
</TopLevelMobileMenuProvider>
</AnalyticsScripts>
);
}
3 changes: 3 additions & 0 deletions docs/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
"tsx": "^4.6.2",
"typescript": "5.6.3"
},
"optionalDependencies": {
"@vercel/site-analytics": "1.0.5"
},
"engines": {
"node": "20.x"
}
Expand Down
36 changes: 35 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading