使用 IntersectionObserver 实现曝光埋点

在 Web 开发中,我们经常需要埋点统计用户的行为,比如 元素曝光 统计,即某个元素在视口中可见并达到一定时间后触发上报。为了解决这一需求,我们可以使用 IntersectionObserver 监听元素的可见性,并结合 setTimeout 计算停留时间,确保符合条件后才触发上报。

需求分析

我们的曝光埋点 SDK 需要满足以下需求:

  • 监听多个元素的可见性
  • 设定曝光阈值(元素可见面积的比例,比如 50% 才算曝光)
  • 设定曝光时间(元素需要在视口内持续一段时间才触发上报)
  • 动态解绑监听(防止内存泄漏)

代码实现

javascript 复制代码
class ExposureTracker {
  constructor({ threshold = 0.5, duration = 1000, callback } = {}) {
    this.threshold = threshold; // 触发曝光的可见比例
    this.duration = duration;   // 触发曝光的时间(ms)
    this.callback = callback;   // 曝光回调
    this.observedElements = new Map(); // 记录元素的曝光状态
    
    this.observer = new IntersectionObserver(this.handleIntersect.bind(this), {
      threshold: Array.from({ length: 10 }, (_, i) => (i + 1) / 10), // 0.1 ~ 1
    });
  }

  observe(element, data = {}) {
    if (!element) return;
    this.observedElements.set(element, { isVisible: false, timer: null, data });
    this.observer.observe(element);
  }

  unobserve(element) {
    if (this.observedElements.has(element)) {
      clearTimeout(this.observedElements.get(element).timer);
      this.observer.unobserve(element);
      this.observedElements.delete(element);
    }
  }

  handleIntersect(entries) {
    entries.forEach((entry) => {
      const { target, intersectionRatio } = entry;
      const record = this.observedElements.get(target);
      if (!record) return;
      
      const isVisible = intersectionRatio >= this.threshold;
      if (isVisible && !record.isVisible) {
        // 开始计时
        record.timer = setTimeout(() => {
          this.callback && this.callback(record.data);
          this.unobserve(target);
        }, this.duration);
      } else if (!isVisible && record.isVisible) {
        // 取消计时
        clearTimeout(record.timer);
      }
      
      record.isVisible = isVisible;
    });
  }
}

// 使用示例
const tracker = new ExposureTracker({
  threshold: 0.5,
  duration: 2000,
  callback: (data) => console.log('曝光上报:', data),
});

document.querySelectorAll('.track').forEach((el, index) => {
  tracker.observe(el, { id: index, message: `元素${index}曝光` });
});

代码解析

  1. 使用 IntersectionObserver 监听元素的可见性

    • threshold 设定多个阈值,确保可以检测到不同的可见比例。
    • 当 intersectionRatio 大于等于设定的 threshold 时,认为元素曝光。
  2. 使用 setTimeout 处理曝光时间

    • 只有当元素的可见比例满足 threshold,且持续 超过 duration 毫秒 后,才会触发回调。
    • 如果元素在 duration 时间内消失,则取消计时,避免误报。
  3. 动态解绑,防止内存泄漏

    • 在元素曝光上报后,使用 unobserve 解除监听,避免对已曝光元素重复监听。

适用场景

  • 统计广告、Banner 是否被用户看到
  • 统计文章或图片是否真正出现在用户视野中
  • 结合 A/B 测试分析不同 UI 组件的可见度和转化率

总结

通过 IntersectionObserver,我们可以高效地监听元素的可见性变化,并结合 setTimeout 控制曝光时间,构建一个轻量级的 曝光埋点 SDK 。这样,我们既能避免传统 scroll 监听带来的性能问题,又能精确统计元素的曝光情况。

希望这篇文章能帮助你更好地理解 IntersectionObserver 并应用到实际项目中!

相关推荐
小天源14 分钟前
智叟云集系统 · 架构与职责分层
服务器·前端·数据库
Highcharts.js2 小时前
React 中原生支持使用 JSX 组件配置自定义表格 Highcharts Grid
前端·javascript·react.js·前端框架·jsx·highcharts·自定义表格
薛定谔的猫-菜鸟程序员2 小时前
Electron开发超详细教程(从入门到放弃)
前端·javascript·electron
最强小杰3 小时前
DeepSeek V4 Flash + Codex CLI 接入教程:原生 Responses API 适配,从配置到跑通收藏这篇就够了(2026)
java·前端·python·ai
静水leo3 小时前
使用 Whistle 拦截接口并模拟后端响应
前端
掘金者阿豪3 小时前
SQL Server数据迁移完成后,凌晨的数据库告警该怎么查?
前端·后端
JudithHuang3 小时前
React 嵌套路由与布局
前端·react.js·arcgis·react·react router
htzyl2063 小时前
前端阶梯——第四十三章、实战二:待办清单To-Do
前端·javascript·html
摹客4 小时前
【3D设计】界面设计中的3D元素:应用场景+5款工具横评(含Three.js)
前端