xbx 3 mesi fa
commit
c36e1d56e6
7 ha cambiato i file con 750 aggiunte e 0 eliminazioni
  1. 32 0
      .eslintrc.json
  2. 68 0
      .gitignore
  3. 33 0
      package.json
  4. 250 0
      pnpm-lock.yaml
  5. 47 0
      rollup.config.js
  6. 286 0
      src/main.js
  7. 34 0
      tsconfig.json

+ 32 - 0
.eslintrc.json

@@ -0,0 +1,32 @@
+{
+  "env": {
+    "browser": true,
+    "es2021": true,
+    "node": true
+  },
+  "extends": "eslint:recommended",
+  "parserOptions": {
+    "ecmaVersion": 12,
+    "sourceType": "module"
+  },
+  "rules": {
+    "indent": [
+      "error",
+      2
+    ],
+    "linebreak-style": [
+      "error",
+      "unix"
+    ],
+    "quotes": [
+      "error",
+      "single"
+    ],
+    "semi": [
+      "error",
+      "always"
+    ],
+    "no-console": "warn",
+    "no-unused-vars": "warn"
+  }
+}

+ 68 - 0
.gitignore

@@ -0,0 +1,68 @@
+# Dependencies
+node_modules/
+jspm_packages/
+
+# Production builds
+/dist/
+/build/
+/out/
+
+# Environment
+.env
+.env.local
+.env.development.local
+.env.test.local
+.env.production.local
+
+# Logs
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+.lerna/
+.pnpm-debug.log*
+
+# Optional npm cache directory
+.npm
+
+# pnpm
+node_modules/.pnpm/
+
+# Editor directories and files
+.vscode/
+.idea/
+*.swp
+*.swo
+*~
+.DS_Store
+Thumbs.db
+
+# Rollup artifacts
+.rollup.cache/
+
+# Coverage directory used by tools like istanbul
+coverage/
+.nyc_output/
+
+# Temporary files
+tmp/
+temp/
+
+# OS generated files
+ehthumbs.db
+Icon?
+._*
+
+# IDE
+.vs/
+*.log
+
+# Runtime data
+pids
+*.pid
+*.seed
+*.pid.lock
+
+# Coverage directory used by tools like istanbul
+/lib-cov
+/cov/coverage
+/cov/lcov.info

+ 33 - 0
package.json

@@ -0,0 +1,33 @@
+{
+	"name": "internal-monitoring-platform",
+	"version": "1.0.0",
+	"description": "内部监控平台,收集并上报代码报错、性能数据、用户行为、加载资源、个性化指标等数据",
+	"main": "dist/bundle-iife.js",
+	"scripts": {
+		"build": "rollup -c",
+		"dev": "rollup -c -w",
+		"build:prod": "rollup -c --environment NODE_ENV:production",
+		"test": "echo \"Error: no test specified\" && exit 1",
+		"lint": "eslint src/",
+		"prebuild": "if exist dist (rmdir /s /q dist) || echo dist does not exist"
+	},
+	"dependencies": {
+		"rollup": "^4.54.0"
+	},
+	"devDependencies": {
+		"@rollup/plugin-node-resolve": "^15.2.3",
+		"@rollup/plugin-commonjs": "^25.0.7",
+		"@rollup/plugin-terser": "^0.4.4",
+		"eslint": "^8.56.0"
+	},
+	"keywords": [
+		"monitoring",
+		"error-tracking",
+		"performance",
+		"analytics",
+		"frontend",
+		"sdk"
+	],
+	"author": "Internal Team",
+	"license": "MIT"
+}

+ 250 - 0
pnpm-lock.yaml

