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:
@@ -0,0 +1,42 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useRef, useCallback } from 'react';
|
||||
import { usePathname } from 'next/navigation';
|
||||
import { trackScrollDepth } from '@/lib/analytics';
|
||||
|
||||
const MILESTONES = [25, 50, 75, 100] as const;
|
||||
|
||||
export function ScrollDepthTracker() {
|
||||
const trackedRef = useRef<Set<number>>(new Set());
|
||||
const pathname = usePathname();
|
||||
|
||||
const handleScroll = useCallback(() => {
|
||||
const scrollTop = window.scrollY;
|
||||
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
|
||||
|
||||
if (docHeight <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const scrollPercent = Math.round((scrollTop / docHeight) * 100);
|
||||
|
||||
MILESTONES.forEach((milestone) => {
|
||||
if (scrollPercent >= milestone && !trackedRef.current.has(milestone)) {
|
||||
trackedRef.current.add(milestone);
|
||||
trackScrollDepth(milestone);
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
trackedRef.current = new Set();
|
||||
|
||||
window.addEventListener('scroll', handleScroll, { passive: true });
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('scroll', handleScroll);
|
||||
};
|
||||
}, [pathname, handleScroll]);
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user