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:
1 parent
b8b7da975d
commit
fb14bf70d3
6 files changed
+72
-6
No files matched your search
@@ -89,6 +89,25 @@ describe('GoogleAnalytics', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('single gtag initialization', () => {
|
describe('single gtag initialization', () => {
|
||||||
|
it('默认 consent 为 denied,且在 config 前用已存偏好回放一次 update(DEF-09)', () => {
|
||||||
|
setupGtag();
|
||||||
|
const { GoogleAnalytics } = require('./GoogleAnalytics');
|
||||||
|
const { container } = render(<GoogleAnalytics />);
|
||||||
|
const inline = container.querySelector('script[data-inline="true"]');
|
||||||
|
const snippet = inline?.textContent ?? '';
|
||||||
|
|
||||||
|
// ① 默认必须是拒绝:旧实现把 analytics_storage 默认成 granted ⇒ 未同意的访客也被采集
|
||||||
|
expect(snippet).toContain("'analytics_storage': 'denied'");
|
||||||
|
// ② 必须从 localStorage 回放已存偏好,否则上一访次同意过的用户拿不到 granted
|
||||||
|
expect(snippet).toContain('novalon-cookie-preferences');
|
||||||
|
// ③ 顺序:update 必须早于 config,否则 config 带着默认拒绝跑掉了
|
||||||
|
const updateAt = snippet.indexOf("gtag('consent', 'update'");
|
||||||
|
const configAt = snippet.indexOf("gtag('config'");
|
||||||
|
expect(updateAt).toBeGreaterThan(-1);
|
||||||
|
expect(configAt).toBeGreaterThan(-1);
|
||||||
|
expect(updateAt).toBeLessThan(configAt);
|
||||||
|
});
|
||||||
|
|
||||||
it('should send exactly one config from the inline snippet, with default pageview enabled', () => {
|
it('should send exactly one config from the inline snippet, with default pageview enabled', () => {
|
||||||
setupGtag();
|
setupGtag();
|
||||||
const { GoogleAnalytics } = require('./GoogleAnalytics');
|
const { GoogleAnalytics } = require('./GoogleAnalytics');
|
||||||
|
|||||||
@@ -90,10 +90,10 @@ function GoogleAnalyticsContent() {
|
|||||||
{`
|
{`
|
||||||
window.dataLayer = window.dataLayer || [];
|
window.dataLayer = window.dataLayer || [];
|
||||||
function gtag(){dataLayer.push(arguments);}
|
function gtag(){dataLayer.push(arguments);}
|
||||||
gtag('js', new Date());
|
|
||||||
|
|
||||||
|
// 默认全拒(opt-in)。用户此前若已明确同意,下面在 config 前回放一次(DEF-09)。
|
||||||
gtag('consent', 'default', {
|
gtag('consent', 'default', {
|
||||||
'analytics_storage': 'granted',
|
'analytics_storage': 'denied',
|
||||||
'ad_storage': 'denied',
|
'ad_storage': 'denied',
|
||||||
'functionality_storage': 'granted',
|
'functionality_storage': 'granted',
|
||||||
'personalization_storage': 'denied',
|
'personalization_storage': 'denied',
|
||||||
@@ -101,6 +101,21 @@ function GoogleAnalyticsContent() {
|
|||||||
'wait_for_update': 500
|
'wait_for_update': 500
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Cookie 偏好可能早于 gtag 定义就已写入 localStorage(用户上一访次同意过)。
|
||||||
|
// 若不在这里回放,updateConsentDetailed 在 window.gtag 尚未存在时会被静默丢弃,
|
||||||
|
// 结果是「同意过的用户反而拿不到 granted」。必须在 config 之前。
|
||||||
|
try {
|
||||||
|
var stored = localStorage.getItem('novalon-cookie-preferences');
|
||||||
|
var prefs = stored ? JSON.parse(stored) : null;
|
||||||
|
gtag('consent', 'update', {
|
||||||
|
'analytics_storage': prefs && prefs.analytics === true ? 'granted' : 'denied',
|
||||||
|
'ad_storage': prefs && prefs.marketing === true ? 'granted' : 'denied',
|
||||||
|
'functionality_storage': prefs && prefs.functionality === true ? 'granted' : 'denied'
|
||||||
|
});
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
gtag('js', new Date());
|
||||||
|
|
||||||
gtag('config', '${GA_MEASUREMENT_ID}', {
|
gtag('config', '${GA_MEASUREMENT_ID}', {
|
||||||
anonymize_ip: true,
|
anonymize_ip: true,
|
||||||
allow_google_signals: true,
|
allow_google_signals: true,
|
||||||
|
|||||||
@@ -1,12 +1,26 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
import { describe, it, expect } from '@jest/globals';
|
import { describe, it, expect, jest } from '@jest/globals';
|
||||||
import { render } from '@testing-library/react';
|
import { render } from '@testing-library/react';
|
||||||
import '@testing-library/jest-dom';
|
import '@testing-library/jest-dom';
|
||||||
|
|
||||||
|
jest.mock('next/navigation', () => ({
|
||||||
|
usePathname: () => '/',
|
||||||
|
}));
|
||||||
|
|
||||||
// The jest.setup.js already mocks next/dynamic to return a component that renders null
|
// The jest.setup.js already mocks next/dynamic to return a component that renders null
|
||||||
// We just need to verify the wrapper renders correctly
|
// We just need to verify the wrapper renders correctly
|
||||||
|
|
||||||
describe('GoogleAnalyticsWrapper', () => {
|
describe('GoogleAnalyticsWrapper', () => {
|
||||||
|
it('/admin 下不加载 GA(后台没有 CookieConsent 入口,谈不了同意)', () => {
|
||||||
|
const { shouldRenderAnalytics } = require('./GoogleAnalyticsWrapper');
|
||||||
|
expect(shouldRenderAnalytics('/admin')).toBe(false);
|
||||||
|
expect(shouldRenderAnalytics('/admin/login')).toBe(false);
|
||||||
|
expect(shouldRenderAnalytics('/admin/content/news')).toBe(false);
|
||||||
|
// 正控制:营销面必须继续加载
|
||||||
|
expect(shouldRenderAnalytics('/')).toBe(true);
|
||||||
|
expect(shouldRenderAnalytics('/products')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it('should render without crashing', () => {
|
it('should render without crashing', () => {
|
||||||
const { GoogleAnalyticsWrapper } = require('./GoogleAnalyticsWrapper');
|
const { GoogleAnalyticsWrapper } = require('./GoogleAnalyticsWrapper');
|
||||||
const { container } = render(<GoogleAnalyticsWrapper />);
|
const { container } = render(<GoogleAnalyticsWrapper />);
|
||||||
|
|||||||
@@ -1,12 +1,27 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
|
import { usePathname } from 'next/navigation';
|
||||||
|
|
||||||
const GoogleAnalytics = dynamic(
|
const GoogleAnalytics = dynamic(
|
||||||
() => import('./GoogleAnalytics').then((mod) => mod.GoogleAnalytics),
|
() => import('./GoogleAnalytics').then((mod) => mod.GoogleAnalytics),
|
||||||
{ ssr: false }
|
{ ssr: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `/admin` 是内部系统,不应加载公开站的 GA。
|
||||||
|
*
|
||||||
|
* 为什么要 gate 而不是只靠 consent(DEF-03 同批,2026-10-24 全站验收 DEF-09 记录):
|
||||||
|
* 后台页面没有 CookieConsent 条,管理员也就没有表达同意的入口;只要 GA 脚本仍在后台
|
||||||
|
* 挂载,后台的每次点击都会进 dataLayer。导成纯函数让单测能直接钉住边界,
|
||||||
|
* 而不是只在浏览器里"看着像没加载"。
|
||||||
|
*/
|
||||||
|
export function shouldRenderAnalytics(pathname: string): boolean {
|
||||||
|
return !pathname.startsWith('/admin');
|
||||||
|
}
|
||||||
|
|
||||||
export function GoogleAnalyticsWrapper() {
|
export function GoogleAnalyticsWrapper() {
|
||||||
|
const pathname = usePathname();
|
||||||
|
if (!shouldRenderAnalytics(pathname)) {return null;}
|
||||||
return <GoogleAnalytics />;
|
return <GoogleAnalytics />;
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,8 @@ describe('Analytics', () => {
|
|||||||
it('should return default preferences', () => {
|
it('should return default preferences', () => {
|
||||||
const prefs = analytics.getDefaultPreferences();
|
const prefs = analytics.getDefaultPreferences();
|
||||||
expect(prefs.necessary).toBe(true);
|
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.marketing).toBe(false);
|
||||||
expect(prefs.functionality).toBe(true);
|
expect(prefs.functionality).toBe(true);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ export interface CookiePreferences {
|
|||||||
|
|
||||||
const DEFAULT_PREFERENCES: CookiePreferences = {
|
const DEFAULT_PREFERENCES: CookiePreferences = {
|
||||||
necessary: true,
|
necessary: true,
|
||||||
analytics: true,
|
// 分析 Cookie 默认关闭(opt-in)。旧默认 true 会让未同意/已拒绝的用户同样被采集(DEF-09);
|
||||||
|
// GDPR/PIPL 口径下「默认同意」不成立,必须是用户主动授予。
|
||||||
|
analytics: false,
|
||||||
marketing: false,
|
marketing: false,
|
||||||
functionality: true,
|
functionality: true,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user