Skip to content

Commit 49c9d59

Browse files
committed
feat: 集成百度统计
1 parent 45afdc6 commit 49c9d59

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import GitHubRibbon from './components/GitHubRibbon'
1010
import PageTitle from './components/PageTitle'
1111
import './gh-fork-ribbon.css';
1212
import './styles/github-ribbon-fix.css';
13+
import BaiduAnalytics from './components/BaiduAnalytics'
1314

1415
const App = () => {
1516
return (
1617
<Router>
1718
<div className="App">
1819
<PageTitle />
1920
<GitHubRibbon repositoryUrl="https://github.com/JSREP/crawler-leetcode" />
21+
<BaiduAnalytics siteId="b4fcc22834ded2c9864e661e6a5a634d" />
2022
<NavBar/>
2123
<div className="content-wrapper" style={{ padding: '20px 0' }}>
2224
<Routes>

src/components/BaiduAnalytics.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useEffect } from 'react';
2+
3+
declare global {
4+
interface Window {
5+
_hmt: any[];
6+
}
7+
}
8+
9+
interface BaiduAnalyticsProps {
10+
siteId: string;
11+
}
12+
13+
const BaiduAnalytics: React.FC<BaiduAnalyticsProps> = ({ siteId }) => {
14+
useEffect(() => {
15+
// 初始化_hmt数组
16+
window._hmt = window._hmt || [];
17+
18+
// 创建百度统计脚本
19+
const script = document.createElement('script');
20+
script.async = true;
21+
script.src = `https://hm.baidu.com/hm.js?${siteId}`;
22+
23+
// 添加脚本到页面
24+
document.head.appendChild(script);
25+
26+
// 清理函数
27+
return () => {
28+
document.head.removeChild(script);
29+
};
30+
}, [siteId]);
31+
32+
return null;
33+
};
34+
35+
export default BaiduAnalytics;

0 commit comments

Comments
 (0)