feat(analytics): 增强 Google Analytics 隐私合规与追踪功能

- 修复 allow_google_signals 配置为 false,禁用跨设备追踪
- 升级 Cookie 同意组件,支持三级偏好控制(必要/分析/营销)
- 新增滚动深度追踪组件,追踪 25%/50%/75%/100% 里程碑
- 更新隐私政策,新增 Cookie 和网站分析工具章节
- 新增细化同意管理函数,支持 PIPL 合规
This commit was merged in pull request #10.
This commit is contained in:
张翔
2026-04-22 22:09:38 +08:00
parent 96dddeb20b
commit 95f246fa36
6 changed files with 387 additions and 54 deletions
+48
View File
@@ -149,6 +149,13 @@ export const trackProductView = (productId: string, productName: string) => {
}
};
export interface CookiePreferences {
necessary: boolean;
analytics: boolean;
marketing: boolean;
timestamp: number;
}
export const updateConsent = (granted: boolean) => {
if (typeof window !== 'undefined' && window.gtag) {
window.gtag('consent', 'update', {
@@ -157,3 +164,44 @@ export const updateConsent = (granted: boolean) => {
});
}
};
export const updateConsentDetailed = (preferences: CookiePreferences) => {
if (typeof window !== 'undefined' && window.gtag) {
window.gtag('consent', 'update', {
analytics_storage: preferences.analytics ? 'granted' : 'denied',
ad_storage: preferences.marketing ? 'granted' : 'denied',
functionality_storage: 'granted',
personalization_storage: preferences.marketing ? 'granted' : 'denied',
security_storage: 'granted',
});
}
};
export const getStoredPreferences = (): CookiePreferences | null => {
if (typeof window === 'undefined') {
return null;
}
try {
const stored = localStorage.getItem('cookie_preferences');
if (stored) {
return JSON.parse(stored) as CookiePreferences;
}
} catch {
return null;
}
return null;
};
export const storePreferences = (preferences: CookiePreferences) => {
if (typeof window !== 'undefined') {
localStorage.setItem('cookie_preferences', JSON.stringify(preferences));
}
};
export const getDefaultPreferences = (): CookiePreferences => ({
necessary: true,
analytics: false,
marketing: false,
timestamp: Date.now(),
});