fix(analytics): 系统性修复 Google Analytics 数据采集问题

- 修复城市 (not set): 移除 allow_google_signals: false,启用 Google 信号补充地理数据
- 修复 Consent Mode v2: 补充 ad_user_data / ad_personalization 参数
- 修复 wait_for_update 与横幅延迟不匹配: 500ms → 3000ms
- 修复 static export 兼容性: GA 初始化脚本从 client component 移至 layout.tsx head 原生 script 标签
- 修复 pageview 追踪: GA3 风格 gtag('config') → GA4 风格 gtag('event', 'page_view')
- 修复 CookieConsent: 横幅延迟 2000ms → 500ms,同意后补发 pageview
- 修复 PerformanceTracker: FID → INP (Core Web Vitals 2024 更新)
- 修复环境变量命名: NEXT_PUBLIC_GA_ID → NEXT_PUBLIC_GA_MEASUREMENT_ID
- 清理 deploy-dist.sh 冗余 server/app 分支逻辑
- 新增部署产物 GA 脚本嵌入验证
This commit is contained in:
张翔
2026-04-29 13:44:44 +08:00
parent 5d14a0780c
commit fb888a673f
8 changed files with 157 additions and 115 deletions
+28 -12
View File
@@ -10,14 +10,6 @@ declare global {
}
}
export const pageview = (url: string) => {
if (typeof window !== 'undefined' && window.gtag && GA_MEASUREMENT_ID) {
window.gtag('config', GA_MEASUREMENT_ID, {
page_path: url,
});
}
};
export const event = (action: string, category: string, label?: string, value?: number) => {
if (typeof window !== 'undefined' && window.gtag && GA_MEASUREMENT_ID) {
window.gtag('event', action, {
@@ -28,6 +20,30 @@ export const event = (action: string, category: string, label?: string, value?:
}
};
export const hasAnalyticsConsent = (): boolean => {
if (typeof window === 'undefined') {return false;}
try {
const stored = localStorage.getItem('cookie_preferences');
if (stored) {
const prefs = JSON.parse(stored) as CookiePreferences;
return prefs.analytics === true;
}
} catch {
return false;
}
return false;
};
export const trackPageView = (pageTitle: string, pagePath: string) => {
if (typeof window !== 'undefined' && window.gtag && GA_MEASUREMENT_ID) {
window.gtag('event', 'page_view', {
page_title: pageTitle,
page_location: window.location.origin + pagePath,
page_path: pagePath,
});
}
};
export const trackContactForm = (formData: Record<string, string>) => {
event('generate_lead', 'engagement', 'contact_form_submission');
@@ -44,10 +60,6 @@ export const trackButtonClick = (buttonName: string, location: string) => {
event('click', 'button', `${location}_${buttonName}`);
};
export const trackPageView = (pageTitle: string, _pagePath: string) => {
event('page_view', 'navigation', pageTitle);
};
export const trackConversion = (conversionName: string, value?: number) => {
if (typeof window !== 'undefined' && window.gtag && GA_MEASUREMENT_ID) {
window.gtag('event', 'conversion', {
@@ -161,6 +173,8 @@ export const updateConsent = (granted: boolean) => {
window.gtag('consent', 'update', {
analytics_storage: granted ? 'granted' : 'denied',
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
});
}
};
@@ -170,6 +184,8 @@ export const updateConsentDetailed = (preferences: CookiePreferences) => {
window.gtag('consent', 'update', {
analytics_storage: preferences.analytics ? 'granted' : 'denied',
ad_storage: preferences.marketing ? 'granted' : 'denied',
ad_user_data: preferences.marketing ? 'granted' : 'denied',
ad_personalization: preferences.marketing ? 'granted' : 'denied',
functionality_storage: 'granted',
personalization_storage: preferences.marketing ? 'granted' : 'denied',
security_storage: 'granted',