Add the Umami script and the Website Info
(#10)
Rather barebones for now because I can go several ways from there
This commit is contained in:
parent
48051690a3
commit
d42bb932cd
7 changed files with 100 additions and 0 deletions
44
netlify/functions/umami.ts
Normal file
44
netlify/functions/umami.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import { UmamiInfo } from "#Infos/Website/Umami.js";
|
||||
import {type Handler} from "@netlify/functions";
|
||||
|
||||
const handler: Handler = async () => {
|
||||
const api_server = "https://api.umami.is/v1";
|
||||
const website_id = "3461d539-c2fb-4930-9d4a-a0e4016a174a";
|
||||
const now = new Date();
|
||||
const response = await fetch(`${api_server}/websites/${website_id}/stats?startAt=${Number(new Date("2025"))}&endAt=${Number(now)}`, {
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
"x-umami-api-key": process.env.API_UMAMI!
|
||||
},
|
||||
});
|
||||
|
||||
// From https://github.com/umami-software/api-client/blob/master/src/types.ts
|
||||
// Not using the package directly because of serious issues I consider it to have
|
||||
const umami = await response.json() as {
|
||||
pageviews: { value: number; prev: number };
|
||||
visitors: { value: number; prev: number };
|
||||
visits: { value: number; prev: number };
|
||||
bounces: { value: number; prev: number };
|
||||
totaltime: { value: number; prev: number };
|
||||
};
|
||||
|
||||
if (!umami) {
|
||||
return {
|
||||
statusCode: 404,
|
||||
};
|
||||
}
|
||||
|
||||
const info: UmamiInfo = {
|
||||
pageviews: umami.pageviews.value,
|
||||
visits: umami.visits.value,
|
||||
visitors: umami.visitors.value,
|
||||
totaltime: umami.totaltime.value
|
||||
};
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify(info),
|
||||
};
|
||||
};
|
||||
|
||||
export {handler};
|
Loading…
Add table
Add a link
Reference in a new issue