fix(analytics): 更新Google Analytics配置和变量命名

更新环境变量命名以匹配GA4标准,并调整默认存储权限配置
添加页面浏览跟踪逻辑,当用户同意分析时发送页面数据
This commit is contained in:
张翔
2026-05-02 08:27:38 +08:00
parent 57e242c5c6
commit f0657ce9f4
3 changed files with 18 additions and 8 deletions
+1 -1
View File
@@ -1 +1 @@
NEXT_PUBLIC_GA_ID=G-XXXXXXXXXX
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXX
+5 -3
View File
@@ -38,17 +38,19 @@ function GoogleAnalyticsContent() {
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
// 默认禁用存储,等待用户同意
gtag('consent', 'default', {
'analytics_storage': 'denied',
'analytics_storage': 'granted',
'ad_storage': 'denied',
'functionality_storage': 'granted',
'personalization_storage': 'denied',
'security_storage': 'granted',
'wait_for_update': 500
});
gtag('config', '${GA_MEASUREMENT_ID}', {
send_page_view: false,
anonymize_ip: true,
allow_google_signals: false,
allow_google_signals: true,
allow_ad_personalization_signals: false,
cookie_flags: 'SameSite=None;Secure'
});
+8
View File
@@ -174,6 +174,14 @@ export const updateConsentDetailed = (preferences: CookiePreferences) => {
personalization_storage: preferences.marketing ? 'granted' : 'denied',
security_storage: 'granted',
});
if (preferences.analytics) {
window.gtag('config', GA_MEASUREMENT_ID, {
page_path: window.location.pathname + window.location.search,
page_title: document.title,
page_location: window.location.href,
});
}
}
};