- 点击埋点:当用户点击元素时,收集该点击元素上的信息,并返回给后端
- 曝光埋点:当需要曝光元素在屏幕 🖥 上出现时,收集该元素上的信息,并返回给后端
数据收集
1 2
| <button x-click="message" name="common">common button</button> <div class="export" export-info="{}"></div>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| const pageClickInfo = {};
window.addEventListener("click", (event) => { const element = event.target; const name = element.getAttribute("name"); name && (pageClickInfo[name] = element.getAttribute("x-click")); });
const intersectionObserver = new IntersectionObserver( (entries) => { entries.forEach((item) => { console.log(item.target, item.intersectionRatio); }); }, { threshold: [0.5, 1] } );
Array.from(document.getElementsByClassName("export")).forEach((el) => { intersectionObserver.observe(el); });
|
数据发送
通过 Beacon API
需要用到 Beacon API,用于发送不需要响应的 异步,非阻塞请求到服务器。
1 2 3 4 5
| window.addEventListener("unload", function (event) { navigator.sendBeacon("/log", "foo=bar"); });
|
通过 a 标签的 ping 属性
虽然发送的是 POST 请求,但只能发送 query 类型数据,不可指定 body
1 2 3
| <a href="https://baidu.com" ping="/log?foo=bar"> click </a>
|
反弹追踪
就是网页跳转时,先跳到一个或多个中间网址,以便收集信息,然后再跳转到原来的目标网址。
防止截取 UA 信息 以确保用户隐私信息,经常会在 【百度搜索】和【洋葱浏览器】上见到
1 2 3 4 5 6 7 8
| const link = document.getElementById("target");
link.addEventListener("click", function (event) { event.preventDefault(); const href = link.getAttribute("href"); window.location.href = `/go?raw_href=${encodeURIComponent(href)}`; });
|