forked from jmug/cactoide
feat: add analytics to website and small improvements
This commit is contained in:
@@ -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
27
src/hooks.server.ts
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user