-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Allen Zhang (张涛)
committed
Oct 31, 2024
1 parent
c2b05df
commit 2fae4bf
Showing
1 changed file
with
36 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,44 @@ | ||
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-test1', | ||
addInfo:{ | ||
// 时机 | ||
timing: timing, | ||
setTimeout(()=>{ | ||
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-test1', | ||
addInfo:{ | ||
// 时机 | ||
timing: timing, | ||
} | ||
} | ||
navigator.sendBeacon( | ||
window.__canyon__analytics__dsn__, | ||
JSON.stringify(data), | ||
); | ||
} | ||
navigator.sendBeacon( | ||
window.__canyon__analytics__dsn__, | ||
JSON.stringify(data), | ||
); | ||
} | ||
|
||
// beforeunload 事件:当用户即将离开页面时触发 | ||
window.addEventListener("beforeunload", () => { | ||
collectCoverageData("beforeunload"); | ||
}); | ||
window.addEventListener("beforeunload", () => { | ||
collectCoverageData("beforeunload"); | ||
}); | ||
|
||
// unload 事件:在页面完全卸载前触发 | ||
window.addEventListener("unload", () => { | ||
collectCoverageData("unload"); | ||
}); | ||
window.addEventListener("unload", () => { | ||
collectCoverageData("unload"); | ||
}); | ||
|
||
// visibilitychange 事件:当页面变为不可见(如切换到其他标签页)时触发 | ||
document.addEventListener("visibilitychange", () => { | ||
if (document.visibilityState === "hidden") { | ||
collectCoverageData("visibilitychange"); | ||
} | ||
}); | ||
} | ||
document.addEventListener("visibilitychange", () => { | ||
if (document.visibilityState === "hidden") { | ||
collectCoverageData("visibilitychange"); | ||
} | ||
}); | ||
} | ||
|
||
},500) |