@@ -0,0 +1,250 @@
+lockfileVersion: '9.0'
+
+settings:
+  autoInstallPeers: true
+  excludeLinksFromLockfile: false
+
+importers:
+
+  .:
+    dependencies:
+      rollup:
+        specifier: ^4.54.0
+        version: 4.54.0
+
+packages:
+
+  '@rollup/rollup-android-arm-eabi@4.54.0':
+    resolution: {integrity: sha512-OywsdRHrFvCdvsewAInDKCNyR3laPA2mc9bRYJ6LBp5IyvF3fvXbbNR0bSzHlZVFtn6E0xw2oZlyjg4rKCVcng==}
+    cpu: [arm]
+    os: [android]
+
+  '@rollup/rollup-android-arm64@4.54.0':
+    resolution: {integrity: sha512-Skx39Uv+u7H224Af+bDgNinitlmHyQX1K/atIA32JP3JQw6hVODX5tkbi2zof/E69M1qH2UoN3Xdxgs90mmNYw==}
+    cpu: [arm64]
+    os: [android]
+
+  '@rollup/rollup-darwin-arm64@4.54.0':
+    resolution: {integrity: sha512-k43D4qta/+6Fq+nCDhhv9yP2HdeKeP56QrUUTW7E6PhZP1US6NDqpJj4MY0jBHlJivVJD5P8NxrjuobZBJTCRw==}
+    cpu: [arm64]
+    os: [darwin]
+
+  '@rollup/rollup-darwin-x64@4.54.0':
+    resolution: {integrity: sha512-cOo7biqwkpawslEfox5Vs8/qj83M/aZCSSNIWpVzfU2CYHa2G3P1UN5WF01RdTHSgCkri7XOlTdtk17BezlV3A==}
+    cpu: [x64]
+    os: [darwin]
+
+  '@rollup/rollup-freebsd-arm64@4.54.0':
+    resolution: {integrity: sha512-miSvuFkmvFbgJ1BevMa4CPCFt5MPGw094knM64W9I0giUIMMmRYcGW/JWZDriaw/k1kOBtsWh1z6nIFV1vPNtA==}
+    cpu: [arm64]
+    os: [freebsd]
+
+  '@rollup/rollup-freebsd-x64@4.54.0':
+    resolution: {integrity: sha512-KGXIs55+b/ZfZsq9aR026tmr/+7tq6VG6MsnrvF4H8VhwflTIuYh+LFUlIsRdQSgrgmtM3fVATzEAj4hBQlaqQ==}
+    cpu: [x64]
+    os: [freebsd]
+
+  '@rollup/rollup-linux-arm-gnueabihf@4.54.0':
+    resolution: {integrity: sha512-EHMUcDwhtdRGlXZsGSIuXSYwD5kOT9NVnx9sqzYiwAc91wfYOE1g1djOEDseZJKKqtHAHGwnGPQu3kytmfaXLQ==}
+    cpu: [arm]
+    os: [linux]
+    libc: [glibc]
+
+  '@rollup/rollup-linux-arm-musleabihf@4.54.0':
+    resolution: {integrity: sha512-+pBrqEjaakN2ySv5RVrj/qLytYhPKEUwk+e3SFU5jTLHIcAtqh2rLrd/OkbNuHJpsBgxsD8ccJt5ga/SeG0JmA==}
+    cpu: [arm]
+    os: [linux]
+    libc: [musl]
+
+  '@rollup/rollup-linux-arm64-gnu@4.54.0':
+    resolution: {integrity: sha512-NSqc7rE9wuUaRBsBp5ckQ5CVz5aIRKCwsoa6WMF7G01sX3/qHUw/z4pv+D+ahL1EIKy6Enpcnz1RY8pf7bjwng==}
+    cpu: [arm64]
+    os: [linux]
+    libc: [glibc]
+
+  '@rollup/rollup-linux-arm64-musl@4.54.0':
+    resolution: {integrity: sha512-gr5vDbg3Bakga5kbdpqx81m2n9IX8M6gIMlQQIXiLTNeQW6CucvuInJ91EuCJ/JYvc+rcLLsDFcfAD1K7fMofg==}
+    cpu: [arm64]
+    os: [linux]
+    libc: [musl]
+
+  '@rollup/rollup-linux-loong64-gnu@4.54.0':
+    resolution: {integrity: sha512-gsrtB1NA3ZYj2vq0Rzkylo9ylCtW/PhpLEivlgWe0bpgtX5+9j9EZa0wtZiCjgu6zmSeZWyI/e2YRX1URozpIw==}
+    cpu: [loong64]
+    os: [linux]
+    libc: [glibc]
+
+  '@rollup/rollup-linux-ppc64-gnu@4.54.0':
+    resolution: {integrity: sha512-y3qNOfTBStmFNq+t4s7Tmc9hW2ENtPg8FeUD/VShI7rKxNW7O4fFeaYbMsd3tpFlIg1Q8IapFgy7Q9i2BqeBvA==}
+    cpu: [ppc64]
+    os: [linux]
+    libc: [glibc]
+
+  '@rollup/rollup-linux-riscv64-gnu@4.54.0':
+    resolution: {integrity: sha512-89sepv7h2lIVPsFma8iwmccN7Yjjtgz0Rj/Ou6fEqg3HDhpCa+Et+YSufy27i6b0Wav69Qv4WBNl3Rs6pwhebQ==}
+    cpu: [riscv64]
+    os: [linux]
+    libc: [glibc]
+
+  '@rollup/rollup-linux-riscv64-musl@4.54.0':
+    resolution: {integrity: sha512-ZcU77ieh0M2Q8Ur7D5X7KvK+UxbXeDHwiOt/CPSBTI1fBmeDMivW0dPkdqkT4rOgDjrDDBUed9x4EgraIKoR2A==}
+    cpu: [riscv64]
+    os: [linux]
+    libc: [musl]
+
+  '@rollup/rollup-linux-s390x-gnu@4.54.0':
+    resolution: {integrity: sha512-2AdWy5RdDF5+4YfG/YesGDDtbyJlC9LHmL6rZw6FurBJ5n4vFGupsOBGfwMRjBYH7qRQowT8D/U4LoSvVwOhSQ==}
+    cpu: [s390x]
+    os: [linux]
+    libc: [glibc]
+
+  '@rollup/rollup-linux-x64-gnu@4.54.0':
+    resolution: {integrity: sha512-WGt5J8Ij/rvyqpFexxk3ffKqqbLf9AqrTBbWDk7ApGUzaIs6V+s2s84kAxklFwmMF/vBNGrVdYgbblCOFFezMQ==}
+    cpu: [x64]
+    os: [linux]
+    libc: [glibc]
+
+  '@rollup/rollup-linux-x64-musl@4.54.0':
+    resolution: {integrity: sha512-JzQmb38ATzHjxlPHuTH6tE7ojnMKM2kYNzt44LO/jJi8BpceEC8QuXYA908n8r3CNuG/B3BV8VR3Hi1rYtmPiw==}
+    cpu: [x64]
+    os: [linux]
+    libc: [musl]
+
+  '@rollup/rollup-openharmony-arm64@4.54.0':
+    resolution: {integrity: sha512-huT3fd0iC7jigGh7n3q/+lfPcXxBi+om/Rs3yiFxjvSxbSB6aohDFXbWvlspaqjeOh+hx7DDHS+5Es5qRkWkZg==}
+    cpu: [arm64]
+    os: [openharmony]
+
+  '@rollup/rollup-win32-arm64-msvc@4.54.0':
+    resolution: {integrity: sha512-c2V0W1bsKIKfbLMBu/WGBz6Yci8nJ/ZJdheE0EwB73N3MvHYKiKGs3mVilX4Gs70eGeDaMqEob25Tw2Gb9Nqyw==}
+    cpu: [arm64]
+    os: [win32]
+
+  '@rollup/rollup-win32-ia32-msvc@4.54.0':
+    resolution: {integrity: sha512-woEHgqQqDCkAzrDhvDipnSirm5vxUXtSKDYTVpZG3nUdW/VVB5VdCYA2iReSj/u3yCZzXID4kuKG7OynPnB3WQ==}
+    cpu: [ia32]
+    os: [win32]
+
+  '@rollup/rollup-win32-x64-gnu@4.54.0':
+    resolution: {integrity: sha512-dzAc53LOuFvHwbCEOS0rPbXp6SIhAf2txMP5p6mGyOXXw5mWY8NGGbPMPrs4P1WItkfApDathBj/NzMLUZ9rtQ==}
+    cpu: [x64]
+    os: [win32]
+
+  '@rollup/rollup-win32-x64-msvc@4.54.0':
+    resolution: {integrity: sha512-hYT5d3YNdSh3mbCU1gwQyPgQd3T2ne0A3KG8KSBdav5TiBg6eInVmV+TeR5uHufiIgSFg0XsOWGW5/RhNcSvPg==}
+    cpu: [x64]
+    os: [win32]
+
+  '@types/estree@1.0.8':
+    resolution: {integrity: sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==}
+
+  fsevents@2.3.3:
+    resolution: {integrity: sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==}
+    engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
+    os: [darwin]
+
+  rollup@4.54.0:
+    resolution: {integrity: sha512-3nk8Y3a9Ea8szgKhinMlGMhGMw89mqule3KWczxhIzqudyHdCIOHw8WJlj/r329fACjKLEh13ZSk7oE22kyeIw==}
+    engines: {node: '>=18.0.0', npm: '>=8.0.0'}
+    hasBin: true
+
+snapshots:
+
+  '@rollup/rollup-android-arm-eabi@4.54.0':
+    optional: true
+
+  '@rollup/rollup-android-arm64@4.54.0':
+    optional: true
+
+  '@rollup/rollup-darwin-arm64@4.54.0':
+    optional: true
+
+  '@rollup/rollup-darwin-x64@4.54.0':
+    optional: true
+
+  '@rollup/rollup-freebsd-arm64@4.54.0':
+    optional: true
+
+  '@rollup/rollup-freebsd-x64@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-arm-gnueabihf@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-arm-musleabihf@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-arm64-gnu@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-arm64-musl@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-loong64-gnu@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-ppc64-gnu@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-riscv64-gnu@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-riscv64-musl@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-s390x-gnu@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-x64-gnu@4.54.0':
+    optional: true
+
+  '@rollup/rollup-linux-x64-musl@4.54.0':
+    optional: true
+
+  '@rollup/rollup-openharmony-arm64@4.54.0':
+    optional: true
+
+  '@rollup/rollup-win32-arm64-msvc@4.54.0':
+    optional: true
+
+  '@rollup/rollup-win32-ia32-msvc@4.54.0':
+    optional: true
+
+  '@rollup/rollup-win32-x64-gnu@4.54.0':
+    optional: true
+
+  '@rollup/rollup-win32-x64-msvc@4.54.0':
+    optional: true
+
+  '@types/estree@1.0.8': {}
+
+  fsevents@2.3.3:
+    optional: true
+
+  rollup@4.54.0:
+    dependencies:
+      '@types/estree': 1.0.8
+    optionalDependencies:
+      '@rollup/rollup-android-arm-eabi': 4.54.0
+      '@rollup/rollup-android-arm64': 4.54.0
+      '@rollup/rollup-darwin-arm64': 4.54.0
+      '@rollup/rollup-darwin-x64': 4.54.0
+      '@rollup/rollup-freebsd-arm64': 4.54.0
+      '@rollup/rollup-freebsd-x64': 4.54.0
+      '@rollup/rollup-linux-arm-gnueabihf': 4.54.0
+      '@rollup/rollup-linux-arm-musleabihf': 4.54.0
+      '@rollup/rollup-linux-arm64-gnu': 4.54.0
+      '@rollup/rollup-linux-arm64-musl': 4.54.0
+      '@rollup/rollup-linux-loong64-gnu': 4.54.0
+      '@rollup/rollup-linux-ppc64-gnu': 4.54.0
+      '@rollup/rollup-linux-riscv64-gnu': 4.54.0
+      '@rollup/rollup-linux-riscv64-musl': 4.54.0
+      '@rollup/rollup-linux-s390x-gnu': 4.54.0
+      '@rollup/rollup-linux-x64-gnu': 4.54.0
+      '@rollup/rollup-linux-x64-musl': 4.54.0
+      '@rollup/rollup-openharmony-arm64': 4.54.0
+      '@rollup/rollup-win32-arm64-msvc': 4.54.0
+      '@rollup/rollup-win32-ia32-msvc': 4.54.0
+      '@rollup/rollup-win32-x64-gnu': 4.54.0
+      '@rollup/rollup-win32-x64-msvc': 4.54.0
+      fsevents: 2.3.3

