buriedPiont.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. class Tracker {
  2. constructor(options = {}) {
  3. this.baseUrl = options.baseUrl || '';
  4. this.timer = null;
  5. this.startTime = 0;
  6. this.heartbeatInterval = options.heartbeatInterval || 30000; // 默认30秒发送一次心跳
  7. this.visitCookieTimeout = options.visitCookieTimeout || 1; // 默认访问次数cookie有效期1分钟
  8. this.maxVisitCount = options.maxVisitCount || 5; // 默认最大访问次数5次
  9. this.blockCookieTimeout = options.blockCookieTimeout || 180; // 默认封锁cookie有效期180分钟
  10. this.myEventSend = options.myEventSend || (() => {}); // 是否开启自定义事件上报
  11. this.beforeDestroy = options.beforeDestroy || (() => {}); // 销毁前的回调函数
  12. }
  13. init() {
  14. const visitCookieName = 'userVisitCount';
  15. const blockCookieName = 'userBlocked';
  16. const isBlocked = getCookie(blockCookieName);
  17. const doneFN = () => {
  18. this.startTime = Date.now();
  19. this.bindEvents();
  20. this.startHeartbeat();
  21. }
  22. if (isBlocked) {
  23. // restoreBlock();
  24. return;
  25. }
  26. let visitCount = getCookie(visitCookieName);
  27. if (visitCount) {
  28. visitCount = parseInt(visitCount, 10);
  29. if (visitCount >= this.maxVisitCount) {
  30. setCookie(blockCookieName, 'true', this.blockCookieTimeout); // 封锁用户 180 分钟
  31. deleteCookie(visitCookieName);
  32. // restoreBlock();
  33. } else {
  34. visitCount += 1;
  35. setCookie(visitCookieName, visitCount, this.visitCookieTimeout); // 每次访问 +1,有效期 1 分钟
  36. doneFN();
  37. }
  38. } else {
  39. setCookie(visitCookieName, '1', this.visitCookieTimeout); // 首次访问,设置访问次数为 1
  40. doneFN();
  41. }
  42. }
  43. bindEvents() {
  44. window.addEventListener('beforeunload', this.handleBeforeUnload);
  45. window.addEventListener('click', this.handleClickEvent);
  46. this.myEventSend(this.sendData.bind(this)); // 绑定自定义事件上报
  47. }
  48. startHeartbeat() {
  49. this.timer = setInterval(() => {
  50. this.sendData({
  51. eventType: 'heartbeat',
  52. duration: Math.round((Date.now() - this.startTime) / 1000),
  53. }, {heartbeatSeconds: this.heartbeatInterval});
  54. }, this.heartbeatInterval);
  55. }
  56. handleBeforeUnload = (e) => {
  57. const data = {
  58. eventType: 'page_close',
  59. duration: Math.round((Date.now() - this.startTime) / 1000),
  60. };
  61. const payload = JSON.stringify(data);
  62. if (navigator.sendBeacon) {
  63. navigator.sendBeacon(this.baseUrl, payload);
  64. } else {
  65. this.sendData(payload, {heartbeatSeconds: this.heartbeatInterval});
  66. }
  67. };
  68. handleClickEvent = (event) => {
  69. const target = event.target;
  70. if (target.matches('[data-track]')) {
  71. const trackInfo = {
  72. eventType: 'button_click',
  73. elementId: target.id || '无ID',
  74. elementClass: target.className || '无class',
  75. text: target.innerText || target.textContent || '',
  76. timestamp: new Date().toISOString(),
  77. };
  78. this.sendData(trackInfo, {heartbeatSeconds: this.heartbeatInterval});
  79. }
  80. };
  81. sendData(data, headers = {}) {
  82. console.log('上报埋点数据:', data);
  83. fetch(`${this.baseUrl}`, { method: 'POST', headers: { 'Content-Type': 'application/json' ,...headers}, body: JSON.stringify(data) });
  84. }
  85. destroy() {
  86. window.removeEventListener('beforeunload', this.handleBeforeUnload);
  87. window.removeEventListener('click', this.handleClickEvent);
  88. this.beforeDestroy();
  89. if (this.timer) {
  90. clearInterval(this.timer);
  91. }
  92. this.sendData({
  93. eventType: 'tracker_destroyed',
  94. duration: Math.round((Date.now() - this.startTime) / 1000),
  95. }, {heartbeatSeconds: this.heartbeatInterval});
  96. }
  97. }
  98. // 使用示例:
  99. // const tracker = new Tracker({ baseUrl: 'https://your-tracking-api.com' });
  100. // tracker.init();
  101. // 暴露给全局或者作为模块导出
  102. window.Tracker = Tracker;