From 55475a54a483b02c7fca8e7d28a79aa368a1a4bb Mon Sep 17 00:00:00 2001 From: Max Dietrich Date: Thu, 25 Jan 2024 12:13:40 +0100 Subject: [PATCH] stats --- src/data/external/analytics.js | 12 ------------ {pages => src}/stats.js | 22 ++++++++++++++++++---- 2 files changed, 18 insertions(+), 16 deletions(-) rename {pages => src}/stats.js (97%) diff --git a/src/data/external/analytics.js b/src/data/external/analytics.js index 5fa00b5d..14d561a6 100644 --- a/src/data/external/analytics.js +++ b/src/data/external/analytics.js @@ -48,18 +48,6 @@ export async function getMatomoActions() { return actions } -export async function getMatomoLiveCounter() { - const res = await fetch( - `${process.env.NEXT_PUBLIC_MATOMO_URL}?method=Live.getCounters&idSite=${process.env.NEXT_PUBLIC_MATOMO_SITE_ID}&lastMinutes=5&module=API&format=JSON&token_auth=${process.env.NEXT_PUBLIC_MATOMO_API_KEY}` - ) - const liveViews = await res.json() - if (liveViews.errors) { - console.error(liveViews.errors) - throw new Error("Failed to fetch Matomo Actions") - } - return liveViews -} - export async function getMatomoPageViews() { const getViews = encodeURI( `${process.env.NEXT_PUBLIC_MATOMO_URL}?method=Actions.get&idSite=${process.env.NEXT_PUBLIC_MATOMO_SITE_ID}&period=day&date=previous30&module=API&format=JSON&token_auth=${process.env.NEXT_PUBLIC_MATOMO_API_KEY}` diff --git a/pages/stats.js b/src/stats.js similarity index 97% rename from pages/stats.js rename to src/stats.js index f0b17e9f..b8c80f80 100644 --- a/pages/stats.js +++ b/src/stats.js @@ -330,13 +330,13 @@ export default function Dashboard({ visitsSummary, photosCount, activities, - liveViews, topPosts, consentCount, biggestTrafficSource, thanks, commentsCount, }) { + const [liveViews, setLiveViews] = useState(0) const router = useRouter() const { forkCount } = githubStats.user.repository @@ -413,6 +413,22 @@ export default function Dashboard({ ? consentCount.find((element) => element.label === "consent - false") : 0 + async function getMatomoLiveCounter() { + const res = await fetch( + `${process.env.NEXT_PUBLIC_MATOMO_URL}?method=Live.getCounters&idSite=${process.env.NEXT_PUBLIC_MATOMO_SITE_ID}&lastMinutes=5&module=API&format=JSON&token_auth=${process.env.NEXT_PUBLIC_MATOMO_API_KEY}` + ) + const liveViews = await res.json() + if (liveViews.errors) { + console.error(liveViews.errors) + throw new Error("Failed to fetch Live Views") + } + setLiveViews(liveViews[0].visitors) + } + + useEffect(() => { + getMatomoLiveCounter() + }, []) + return ( <> @@ -430,7 +446,7 @@ export default function Dashboard({ Web Analytics - {liveViews[0].visitors} + {liveViews} Visitors in the last 5 minutes @@ -869,7 +885,6 @@ export async function getStaticProps() { const allWebmentions = (await fetchWebmentions()) || [] const visitsSummary = (await getMatomoVisitsSummary()) || [] const activities = (await getAllActivities()) || [] - const liveViews = (await getMatomoLiveCounter()) || [] const topPosts = (await getMatomoTopPageUrls()) || [] const consentCount = (await getMatomoConsent()) || [] const biggestTrafficSource = (await getBiggestTrafficSource()) || [] @@ -900,7 +915,6 @@ export async function getStaticProps() { visitsSummary, photosCount, activities, - liveViews, topPosts, consentCount, biggestTrafficSource,