umami-view.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // 从配置文件中获取 umami 的配置
  2. const website_id = CONFIG.web_analytics.umami.website_id;
  3. // 拼接请求地址
  4. const request_url = `${CONFIG.web_analytics.umami.api_server}/websites/${website_id}/stats`;
  5. const start_time = new Date(CONFIG.web_analytics.umami.start_time).getTime();
  6. const end_time = new Date().getTime();
  7. const token = CONFIG.web_analytics.umami.token;
  8. // 检查配置是否为空
  9. if (!website_id) {
  10. throw new Error("Umami website_id is empty");
  11. }
  12. if (!request_url) {
  13. throw new Error("Umami request_url is empty");
  14. }
  15. if (!start_time) {
  16. throw new Error("Umami start_time is empty");
  17. }
  18. if (!token) {
  19. throw new Error("Umami token is empty");
  20. }
  21. // 构造请求参数
  22. const params = new URLSearchParams({
  23. startAt: start_time,
  24. endAt: end_time,
  25. });
  26. // 构造请求头
  27. const request_header = {
  28. method: "GET",
  29. headers: {
  30. "Content-Type": "application/json",
  31. "x-umami-api-key": "oZKCH3msvqt10VlXKwoJvHclmaS4bVx0",
  32. },
  33. };
  34. // 获取站点统计数据
  35. async function siteStats() {
  36. try {
  37. const response = await fetch(`${request_url}?${params}`, request_header);
  38. const data = await response.json();
  39. const uniqueVisitors = data.uniques.value; // 获取独立访客数
  40. const pageViews = data.pageviews.value; // 获取页面浏览量
  41. let pvCtn = document.querySelector("#umami-site-pv-container");
  42. if (pvCtn) {
  43. let ele = document.querySelector("#umami-site-pv");
  44. if (ele) {
  45. ele.textContent = pageViews; // 设置页面浏览量
  46. pvCtn.style.display = "inline"; // 将元素显示出来
  47. }
  48. }
  49. let uvCtn = document.querySelector("#umami-site-uv-container");
  50. if (uvCtn) {
  51. let ele = document.querySelector("#umami-site-uv");
  52. if (ele) {
  53. ele.textContent = uniqueVisitors;
  54. uvCtn.style.display = "inline";
  55. }
  56. }
  57. } catch (error) {
  58. console.error(error);
  59. return "-1";
  60. }
  61. }
  62. // 获取页面浏览量
  63. async function pageStats(path) {
  64. try {
  65. const response = await fetch(`${request_url}?${params}&url=${path}`, request_header);
  66. const data = await response.json();
  67. const pageViews = data.pageviews.value;
  68. let viewCtn = document.querySelector("#umami-page-views-container");
  69. if (viewCtn) {
  70. let ele = document.querySelector("#umami-page-views");
  71. if (ele) {
  72. ele.textContent = pageViews;
  73. viewCtn.style.display = "inline";
  74. }
  75. }
  76. } catch (error) {
  77. console.error(error);
  78. return "-1";
  79. }
  80. }
  81. siteStats();
  82. // 获取页面容器
  83. let viewCtn = document.querySelector("#umami-page-views-container");
  84. // 如果页面容器存在,则获取页面浏览量
  85. if (viewCtn) {
  86. let path = window.location.pathname;
  87. let target = decodeURI(path.replace(/\/*(index.html)?$/, "/"));
  88. pageStats(target);
  89. }