+ 47 - 0
rollup.config.js

@@ -0,0 +1,47 @@
+import { defineConfig } from 'rollup';
+import { nodeResolve } from '@rollup/plugin-node-resolve';
+import commonjs from '@rollup/plugin-commonjs';
+import terser from '@rollup/plugin-terser';
+
+export default defineConfig({
+  input: 'src/main.js',
+  output: [
+    {
+      file: 'dist/bundle-iife.js',
+      format: 'iife',
+      name: 'MonitorSDK',
+      globals: {
+        // 定义全局变量映射
+      }
+    },
+    {
+      file: 'dist/bundle-esm.js',
+      format: 'esm'
+    },
+    {
+      file: 'dist/bundle-cjs.js',
+      format: 'cjs'
+    },
+    {
+      file: 'dist/bundle-umd.js',
+      format: 'umd',
+      name: 'MonitorSDK'
+    }
+  ],
+  plugins: [
+    nodeResolve({
+      browser: true
+    }),
+    commonjs(),
+    terser({
+      format: {
+        comments: false
+      },
+      compress: {
+        drop_console: true, // 生产环境移除console
+        drop_debugger: true
+      }
+    })
+  ],
+  external: [] // 外部依赖
+});

+ 286 - 0
src/main.js

@@ -0,0 +1,286 @@
+// 内部监控平台SDK基础配置
+
+class MonitorSDK {
+  constructor(options = {}) {
+    this.config = {
+      // 基础配置
+      appId: options.appId || '',
+      apiUrl: options.apiUrl || '',
+      enableErrorTracking: options.enableErrorTracking !== false,
+      enablePerformanceTracking: options.enablePerformanceTracking !== false,
+      enableUserBehaviorTracking: options.enableUserBehaviorTracking !== false,
+      enableResourceTracking: options.enableResourceTracking !== false,
+      sampleRate: options.sampleRate || 1.0, // 采样率,默认100%
+      reportInterval: options.reportInterval || 5000, // 上报间隔,默认5秒
+      ...options
+    };
+    
+    this.init();
+  }
+  
+  init() {
+    console.log('MonitorSDK initialized');
+    
+    // 初始化各种监控模块
+    if (this.config.enableErrorTracking) {
+      this.initErrorTracking();
+    }
+    
+    if (this.config.enablePerformanceTracking) {
+      this.initPerformanceTracking();
+    }
+    
+    if (this.config.enableUserBehaviorTracking) {
+      this.initUserBehaviorTracking();
+    }
+    
+    if (this.config.enableResourceTracking) {
+      this.initResourceTracking();
+    }
+    
+    // 定期上报数据
+    this.startReporting();
+  }
+  
+  // 错误监控
+  initErrorTracking() {
+    // 监控JavaScript错误
+    window.addEventListener('error', (event) => {
+      this.reportError({
+        type: 'javascript_error',
+        message: event.message,
+        filename: event.filename,
+        lineno: event.lineno,
+        colno: event.colno,
+        stack: event.error ? event.error.stack : null,
+        timestamp: Date.now()
+      });
+    });
+    
+    // 监控未处理的Promise错误
+    window.addEventListener('unhandledrejection', (event) => {
+      this.reportError({
+        type: 'promise_rejection',
+        message: event.reason && event.reason.message ? event.reason.message : String(event.reason),
+        stack: event.reason && event.reason.stack ? event.reason.stack : null,
+        timestamp: Date.now()
+      });
+    });
+  }
+  
+  // 性能监控
+  initPerformanceTracking() {
+    // 页面加载性能
+    if (performance && performance.timing) {
+      setTimeout(() => {
+        const perfData = {
+          type: 'performance',
+          navigationStart: performance.timing.navigationStart,
+          // DNS查询耗时
+          dnsTime: performance.timing.domainLookupEnd - performance.timing.domainLookupStart,
+          // TCP连接耗时
+          tcpTime: performance.timing.connectEnd - performance.timing.connectStart,
+          // 请求响应耗时
+          responseTime: performance.timing.responseEnd - performance.timing.requestStart,
+          // DOM解析耗时
+          domParseTime: performance.timing.domComplete - performance.timing.domLoading,
+          // DOM内容加载完成耗时
+          domContentLoadedTime: performance.timing.domContentLoadedEventEnd - performance.timing.navigationStart,
+          // 页面加载完成总耗时
+          loadCompleteTime: performance.timing.loadEventEnd - performance.timing.navigationStart,
+          timestamp: Date.now()
+        };
+        this.reportData(perfData);
+      }, 0);
+    }
+    
+    // 监控Web Vitals
+    this.measureWebVitals();
+  }
+  
+  // 用户行为监控
+  initUserBehaviorTracking() {
+    // 监控页面点击事件
+    document.addEventListener('click', (event) => {
+      const target = event.target;
+      this.reportData({
+        type: 'user_behavior',
+        action: 'click',
+        element: target.tagName,
+        elementId: target.id,
+        elementClass: target.className,
+        pageUrl: window.location.href,
+        timestamp: Date.now()
+      });
+    });
+    
+    // 监控页面停留时间
+    let pageVisibleTime = Date.now();
+    document.addEventListener('visibilitychange', () => {
+      if (document.visibilityState === 'hidden') {
+        this.reportData({
+          type: 'page_stay',
+          duration: Date.now() - pageVisibleTime,
+          pageUrl: window.location.href,
+          timestamp: Date.now()
+        });
+      } else {
+        pageVisibleTime = Date.now();
+      }
+    });
+  }
+  
+  // 资源加载监控
+  initResourceTracking() {
+    if (performance && performance.getEntriesByType) {
+      // 监控资源加载
+      const resources = performance.getEntriesByType('resource');
+      resources.forEach(resource => {
+        this.reportData({
+          type: 'resource_load',
+          name: resource.name,
+          duration: resource.duration,
+          entryType: resource.entryType,
+          timestamp: Date.now()
+        });
+      });
+    }
+  }
+  
+  // 上报错误
+  reportError(errorData) {
+    // 检查采样率
+    if (Math.random() > this.config.sampleRate) {
+      return; // 根据采样率决定是否上报
+    }
+    
+    this.reportData({
+      type: 'error',
+      ...errorData
+    });
+  }
+  
+  // 上报数据
+  reportData(data) {
+    // 检查采样率
+    if (Math.random() > this.config.sampleRate) {
+      return; // 根据采样率决定是否上报
+    }
+    
+    // 添加通用信息
+    data.sessionId = this.getSessionId();
+    data.userAgent = navigator.userAgent;
+    data.url = window.location.href;
+    data.timestamp = Date.now();
+    
+    // 发送到服务端
+    if (this.config.apiUrl) {
+      try {
+        // 使用navigator.sendBeacon发送数据,即使页面关闭也能发送
+        if (navigator.sendBeacon) {
+          navigator.sendBeacon(
+            this.config.apiUrl,
+            JSON.stringify(data)
+          );
+        } else {
+          // 备用方案:使用fetch
+          fetch(this.config.apiUrl, {
+            method: 'POST',
+            body: JSON.stringify(data),
+            headers: {
+              'Content-Type': 'application/json'
+            }
+          }).catch(err => {
+            console.error('MonitorSDK: Failed to send data:', err);
+          });
+        }
+      } catch (error) {
+        console.error('MonitorSDK: Error sending data:', error);
+      }
+    } else {
+      // 开发模式下打印到控制台
+      console.log('MonitorSDK: Data to send:', data);
+    }
+  }
+  
+  // 开始定期上报
+  startReporting() {
+    setInterval(() => {
+      // 这里可以实现批量上报逻辑
+    }, this.config.reportInterval);
+  }
+  
+  // 获取会话ID
+  getSessionId() {
+    if (!this.sessionId) {
+      this.sessionId = 'session_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
+    }
+    return this.sessionId;
+  }
+  
+  // 测量Web Vitals
+  measureWebVitals() {
+    // 这里可以集成web-vitals库来测量核心Web指标
+    // 如CLS (Cumulative Layout Shift), FID (First Input Delay), LCP (Largest Contentful Paint)
+    if ('PerformanceObserver' in window) {
+      // LCP (Largest Contentful Paint)
+      new PerformanceObserver((entryList) => {
+        const entries = entryList.getEntries();
+        const lastEntry = entries[entries.length - 1];
+        if (lastEntry) {
+          this.reportData({
+            type: 'web_vitals',
+            metric: 'LCP',
+            value: lastEntry.startTime,
+            timestamp: Date.now()
+          });
+        }
+      }).observe({ entryTypes: ['largest-contentful-paint'] });
+      
+      // FID (First Input Delay)
+      new PerformanceObserver((entryList) => {
+        const firstInput = entryList.getEntries()[0];
+        if (firstInput) {
+          const fid = firstInput.processingStart - firstInput.startTime;
+          this.reportData({
+            type: 'web_vitals',
+            metric: 'FID',
+            value: fid,
+            timestamp: Date.now()
+          });
+        }
+      }).observe({ entryTypes: ['first-input'] });
+      
+      // CLS (Cumulative Layout Shift)
+      let clsValue = 0;
+      new PerformanceObserver((entryList) => {
+        for (const entry of entryList.getEntries()) {
+          if (!entry.hadRecentInput) {
+            clsValue += entry.value;
+          }
+        }
+        this.reportData({
+          type: 'web_vitals',
+          metric: 'CLS',
+          value: clsValue,
+          timestamp: Date.now()
+        });
+      }).observe({ entryTypes: ['layout-shift'] });
+    }
+  }
+}
+
+// 初始化监控SDK
+const monitor = new MonitorSDK({
+  appId: 'monitor-app',
+  apiUrl: '/api/monitor', // 这里应该是实际的API地址
+  enableErrorTracking: true,
+  enablePerformanceTracking: true,
+  enableUserBehaviorTracking: true,
+  enableResourceTracking: true,
+  sampleRate: 1.0
+});
+
+// 将monitor实例暴露到全局,方便调用
+window.MonitorSDK = MonitorSDK;
+window.monitorInstance = monitor;

+ 34 - 0
tsconfig.json

@@ -0,0 +1,34 @@
+{
+  "include": [],
+  "compilerOptions": {
+    "resolveJsonModule": true,
+    "target": "ES2022",
+    "module": "ESNext",
+    "lib": ["ES2022", "DOM"],
+    "importHelpers": true,
+    "declaration": true,
+    "declarationMap": true,
+    "sourceMap": true,
+    "strict": true,
+    "noImplicitAny": true,
+    "strictNullChecks": true,
+    "strictFunctionTypes": true,
+    "strictPropertyInitialization": true,
+    "noImplicitThis": true,
+    "alwaysStrict": true,
+    "noUnusedLocals": true,
+    "noUnusedParameters": true,
+    "noImplicitReturns": true,
+    "noFallthroughCasesInSwitch": true,
+    "moduleResolution": "bundler",
+    "baseUrl": "./",
+    "paths": {
+      "*": ["src/*", "node_modules/*"],
+      "@websee/*":["packages/*/src"]
+    },
+    "jsx": "react-jsx",
+    "esModuleInterop": true,
+    "skipLibCheck": true,
+    "forceConsistentCasingInFileNames": true
+  }
+}