前端数据埋点上报方式

1、发送普通的get请求,缺点是请求频繁可能会阻塞正常接口

const get = request({method:'get',eventId:'report-sdk-eventId'});

2、通过new Image(),通过给src赋值,缺点是实时上报,不能延迟上报

const image = new Image();
image.src = 'https://report';

3、通过css定义content,按钮点击就会上报,但是不能动态传入一些变量
.report:active::after {
content: var(--report);
display: inline-block;
position: absolute;
}

<button class="report" style="--report:url(./pixel.gif?action=click&id=button1)">按钮1</button>

4、indexDB 离线上报,是在用户点击完之后
...
初始化好indexDB后,发送原生的XMLHttpRequest

const xhr = new XMLHttpRequest();
xhr.send({
eventId:'report-sdk-eventId'
})
posted @ 2022-03-19 12:57  程序員劝退师  阅读(422)  评论(0编辑  收藏  举报