浏览代码

fix: ----

jcq 4 周之前
父节点
当前提交
814df73cfb
共有 3 个文件被更改,包括 62 次插入6 次删除
  1. 47 5
      buriedPiont.js
  2. 11 0
      main.html
  3. 4 1
      vue-test/src/components/HelloWorld.vue

+ 47 - 5
buriedPiont.js

@@ -1,4 +1,44 @@
+
+// 通用 JSBridge
+window.JSBridge = {
+  getOS: function () {
+    const ua = navigator.userAgent || navigator.vendor || window.opera;
+    if (/android/i.test(ua)) return 'Android';
+    if (/iPad|iPhone|iPod/.test(ua) && !window.MSStream) return 'iOS';
+    return 'Web';
+  },
+  getUserId: function (callback) {
+    // Android WebView
+    if (window.Android && typeof window.Android.getUserId === 'function') {
+      callback(window.Android.getUserId());
+      return;
+    }
+    // iOS WebView
+    if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.getUserId) {
+      window.webkit.messageHandlers.getUserId.postMessage(null);
+      window.onUserIdReceived = callback; // iOS原生需回调window.onUserIdReceived(userId)
+      return;
+    }
+    // Web环境(用 FingerprintJS 生成指纹)
+    if (window.FingerprintJS) {
+      FingerprintJS.load().then(fp => {
+        fp.get().then(result => {
+          callback(result.visitorId);
+        });
+      });
+    } else {
+      callback('unsupported');
+    }
+  }
+};
+
+
+let userId = '';
+JSBridge.getUserId(function (id) {
+  userId = id;
+});
 class Tracker {
+
   constructor(options = {}) {
     this.baseUrl = options.baseUrl || '';
     this.timer = null;
@@ -19,10 +59,12 @@ class Tracker {
 
   startHeartbeat() {
     this.timer = setInterval(() => {
+      console.log(userId);
+
       this.sendData({
         eventType: 'heartbeat',
         duration: Math.round((Date.now() - this.startTime) / 1000),
-      }, {heartbeatSeconds: this.heartbeatInterval});
+      }, { heartbeatSeconds: this.heartbeatInterval, userId: userId });
     }, this.heartbeatInterval);
   }
 
@@ -35,7 +77,7 @@ class Tracker {
     if (navigator.sendBeacon) {
       navigator.sendBeacon(this.baseUrl, payload);
     } else {
-      this.sendData(payload, {heartbeatSeconds: this.heartbeatInterval});
+      this.sendData(payload, { heartbeatSeconds: this.heartbeatInterval, userId: userId });
     }
   };
 
@@ -49,7 +91,7 @@ class Tracker {
         text: target.innerText || target.textContent || '',
         timestamp: new Date().toISOString(),
       };
-      this.sendData(trackInfo, {heartbeatSeconds: this.heartbeatInterval});
+      this.sendData(trackInfo, { heartbeatSeconds: this.heartbeatInterval, userId: userId });
     }
   };
 
@@ -67,7 +109,7 @@ class Tracker {
     this.sendData({
       eventType: 'tracker_destroyed',
       duration: Math.round((Date.now() - this.startTime) / 1000),
-    }, {heartbeatSeconds: this.heartbeatInterval});
+    }, { heartbeatSeconds: this.heartbeatInterval, userId: userId });
   }
 }
 
@@ -76,4 +118,4 @@ class Tracker {
 // tracker.init();
 
 // 暴露给全局或者作为模块导出
-window.Tracker = Tracker;
+window.Tracker = Tracker;

+ 11 - 0
main.html

@@ -8,9 +8,20 @@
 <body>
   这是一个空页面
   <button id="btn" data-track>点击我</button>
+  <div>操作系统: <span id="os"></span></div>
+  <div>用户唯一标识: <span id="userId"></span></div>
 </body>
 <script src="buriedPiont.js"></script>
 <script>
+
+
+  // 展示操作系统和用户唯一标识
+  document.addEventListener('DOMContentLoaded', function () {
+    document.getElementById('os').innerText = JSBridge.getOS();
+    JSBridge.getUserId(function (userId) {
+      document.getElementById('userId').innerText = userId;
+    });
+  });
   const tracker = new Tracker({ 
     baseUrl: 'http://192.168.3.9:3000/api/log',
     heartbeatInterval: 5000,

+ 4 - 1
vue-test/src/components/HelloWorld.vue

@@ -8,6 +8,9 @@
 </template>
 <script setup>
 import UtmTracker from "utm-params-extractor-test";
+import {ref} from 'vue';
+
+const userId = ref('');
 
 function generateAndAppendUtmParams() {
   // 获取随机合法值的函数
@@ -70,7 +73,7 @@ const fetchData = () => {
   console.log(requestData);
   fetch('http://192.168.3.9:3000/api/insert', {
     method: 'POST',
-    headers: { 'Content-Type': 'application/json' },
+    headers: { 'Content-Type': 'application/json',userid: userId },
     body: JSON.stringify(requestData)
   })
    .then(response => response.json())