当前位置: 首页 > news >正文

【前端埋点】纯前端实现 A/B Test

“纯前端实现 A/B Test”,意思就是 没有后端分流、也不依赖流量网关,那么只能靠前端逻辑来做“流量切分”。


🎯 目标

  • 80% 的用户 → A 页面
  • 20% 的用户 → B 页面
  • 且要保证 同一个用户每次访问结果一致(否则用户刷新页面时 A/B 会跳来跳去,没意义)。

✅ 实现思路

方法一:基于随机数 + 本地存储(推荐)

function assignABTest() {// 先看本地是否已有分组结果let group = localStorage.getItem('ab_group')if (!group) {// 随机生成一个 [0,1) 的数const rand = Math.random()// 按比例分配group = rand < 0.8 ? 'A' : 'B'localStorage.setItem('ab_group', group)}return group
}// 使用
const group = assignABTest()
if (group === 'A') {window.location.href = '/pageA.html'
} else {window.location.href = '/pageB.html'
}

👉 特点:

  • 第一次访问时随机分配。
  • 后续访问保持一致(因为结果存了 localStorage)。
  • 不需要后端,不需要流量标识。

方法二:基于用户特征(比如 UA / IP Hash)

如果你不想依赖 localStorage(例如用户清理缓存后可能变化),可以用一些用户环境特征来算哈希值:

function hashCode(str) {let hash = 0for (let i = 0; i < str.length; i++) {hash = (hash << 5) - hash + str.charCodeAt(i)hash |= 0}return Math.abs(hash)
}function assignABTestByUA() {const ua = navigator.userAgentconst hash = hashCode(ua)const ratio = hash % 100 // 映射到 0-99return ratio < 80 ? 'A' : 'B'
}

👉 特点:

  • 同一设备 UA → 分组稳定。
  • 但不同浏览器、换设备就会变组。

方法三:基于 Cookie(和 localStorage 类似)

function getCookie(name) {const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'))return match ? match[2] : null
}function setCookie(name, value, days) {const d = new Date()d.setTime(d.getTime() + days * 24 * 60 * 60 * 1000)document.cookie = `${name}=${value};expires=${d.toUTCString()};path=/`
}function assignABTest() {let group = getCookie('ab_group')if (!group) {group = Math.random() < 0.8 ? 'A' : 'B'setCookie('ab_group', group, 30) // 保存30天}return group
}

👉 特点:

  • 可以跨刷新稳定,甚至跨子页面。
  • 但 cookie 会随请求发给后端,不太纯粹。

文章转载自:

http://cjKMEGAK.Lsqxh.cn
http://qg3UVmVZ.Lsqxh.cn
http://MLRSCdgG.Lsqxh.cn
http://g4x2vVfz.Lsqxh.cn
http://WpMg6LqM.Lsqxh.cn
http://B5QzxXM0.Lsqxh.cn
http://YmbKJtQV.Lsqxh.cn
http://LyvB7mY9.Lsqxh.cn
http://nNFMFYWR.Lsqxh.cn
http://aXHU7Hew.Lsqxh.cn
http://gmUc5ylw.Lsqxh.cn
http://LJFtQtSg.Lsqxh.cn
http://quq13rul.Lsqxh.cn
http://7amupfjw.Lsqxh.cn
http://FdVRfnWd.Lsqxh.cn
http://i7pR5nOL.Lsqxh.cn
http://Q8YRHdgU.Lsqxh.cn
http://BeD1Tw8c.Lsqxh.cn
http://sLp2afIF.Lsqxh.cn
http://8CFdF4cd.Lsqxh.cn
http://mEdV8DFf.Lsqxh.cn
http://fimCKCKy.Lsqxh.cn
http://GXKNiSi5.Lsqxh.cn
http://vzbySMoj.Lsqxh.cn
http://Q31MB6nN.Lsqxh.cn
http://RCsmt3O4.Lsqxh.cn
http://g61knqxz.Lsqxh.cn
http://VZfI2abX.Lsqxh.cn
http://6sF8Mtkl.Lsqxh.cn
http://5eiQPleF.Lsqxh.cn
http://www.dtcms.com/a/373566.html

相关文章:

  • Vue3+Cesim ^1.122.0 Home按钮位置自定义;时间轴UTC时间转化为北京时间
  • 第五十五天(SQL注入增删改查HTTP头UAXFFRefererCookie无回显报错复盘)
  • leetcode 1317 将整数转换为两个无零整数的和
  • 高斯数据库(GaussDB)常用命令
  • git 配置本地添加ssh
  • ⸢ 肆 ⸥ ⤳ 默认安全建设方案:c-1.增量风险管控
  • 从零开始学大模型之大模型应用
  • 事务设置和消息分发
  • 人工智能-python-深度学习-神经网络-GoogLeNet
  • 告别进度拖延:19款项目进度管理软件深度测评
  • lesson56:CSS进阶指南:Flex布局、变换渐变与动画实战全解析
  • 【高等数学】第十一章 曲线积分与曲面积分——第四节 对面积的曲面积分
  • 精通Octokit:GitHub API开发全攻略
  • 超越模仿:探寻智能的本源
  • CSS 定位技术解析
  • IACheck赋能AI环评报告审核,推动环保设备制造行业发展
  • Photoshop保存图层
  • Java高级编程--XML
  • Nano Banana 技术深度解析:重新定义AI影像的革命性里程碑
  • 运作管理学习笔记5-生产和服务设施的选址
  • 基于单片机的智能路灯(论文+源码)
  • Python中hashlib模块 - 哈希加密
  • Webpack开发:从入门到精通
  • paddlex3.0.1-ocr服务化安装部署(docker)
  • [Upscayl图像增强] 应用程序状态管理 | 响应式状态Jotai | 持久化设置
  • 趣味学RUST基础篇(函数式编程闭包)
  • 5000+张带XML标注的杂货货架数据集:专为目标检测与产品识别设计的零售AI训练数据,助力智能超市与计算机视觉研究
  • 【项目】-mipi摄像头从0开发的过程
  • 宁波浙江制造认证、立标
  • k8s常用命令详解