fix(analytics): 分析 Cookie 改 opt-in,后台不加载 GA(DEF-09)

两处独立问题:

① 默认同意。`DEFAULT_PREFERENCES.analytics` 是 true,且 GA inline snippet 的
   `consent default` 直接写 `analytics_storage: 'granted'` ⇒ **未同意、乃至已拒绝
   的用户照样被采集**。改为默认 denied;同时在 `gtag('config')` 之前从
   localStorage 回放一次已存偏好 —— 否则「上一访次同意过」的用户会退化,
   因为 `updateConsentDetailed` 在 `window.gtag` 尚未定义时会被静默丢弃。

② 后台加载 GA。`/admin` 没有 CookieConsent 条,管理员也就没有表达同意的入口,
   但 GA 脚本照常挂载、每次点击都进 dataLayer。新增 `shouldRenderAnalytics()`,
   `/admin*` 返回 null。

判别性测试(各自负对照已做):
- 默认改回 granted ⇒ 「默认 consent 为 denied」转红
- `shouldRenderAnalytics` 改成恒 true ⇒ 「/admin 不加载 GA」转红
- `getDefaultPreferences().analytics` 断言同步改为 false
This commit is contained in:
zhangxiang committed 2026-11-25 09:00:03 +08:00
1 parent b8b7da975d
commit fb14bf70d3
6 files changed
+72 -6

No files matched your search

+2 -1
View File
@@ -39,7 +39,8 @@ describe('Analytics', () => {
it('should return default preferences', () => {
const prefs = analytics.getDefaultPreferences();
expect(prefs.necessary).toBe(true);
expect(prefs.analytics).toBe(true);
// DEF-09:分析 Cookie 改为 opt-in —— 未同意时默认不采集
expect(prefs.analytics).toBe(false);
expect(prefs.marketing).toBe(false);
expect(prefs.functionality).toBe(true);
});
+3 -1
View File
@@ -17,7 +17,9 @@ export interface CookiePreferences {
const DEFAULT_PREFERENCES: CookiePreferences = {
necessary: true,
analytics: true,
// 分析 Cookie 默认关闭(opt-in)。旧默认 true 会让未同意/已拒绝的用户同样被采集(DEF-09);
// GDPR/PIPL 口径下「默认同意」不成立,必须是用户主动授予。
analytics: false,
marketing: false,
functionality: true,
};