diff --git a/index.html b/index.html index 48b43c8..ec398ea 100644 --- a/index.html +++ b/index.html @@ -254,6 +254,17 @@ abbr.term:hover{border-bottom-color:var(--brand-1);color:var(--brand-1)} } @media (prefers-reduced-motion:reduce){*{transition:none!important}} + + + + + +
@@ -979,6 +990,16 @@ function apply(){ } /* ---------- 事件 ---------- */ +// 离线单文件会把 GA 片段整段剥掉,这里没有 gtag 也要能跑 +const track = (name, params) => { if (typeof gtag === 'function') gtag('event', name, params); }; +// 搜索词上报:搜了没命中的词就是该补的条目。results 要在 GA 后台注册成自定义指标才看得到。 +let SENT_Q = ''; +function reportSearch(){ + const q = state.q; + if (q.length < 2 || q === SENT_Q) return; + SENT_Q = q; + track('search', { search_term: q, results: Number(document.getElementById('cnt').textContent) || 0 }); +} function wire(){ const sb = document.getElementById('sidebar'), bd = document.getElementById('backdrop'), mb = document.getElementById('menu'); const setMenu = (on) => { sb.classList.toggle('open', on); bd.classList.toggle('on', on); mb.setAttribute('aria-expanded', String(on)); }; @@ -999,7 +1020,11 @@ function wire(){ })); document.getElementById('f-dispute').addEventListener('change', e => { state.dispute = e.target.checked; apply(); toTop(); }); document.getElementById('f-todo').addEventListener('change', e => { state.todo = e.target.checked; apply(); toTop(); }); - let tm; document.getElementById('q').addEventListener('input', e => { clearTimeout(tm); tm = setTimeout(() => { state.q = e.target.value.trim(); apply(); }, 120); }); + // 上报比筛选晚得多:按 120ms 那一拍发,「高血压」会被当成「高」「高血」「高血压」三次搜索 + let tm, qtm; document.getElementById('q').addEventListener('input', e => { + clearTimeout(tm); tm = setTimeout(() => { state.q = e.target.value.trim(); apply(); }, 120); + clearTimeout(qtm); qtm = setTimeout(reportSearch, 2000); + }); const reset = () => { state.q=''; for (const d of DIMS) state[d].clear(); state.dispute=false; state.todo=false; apply(); toTop(); }; document.getElementById('reset').addEventListener('click', reset); document.getElementById('reset2').addEventListener('click', reset); diff --git a/tools/offline/build.mjs b/tools/offline/build.mjs index 11a1432..56a7eba 100644 --- a/tools/offline/build.mjs +++ b/tools/offline/build.mjs @@ -24,6 +24,14 @@ const must = (needle, label) => { if (!html.includes(needle)) throw new Error(`index.html 里找不到${label},离线版脚本要跟着改:${needle}`); }; +// 统计脚本不能跟着离线版走:别人双击打开的副本不该往外发请求,断网时还要等超时 +const GA_START = ''; +must(GA_START, ' GA 片段的起始标记'); +must(GA_END, ' GA 片段的结束标记'); +html = html.slice(0, html.indexOf(GA_START)) + html.slice(html.indexOf(GA_END) + GA_END.length); +// 只查外连域名:主脚本里的 track() 带 typeof 守卫,没有 gtag 也能跑,不算残留 +if (/googletagmanager|google-analytics/.test(html)) throw new Error('剥掉标记之间的内容后仍有统计域名残留,离线版会往外发请求'); + // 相对链接在本地打开时是死的,改成线上地址 must('href="README.md"', ' README.md 链接'); must('href="book/"', ' book/ 链接');