From d773f9da2974510cba3193c7c6b9f20e77feaf39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Allen=20Zhang=20=28=E5=BC=A0=E6=B6=9B=29?= Date: Thu, 31 Oct 2024 14:37:21 +0800 Subject: [PATCH] feat: update --- packages/canyon-platform/public/0.0.1/main.js | 42 +++++++++++++++++++ tools/canyon-collect/README.md | 3 ++ tools/canyon-collect/main.js | 0 3 files changed, 45 insertions(+) create mode 100644 packages/canyon-platform/public/0.0.1/main.js create mode 100644 tools/canyon-collect/README.md create mode 100644 tools/canyon-collect/main.js diff --git a/packages/canyon-platform/public/0.0.1/main.js b/packages/canyon-platform/public/0.0.1/main.js new file mode 100644 index 00000000..295a8725 --- /dev/null +++ b/packages/canyon-platform/public/0.0.1/main.js @@ -0,0 +1,42 @@ +if (window.__coverage__ && window.__canyon__analytics__dsn__) { + function collectCoverageData(timing) { + const data = { + coverage: JSON.stringify(Object.entries(window.__coverage__).map(([path, {b,f,s}]) => ({ + path, + b, + f, + s, + })).reduce((acc, {path, b, f, s}) => { + acc[path] = {b, f, s}; + return acc; + })), + canyonID:'canyon-platform', + addInfo:{ + // 时机 + timing: timing, + } + } + navigator.sendBeacon( + window.__canyon__analytics__dsn__, + JSON.stringify(data), + ); + } + +// beforeunload 事件:当用户即将离开页面时触发 + window.addEventListener("beforeunload", () => { + collectCoverageData("beforeunload"); + }); + +// unload 事件:在页面完全卸载前触发 + window.addEventListener("unload", () => { + collectCoverageData("unload"); + }); + +// visibilitychange 事件:当页面变为不可见(如切换到其他标签页)时触发 + document.addEventListener("visibilitychange", () => { + if (document.visibilityState === "hidden") { + collectCoverageData("visibilitychange"); + } + }); + +} diff --git a/tools/canyon-collect/README.md b/tools/canyon-collect/README.md new file mode 100644 index 00000000..fd0132e5 --- /dev/null +++ b/tools/canyon-collect/README.md @@ -0,0 +1,3 @@ +Canyon Collect + +js sdk,直接script引入 diff --git a/tools/canyon-collect/main.js b/tools/canyon-collect/main.js new file mode 100644 index 00000000..e69de29b