Site Analytics
Configure analytics script injection and event tracking.
MuseMVP integrates Google Analytics (GA4) and Baidu Tongji, with a unified useAnalytics hook that sends custom events to both platforms. Suitable for products targeting both international and Chinese markets.
Injection Strategy
src/modules/analytics/index.tsx
src/modules/html-document/Document.tsx
src/config/index.ts
| File | Purpose |
|---|---|
src/modules/analytics/index.tsx | AnalyticsScript and useAnalytics hook |
src/modules/html-document/Document.tsx | Mount point for AnalyticsScript |
src/config/index.ts | config.analytics source |
Configuration
// src/config/index.ts
analytics: {
googleAnalyticsId: "G-XXXXXXXXXX", // GA4 measurement ID
baiduTongjiId: "", // Baidu Tongji ID; leave empty to skip
}- Google Analytics: Uses @next/third-parties
GoogleAnalyticscomponent - Baidu Tongji: Injected via
next/scriptwith dynamic script
Development Behavior
- In development (
NODE_ENV === "development"), analytics scripts are not injected - Console logs
[AnalyticsScript] local development, skip analytics. - Keeps dev and test traffic out of production data
Event Tracking
Use useAnalytics() to send custom events:
import { useAnalytics } from "@/modules/analytics";
function MyComponent() {
const { trackEvent } = useAnalytics();
const handleClick = () => {
trackEvent("button_click", { button_id: "pricing_cta", page: "home" });
};
return <button onClick={handleClick}>Upgrade</button>;
}Events are sent to:
gtag(Google Analytics)_hmt(Baidu Tongji, whenbaiduTongjiIdis set)
SSR Safety
trackEvent checks typeof window !== "undefined" and does nothing during SSR.
Pre-Launch Checklist
Verify production analytics IDs: googleAnalyticsId and baiduTongjiId should be production IDs.
Confirm scripts load after hydration: use browser Network tab.
Validate key conversion events with Google Analytics Debugger or Baidu real-time reports.
Related Docs
- SEO — Sitemap, metadata, search optimization
- Tech Stack — Analytics integration