mirror of
https://github.com/MCKero6423/fuwari.git
synced 2026-10-02 03:15:35 +00:00
fix: 修复 Umami v3 API 浏览量统计
This commit is contained in:
1 parent
294fe4c092
commit
af0a425c72
4 files changed
+36
-43
No files matched your search
@@ -122,14 +122,15 @@ const { remarkPluginFrontmatter } = await entry.render();
|
|||||||
// 第一步:获取网站ID和token
|
// 第一步:获取网站ID和token
|
||||||
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
||||||
if (!shareResponse.ok) {
|
if (!shareResponse.ok) {
|
||||||
throw new Error('获取分享信息失败');
|
throw new Error(`获取分享信息失败: ${shareResponse.status}`);
|
||||||
}
|
}
|
||||||
const shareData = await shareResponse.json();
|
const shareData = await shareResponse.json();
|
||||||
const { websiteId, token } = shareData;
|
const { websiteId, token } = shareData;
|
||||||
|
|
||||||
// 第二步:获取统计数据
|
// 第二步:获取统计数据
|
||||||
const currentTimestamp = Date.now();
|
const currentTimestamp = Date.now();
|
||||||
const statsUrl = `${umamiConfig.baseUrl}/api/websites/${websiteId}/stats?startAt=0&endAt=${currentTimestamp}&unit=hour&timezone=${encodeURIComponent(umamiConfig.timezone)}&url=%2Fposts%2F${slug}%2F&compare=false`;
|
// 使用 path=eq. 前缀进行精确匹配
|
||||||
|
const statsUrl = `${umamiConfig.baseUrl}/api/websites/${websiteId}/stats?startAt=0&endAt=${currentTimestamp}&unit=hour&timezone=${encodeURIComponent(umamiConfig.timezone)}&path=eq./posts/${slug}/&compare=false`;
|
||||||
|
|
||||||
const statsResponse = await fetch(statsUrl, {
|
const statsResponse = await fetch(statsUrl, {
|
||||||
headers: {
|
headers: {
|
||||||
@@ -138,11 +139,12 @@ const { remarkPluginFrontmatter } = await entry.render();
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!statsResponse.ok) {
|
if (!statsResponse.ok) {
|
||||||
throw new Error('获取统计数据失败');
|
throw new Error(`获取统计数据失败: ${statsResponse.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const statsData = await statsResponse.json();
|
const statsData = await statsResponse.json();
|
||||||
const pageViews = statsData.pageviews?.value || 0;
|
// Umami v2/v3 兼容:直接读取 pageviews
|
||||||
|
const pageViews = statsData.pageviews || 0;
|
||||||
|
|
||||||
const displayElement = document.getElementById(`page-views-${slug}`);
|
const displayElement = document.getElementById(`page-views-${slug}`);
|
||||||
if (displayElement) {
|
if (displayElement) {
|
||||||
|
|||||||
@@ -102,14 +102,15 @@ const className = Astro.props.class;
|
|||||||
// 第一步:获取网站ID和token
|
// 第一步:获取网站ID和token
|
||||||
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
||||||
if (!shareResponse.ok) {
|
if (!shareResponse.ok) {
|
||||||
throw new Error('获取分享信息失败');
|
throw new Error(`获取分享信息失败: ${shareResponse.status}`);
|
||||||
}
|
}
|
||||||
const shareData = await shareResponse.json();
|
const shareData = await shareResponse.json();
|
||||||
const { websiteId, token } = shareData;
|
const { websiteId, token } = shareData;
|
||||||
|
|
||||||
// 第二步:获取统计数据
|
// 第二步:获取统计数据
|
||||||
const currentTimestamp = Date.now();
|
const currentTimestamp = Date.now();
|
||||||
const statsUrl = `${umamiConfig.baseUrl}/api/websites/${websiteId}/stats?startAt=0&endAt=${currentTimestamp}&unit=hour&timezone=${encodeURIComponent(umamiConfig.timezone)}&url=%2Fposts%2F${slug}%2F&compare=false`;
|
// 使用 path=eq. 前缀进行精确匹配
|
||||||
|
const statsUrl = `${umamiConfig.baseUrl}/api/websites/${websiteId}/stats?startAt=0&endAt=${currentTimestamp}&unit=hour&timezone=${encodeURIComponent(umamiConfig.timezone)}&path=eq./posts/${slug}/&compare=false`;
|
||||||
|
|
||||||
const statsResponse = await fetch(statsUrl, {
|
const statsResponse = await fetch(statsUrl, {
|
||||||
headers: {
|
headers: {
|
||||||
@@ -118,12 +119,13 @@ const className = Astro.props.class;
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!statsResponse.ok) {
|
if (!statsResponse.ok) {
|
||||||
throw new Error('获取统计数据失败');
|
throw new Error(`获取统计数据失败: ${statsResponse.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const statsData = await statsResponse.json();
|
const statsData = await statsResponse.json();
|
||||||
const pageViews = statsData.pageviews?.value || 0;
|
// Umami v2/v3 兼容
|
||||||
const visits = statsData.visits?.value || 0;
|
const pageViews = statsData.pageviews || 0;
|
||||||
|
const visits = statsData.visitors || 0;
|
||||||
|
|
||||||
const displayElement = document.getElementById('page-views-display');
|
const displayElement = document.getElementById('page-views-display');
|
||||||
if (displayElement) {
|
if (displayElement) {
|
||||||
|
|||||||
@@ -56,12 +56,12 @@ const config = profileConfig;
|
|||||||
// 第一步:获取网站ID和token
|
// 第一步:获取网站ID和token
|
||||||
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
||||||
if (!shareResponse.ok) {
|
if (!shareResponse.ok) {
|
||||||
throw new Error('获取分享信息失败');
|
throw new Error(`获取分享信息失败: ${shareResponse.status}`);
|
||||||
}
|
}
|
||||||
const shareData = await shareResponse.json();
|
const shareData = await shareResponse.json();
|
||||||
const { websiteId, token } = shareData;
|
const { websiteId, token } = shareData;
|
||||||
|
|
||||||
// 第二步:获取全站统计数据(不指定url参数获取全站数据)
|
// 第二步:获取全站统计数据(不指定path参数获取全站数据)
|
||||||
const currentTimestamp = Date.now();
|
const currentTimestamp = Date.now();
|
||||||
const statsUrl = `${umamiConfig.baseUrl}/api/websites/${websiteId}/stats?startAt=0&endAt=${currentTimestamp}&unit=hour&timezone=${encodeURIComponent(umamiConfig.timezone)}&compare=false`;
|
const statsUrl = `${umamiConfig.baseUrl}/api/websites/${websiteId}/stats?startAt=0&endAt=${currentTimestamp}&unit=hour&timezone=${encodeURIComponent(umamiConfig.timezone)}&compare=false`;
|
||||||
|
|
||||||
@@ -72,11 +72,12 @@ const config = profileConfig;
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!statsResponse.ok) {
|
if (!statsResponse.ok) {
|
||||||
throw new Error('获取统计数据失败');
|
throw new Error(`获取统计数据失败: ${statsResponse.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const statsData = await statsResponse.json();
|
const statsData = await statsResponse.json();
|
||||||
const pageviews = statsData.pageviews?.value || 0;
|
// Umami v2/v3 兼容
|
||||||
|
const pageviews = statsData.pageviews || 0;
|
||||||
|
|
||||||
const statsElement = document.getElementById('site-stats');
|
const statsElement = document.getElementById('site-stats');
|
||||||
if (statsElement) {
|
if (statsElement) {
|
||||||
|
|||||||
+18
-30
@@ -12,33 +12,26 @@ import { LinkPreset } from "./types/config";
|
|||||||
export const siteConfig: SiteConfig = {
|
export const siteConfig: SiteConfig = {
|
||||||
title: "MC_Kero blog",
|
title: "MC_Kero blog",
|
||||||
subtitle: "MC_Kero的博客,有时候会写点文章",
|
subtitle: "MC_Kero的博客,有时候会写点文章",
|
||||||
lang: "zh_CN", // Language code, e.g. 'en', 'zh_CN', 'ja', etc.
|
lang: "zh_CN",
|
||||||
themeColor: {
|
themeColor: {
|
||||||
hue: 355, // Default hue for the theme color, from 0 to 360. e.g. red: 0, teal: 200, cyan: 250, pink: 345
|
hue: 355,
|
||||||
fixed: false, // Hide the theme color picker for visitors
|
fixed: false,
|
||||||
},
|
},
|
||||||
banner: {
|
banner: {
|
||||||
enable: false,
|
enable: false,
|
||||||
src: "assets/images/demo-banner.png", // Relative to the /src directory. Relative to the /public directory if it starts with '/'
|
src: "assets/images/demo-banner.png",
|
||||||
position: "center", // Equivalent to object-position, only supports 'top', 'center', 'bottom'. 'center' by default
|
position: "center",
|
||||||
credit: {
|
credit: {
|
||||||
enable: false, // Display the credit text of the banner image
|
enable: false,
|
||||||
text: "", // Credit text to be displayed
|
text: "",
|
||||||
url: "", // (Optional) URL link to the original artwork or artist's page
|
url: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
toc: {
|
toc: {
|
||||||
enable: true, // Display the table of contents on the right side of the post
|
enable: true,
|
||||||
depth: 2, // Maximum heading depth to show in the table, from 1 to 3
|
depth: 2,
|
||||||
},
|
},
|
||||||
favicon: [
|
favicon: [],
|
||||||
// Leave this array empty to use the default favicon
|
|
||||||
// {
|
|
||||||
// src: '/favicon/icon.png', // Path of the favicon, relative to the /public directory
|
|
||||||
// theme: 'light', // (Optional) Either 'light' or 'dark', set only if you have different favicons for light and dark mode
|
|
||||||
// sizes: '32x32', // (Optional) Size of the favicon, set only if you have favicons of different sizes
|
|
||||||
// }
|
|
||||||
],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const navBarConfig: NavBarConfig = {
|
export const navBarConfig: NavBarConfig = {
|
||||||
@@ -50,27 +43,25 @@ export const navBarConfig: NavBarConfig = {
|
|||||||
LinkPreset.About,
|
LinkPreset.About,
|
||||||
{
|
{
|
||||||
name: "状态",
|
name: "状态",
|
||||||
url: "https://status.mckero.com", // Internal links should not include the base path, as it is automatically added
|
url: "https://status.mckero.com",
|
||||||
external: true, // Show an external link icon and will open in a new tab
|
external: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "统计",
|
name: "统计",
|
||||||
url: "https://umami.mckero.com/share/v4wXkxwCYns2ypLN/blog.mckero.com", // Internal links should not include the base path, as it is automatically added
|
url: "https://umami.mckero.com/share/v4wXkxwCYns2ypLN/blog.mckero.com",
|
||||||
external: true, // Show an external link icon and will open in a new tab
|
external: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
export const profileConfig: ProfileConfig = {
|
export const profileConfig: ProfileConfig = {
|
||||||
avatar: "assets/images/demo-avatar.png", // Relative to the /src directory. Relative to the /public directory if it starts with '/'
|
avatar: "assets/images/demo-avatar.png",
|
||||||
name: "MC_Kero",
|
name: "MC_Kero",
|
||||||
bio: "依稀当年泪目干!",
|
bio: "依稀当年泪目干!",
|
||||||
links: [
|
links: [
|
||||||
{
|
{
|
||||||
name: "Github",
|
name: "Github",
|
||||||
icon: "fa6-brands:github", // Visit https://icones.js.org/ for icon codes
|
icon: "fa6-brands:github",
|
||||||
// You will need to install the corresponding icon set if it's not already included
|
|
||||||
// `pnpm add @iconify-json/<icon-set-name>`
|
|
||||||
url: "https://github.com/MCKero6423",
|
url: "https://github.com/MCKero6423",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -78,7 +69,6 @@ export const profileConfig: ProfileConfig = {
|
|||||||
icon: "fa6-brands:bilibili",
|
icon: "fa6-brands:bilibili",
|
||||||
url: "https://space.bilibili.com/3493260722964658",
|
url: "https://space.bilibili.com/3493260722964658",
|
||||||
},
|
},
|
||||||
|
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -90,14 +80,12 @@ export const licenseConfig: LicenseConfig = {
|
|||||||
|
|
||||||
export const umamiConfig: UmamiConfig = {
|
export const umamiConfig: UmamiConfig = {
|
||||||
enable: true,
|
enable: true,
|
||||||
baseUrl: "https://umami.mckero.com",
|
baseUrl: "https://umami.mckero.com", // 自部署版本,不需要加 /analytics/us
|
||||||
shareId: "v4wXkxwCYns2ypLN",
|
shareId: "v4wXkxwCYns2ypLN",
|
||||||
timezone: "Asia/Shanghai",
|
timezone: "Asia/Shanghai",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const expressiveCodeConfig: ExpressiveCodeConfig = {
|
export const expressiveCodeConfig: ExpressiveCodeConfig = {
|
||||||
// Note: Some styles (such as background color) are being overridden, see the astro.config.mjs file.
|
|
||||||
// Please select a dark theme, as this blog theme currently only supports dark background color
|
|
||||||
theme: "github-dark",
|
theme: "github-dark",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user