Skip to content

Commit

Permalink
stats
Browse files Browse the repository at this point in the history
  • Loading branch information
dietrichmax committed Jan 25, 2024
1 parent ef23daa commit 55475a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
12 changes: 0 additions & 12 deletions src/data/external/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
22 changes: 18 additions & 4 deletions pages/stats.js → src/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
<>
<Layout>
Expand All @@ -430,7 +446,7 @@ export default function Dashboard({
<StatsGrid>
<Title>Web Analytics</Title>
<StatsLargeGrid>
<GridStats>{liveViews[0].visitors}</GridStats>
<GridStats>{liveViews}</GridStats>
<GridStatsDescription>
Visitors in the last 5 minutes
</GridStatsDescription>
Expand Down Expand Up @@ -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()) || []
Expand Down Expand Up @@ -900,7 +915,6 @@ export async function getStaticProps() {
visitsSummary,
photosCount,
activities,
liveViews,
topPosts,
consentCount,
biggestTrafficSource,
Expand Down

0 comments on commit 55475a5

Please sign in to comment.