Commit 89ba8c29 by luoqi

feat(web): add Baidu analytics to PAC and FridayAI

parent 6a4e0d99
Pipeline #3791 failed in 0 seconds
import { BaiduAnalytics } from '@/components/baidu-analytics';
import type { Metadata, Viewport } from 'next';
import type { ReactNode } from 'react';
import './globals.css';
......@@ -23,6 +24,7 @@ export default function RootLayout({ children }: { children: ReactNode }) {
<AuthSessionBoundary>{children}</AuthSessionBoundary>
{/* ⚠️ 挂在根布局 —— 提示条要能从任何一页弹出来,⛔ 不能每页各挂一个(会叠出多份) */}
<Toaster />
<BaiduAnalytics />
</body>
</html>
);
......
"use client";
import { useEffect } from 'react';
import { usePathname } from 'next/navigation';
const SITE_ID = '731849b080736f1c473e1118c4d729ad';
const PRODUCTION_HOST = 'fai.friday.tech';
type AnalyticsWindow = Window & {
_hmt?: unknown[][];
__pacBaiduLastPage?: string;
};
/** 仅统计生产页面访问;查询参数和动态业务记录编号不进入手动 PV。 */
export function BaiduAnalytics() {
const pathname = usePathname();
useEffect(() => {
if (!pathname || window.location.hostname !== PRODUCTION_HOST) return;
const analytics = window as AnalyticsWindow;
const queue = (analytics._hmt ??= []);
// SPA 自行上报首屏与路由切换,避免自动首屏统计重复。
if (!document.getElementById('baidu-analytics')) {
queue.push(['_setAutoPageview', false]);
const script = document.createElement('script');
script.id = 'baidu-analytics';
script.async = true;
script.src = `https://hm.baidu.com/hm.js?${SITE_ID}`;
script.referrerPolicy = 'no-referrer';
document.head.appendChild(script);
}
const page = pathname
.replace(/^(\/plans)\/[^/]+$/, '$1/:id')
.replace(/^(\/admin\/ai-invocations)\/[^/]+$/, '$1/:id')
.replace(/^(\/host\/appointments)\/[^/]+$/, '$1/:id');
if (analytics.__pacBaiduLastPage === page) return;
analytics.__pacBaiduLastPage = page;
queue.push(['_trackPageview', page]);
}, [pathname]);
return null;
}
import { BaiduAnalytics } from '@/components/baidu-analytics';
import type { Metadata } from 'next';
import { Toaster } from '@/components/ui/toaster';
import './globals.css';
......@@ -13,6 +14,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<body className="min-h-screen bg-background text-foreground antialiased">
{children}
<Toaster />
<BaiduAnalytics />
</body>
</html>
);
......
"use client";
import { useEffect } from 'react';
import { usePathname } from 'next/navigation';
const SITE_ID = '731849b080736f1c473e1118c4d729ad';
const PRODUCTION_HOST = 'pac.friday.tech';
type AnalyticsWindow = Window & {
_hmt?: unknown[][];
__pacBaiduLastPage?: string;
};
/** 仅统计生产页面访问;查询参数和动态业务记录编号不进入手动 PV。 */
export function BaiduAnalytics() {
const pathname = usePathname();
useEffect(() => {
if (!pathname || window.location.hostname !== PRODUCTION_HOST) return;
const analytics = window as AnalyticsWindow;
const queue = (analytics._hmt ??= []);
// SPA 自行上报首屏与路由切换,避免自动首屏统计重复。
if (!document.getElementById('baidu-analytics')) {
queue.push(['_setAutoPageview', false]);
const script = document.createElement('script');
script.id = 'baidu-analytics';
script.async = true;
script.src = `https://hm.baidu.com/hm.js?${SITE_ID}`;
script.referrerPolicy = 'no-referrer';
document.head.appendChild(script);
}
const page = pathname
.replace(/^(\/plans)\/[^/]+$/, '$1/:id')
.replace(/^(\/admin\/ai-invocations)\/[^/]+$/, '$1/:id')
.replace(/^(\/host\/appointments)\/[^/]+$/, '$1/:id');
if (analytics.__pacBaiduLastPage === page) return;
analytics.__pacBaiduLastPage = page;
queue.push(['_trackPageview', page]);
}, [pathname]);
return null;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment