React + React Router + Vite
应用程序正在生产中运行。现在我正在尝试将其与 Google Analytics 集成。 这是我的代码:
import { getAnalytics, logEvent } from 'firebase/analytics';
import { initializeApp } from 'firebase/app';
const app = initializeApp({
apiKey: String(import.meta.env.VITE_FIREBASE_API_KEY),
authDomain: String(import.meta.env.VITE_FIREBASE_AUTH_DOMAIN),
projectId: String(import.meta.env.VITE_FIREBASE_PROJECT_ID),
storageBucket: String(import.meta.env.VITE_FIREBASE_STORAGE_BUCKET),
messagingSenderId: String(import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID),
appId: String(import.meta.env.VITE_FIREBASE_APP_ID),
measurementId: String(import.meta.env.VITE_FIREBASE_MEASUREMENT_ID),
});
const analytics = getAnalytics(app);
export const logPageView = (pageTitle: string, pagePath: string): void => {
logEvent(analytics, 'page_view', { page_title: pageTitle, page_path: pagePath });
};
onst withPageTitle = (component: JSX.Element, pageTitle: string): JSX.Element => {
return (
<>
<PageTitle title={pageTitle} />
{component}
</>
);
};
const routes = createRoutesFromElements(
<Route path="/" element={<Index />} errorElement={withPageTitle(<UnexpectedError />, 'Erro Desconhecido')}>
<Route element={<AppPages />}>
<Route index element={<div />} />
<Route path="login" element={withPageTitle(<SignIn />, 'Login')} />
<Route path="sign_up" element={withPageTitle(<SignUp />, 'Crie sua Conta')} />
<Route path="verify" element={withPageTitle(<VerifyEmail />, 'Verifique sua Conta')} />
...
</Route>
<Route path="logout" element={<SignOut />} />
<Route path="*" element={withPageTitle(<NotFound />, 'Página Não Encontrada')} />
</Route>,
);
export function PageTitle({ title }: PageTitleProps): null {
const location = useLocation();
useEffect(() => {
document.title = title;
logPageView(title, location.pathname);
}, [location, title]);
return null;
}
http://localhost:3000
一切正常。我可以看到
collect
请求已发送至 Google。当我在本地生成并运行生产 docker 映像时,它也可以工作。但是,当我将图像推送到生产环境并将应用程序部署在我的域后面时,我没有看到任何发送的
collect
请求。我尝试将网站 URL 添加到数据流并将 API 限制为网站 URL,但没有任何效果。
为什么 Firebase Analytics 不能仅在我的生产网站上运行?我可以做些什么来调试和检查问题所在吗?我在这里错过了什么吗?