feat(ui): 保时捷语法复刻收尾(ADR-0008/0009/0011/0012/0013) #65
No files matched your search
@@ -89,6 +89,25 @@ describe('GoogleAnalytics', () => {
|
||||
});
|
||||
|
||||
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', () => {
|
||||
setupGtag();
|
||||
const { GoogleAnalytics } = require('./GoogleAnalytics');
|
||||
|
||||
@@ -90,10 +90,10 @@ function GoogleAnalyticsContent() {
|
||||
{`
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
|
||||
// 默认全拒(opt-in)。用户此前若已明确同意,下面在 config 前回放一次(DEF-09)。
|
||||
gtag('consent', 'default', {
|
||||
'analytics_storage': 'granted',
|
||||
'analytics_storage': 'denied',
|
||||
'ad_storage': 'denied',
|
||||
'functionality_storage': 'granted',
|
||||
'personalization_storage': 'denied',
|
||||
@@ -101,6 +101,21 @@ function GoogleAnalyticsContent() {
|
||||
'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}', {
|
||||
anonymize_ip: true,
|
||||
allow_google_signals: true,
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
// @ts-nocheck
|
||||
import { describe, it, expect } from '@jest/globals';
|
||||
import { describe, it, expect, jest } from '@jest/globals';
|
||||
import { render } from '@testing-library/react';
|
||||
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
|
||||
// We just need to verify the wrapper renders correctly
|
||||
|
||||
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', () => {
|
||||
const { GoogleAnalyticsWrapper } = require('./GoogleAnalyticsWrapper');
|
||||
const { container } = render(<GoogleAnalyticsWrapper />);
|
||||
|
||||
@@ -1,12 +1,27 @@
|
||||
'use client';
|
||||
|
||||
import dynamic from 'next/dynamic';
|
||||
import { usePathname } from 'next/navigation';
|
||||
|
||||
const GoogleAnalytics = dynamic(
|
||||
() => import('./GoogleAnalytics').then((mod) => mod.GoogleAnalytics),
|
||||
{ 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() {
|
||||
const pathname = usePathname();
|
||||
if (!shouldRenderAnalytics(pathname)) {return null;}
|
||||
return <GoogleAnalytics />;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user