mirror of
https://github.com/PlayEdu/frontend.git
synced 2025-12-23 22:19:39 +08:00
引入谷歌分析
This commit is contained in:
30
src/App.tsx
30
src/App.tsx
@@ -1,31 +1,43 @@
|
||||
import { useRoutes } from "react-router-dom";
|
||||
import { Suspense, useEffect } from "react";
|
||||
import ReactGA from "react-ga";
|
||||
import { useLocation, useRoutes } from "react-router-dom";
|
||||
import routes from "./routes";
|
||||
import "./App.scss";
|
||||
import { Suspense } from "react";
|
||||
import LoadingPage from "./pages/loading";
|
||||
import { user } from "./api/index";
|
||||
import { getToken } from "./utils/index";
|
||||
import { useDispatch } from "react-redux";
|
||||
import { loginAction } from "./store/user/loginUserSlice";
|
||||
|
||||
function App() {
|
||||
const Views = () => useRoutes(routes);
|
||||
const G_ID = import.meta.env.VITE_G_ID || "";
|
||||
if (G_ID) {
|
||||
ReactGA.initialize(G_ID);
|
||||
}
|
||||
|
||||
const App = () => {
|
||||
const dispatch = useDispatch();
|
||||
const getUser = () => {
|
||||
const Views = () => useRoutes(routes);
|
||||
|
||||
if (getToken()) {
|
||||
user.detail().then((res: any) => {
|
||||
const data = res.data;
|
||||
dispatch(loginAction(data));
|
||||
});
|
||||
};
|
||||
if (getToken()) {
|
||||
getUser();
|
||||
}
|
||||
|
||||
const location = useLocation();
|
||||
useEffect(() => {
|
||||
if (!G_ID) {
|
||||
return;
|
||||
}
|
||||
ReactGA.pageview(location.pathname + location.search);
|
||||
}, [location]);
|
||||
|
||||
return (
|
||||
<Suspense fallback={<LoadingPage />}>
|
||||
<Views />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default App;
|
||||
|
||||
Reference in New Issue
Block a user