运营需要分析用户浏览品类、停留时长、点击行为,优化首页推荐和分类布局。但日淘代拍平台用户分布海内外,网络波动大,完整埋点请求易阻塞页面加载。
重型埋点的负面影响
埋点同步上报数据会阻塞页面渲染,海外弱网环境页面卡顿、白屏。全量记录每一次点击、滚动产生海量日志,存储成本高。未区分核心行为与无效滑动,大量冗余数据增加数据分析工作量。
轻量化埋点方案
采用异步图片像素上报方案,用户浏览、点击行为触发后生成1*1透明图片请求上报参数,不占用JS异步请求连接,完全不阻塞页面交互。
// 轻量化埋点SDK
class LiteTracker {
constructor(config) {
this.endpoint = config.endpoint || '/api/track.gif';
this.site = config.site || 'default';
this.buffer = [];
this.flushInterval = config.flushInterval || 5000; // 5秒批量上报
this.cache = new Set(); // 去重缓存
// 启动定时批量上报
setInterval(() => this.flush(), this.flushInterval);
// 页面关闭前上报剩余数据
window.addEventListener('beforeunload', () => this.flush(true));
}
// 记录核心行为
track(eventType, params = {}) {
// 去重:5秒内同一商品重复浏览仅上报一次
const dedupKey = `${eventType}_${params.goods_id || ''}`;
const now = Date.now();
if (this.cache.has(dedupKey) && (now - this.lastTrackTime) < 5000) {
return;
}
this.cache.add(dedupKey);
this.lastTrackTime = now;
const data = {
event: eventType, // 商品详情、分类点击、收藏、加入代购、筛选切换
site: this.site,
goods_id: params.goods_id || '',
category: params.category || '',
device: this.getDeviceType(),
region: this.getRegion(),
timestamp: now,
user_id: this.getUserId()
};
this.buffer.push(data);
// 缓冲区超过10条立即上报
if (this.buffer.length >= 10) {
this.flush();
}
}
// 异步图片上报
flush(immediate = false) {
if (this.buffer.length === 0) return;
const data = this.buffer.splice(0, this.buffer.length);
const params = new URLSearchParams();
params.append('data', JSON.stringify(data));
// 使用Image对象上报,不阻塞页面
const img = new Image(1, 1);
img.src = `${this.endpoint}?${params.toString()}`;
if (immediate) {
// 页面关闭前使用sendBeacon确保数据送达
navigator.sendBeacon && navigator.sendBeacon(
this.endpoint,
JSON.stringify(data)
);
}
}
getDeviceType() {
// 检测设备类型:PC / Mobile / Tablet
const ua = navigator.userAgent;
if (/mobile/i.test(ua)) return 'Mobile';
if (/tablet/i.test(ua)) return 'Tablet';
return 'PC';
}
getRegion() {
// 通过IP或浏览器语言判断地区
// 实际项目中通过后端接口获取
return navigator.language.includes('zh') ? 'CN' : 'Other';
}
}数据存储与统计分析
埋点日志异步写入时序数据表,按天自动分区存储,保留90天明细
-- 埋点数据表结构
CREATE TABLE user_tracking (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(32),
event_type VARCHAR(32),
goods_id VARCHAR(64),
category VARCHAR(64),
device VARCHAR(16),
region VARCHAR(8),
site VARCHAR(16), -- 区分雅虎/煤炉用户行为
event_time DATETIME,
INDEX idx_user_time (user_id, event_time),
INDEX idx_category_time (category, event_time),
INDEX idx_site_time (site, event_time)
) PARTITION BY RANGE (TO_DAYS(event_time)) (
-- 按天自动分区
);带宽优化
合并短时间内连续多条行为埋点,5秒内重复浏览同一商品仅上报一次。海外用户埋点请求走境外CDN节点,降低跨境网络延迟。
Bidfans平台因网络波动重埋点易阻塞页面,这套轻量化无侵入埋点方案解决了这个问题。在不影响代拍前台页面加载的前提下采集用户行为数据,支撑运营品类和首页推荐优化。对于做日本代购、雅虎代拍、煤炉代购的平台来说,这套方案在数据有效性与页面访问性能之间找到了平衡。
思考:你们的前端埋点有没有影响过页面加载速度?是怎么优化的?
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。