feat: add analytics to website and small improvements

This commit is contained in:
Levente Orban
2025-08-27 16:06:12 +02:00
parent 7e6dbf15f9
commit 1ea87ab3ae
8 changed files with 60 additions and 99 deletions

View File

@@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
%sveltekit.head%
%sveltekit.head% %ANALYTICS_SCRIPT%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>

27
src/hooks.server.ts Normal file
View File

@@ -0,0 +1,27 @@
import { env } from '$env/dynamic/private';
export const handle = async ({ event, resolve }) => {
// Check if analytics is enabled
const analyticsEnabled = env.ANALYTICS === 'true';
// Define the analytics script HTML
const analyticsScript = analyticsEnabled
? '<script defer src="https://analytics.dalev.hu/script.js" data-website-id="7425d098-e340-4464-bd03-c2e47b004cd9"></script>'
: '';
// Replace the placeholder with the actual script or empty string
const response = await resolve(event);
if (response.headers.get('content-type')?.includes('text/html')) {
const html = await response.text();
const modifiedHtml = html.replace('%ANALYTICS_SCRIPT%', analyticsScript);
return new Response(modifiedHtml, {
headers: response.headers,
status: response.status,
statusText: response.statusText
});
}
return response;
};