BLOG-112 feat: add Google Analytics integration and update environment variables
- Implement Google Analytics script loading in a new component. - Add GA_MEASUREMENT_ID to the environment configuration. - Update layout to include GoogleAnalytics component. - Enhance app.html with viewport and description meta tags.
This commit is contained in:
parent
18f29655bf
commit
db74a9b166
@ -24,4 +24,5 @@ ENV NODE_ENV=production
|
||||
ENV HOSTNAME=0.0.0.0
|
||||
ENV PORT=3000
|
||||
ENV PUBLIC_API_BASE_URL=http://127.0.0.1:8080/
|
||||
ENV PUBLIC_GA_MEASUREMENT_ID=
|
||||
CMD ["node", "build"]
|
||||
|
5
frontend/src/app.d.ts
vendored
5
frontend/src/app.d.ts
vendored
@ -15,6 +15,11 @@ declare global {
|
||||
|
||||
declare const __VERSION__: string;
|
||||
}
|
||||
|
||||
interface Window {
|
||||
dataLayer: unknown[];
|
||||
gtag: (command: 'config' | 'set' | 'js' | 'event', ...args: unknown[]) => void;
|
||||
}
|
||||
}
|
||||
|
||||
export {};
|
||||
|
@ -12,8 +12,10 @@
|
||||
media="(prefers-color-scheme: dark)"
|
||||
href="%sveltekit.assets%/icon/logo-dark.svg"
|
||||
/>
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>魚之魷魂 SquidSpirit</title>
|
||||
|
||||
<meta name="description" content="程式、科技、教學、分享" />
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||
@ -30,6 +32,7 @@
|
||||
/>
|
||||
</noscript>
|
||||
<script src="%sveltekit.assets%/js/font-loader.js" defer></script>
|
||||
|
||||
%sveltekit.head%
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover" class="antialiased">
|
||||
|
28
frontend/src/lib/common/framework/ui/GoogleAnalytics.svelte
Normal file
28
frontend/src/lib/common/framework/ui/GoogleAnalytics.svelte
Normal file
@ -0,0 +1,28 @@
|
||||
<script lang="ts">
|
||||
import { Environment } from '$lib/environment';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
onMount(() => {
|
||||
const gaMeasurementId = Environment.GA_MEASUREMENT_ID;
|
||||
|
||||
if (!gaMeasurementId.startsWith('G-')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (document.querySelector(`script[src*="${gaMeasurementId}"]`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const gaScript = document.createElement('script');
|
||||
gaScript.async = true;
|
||||
gaScript.src = `https://www.googletagmanager.com/gtag/js?id=${gaMeasurementId}`;
|
||||
document.head.appendChild(gaScript);
|
||||
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
window.gtag = (command: 'config' | 'set' | 'js' | 'event', ...args: unknown[]) => {
|
||||
window.dataLayer.push(command, ...args);
|
||||
};
|
||||
window.gtag('js', new Date());
|
||||
window.gtag('config', gaMeasurementId);
|
||||
});
|
||||
</script>
|
@ -2,4 +2,5 @@ import { env } from '$env/dynamic/public';
|
||||
|
||||
export abstract class Environment {
|
||||
static readonly API_BASE_URL = env.PUBLIC_API_BASE_URL ?? 'http://localhost:5173/api/';
|
||||
static readonly GA_MEASUREMENT_ID = env.PUBLIC_GA_MEASUREMENT_ID ?? '';
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
<script>
|
||||
<script lang="ts">
|
||||
import Footer from '$lib/common/framework/ui/Footer.svelte';
|
||||
import Navbar from '$lib/common/framework/ui/Navbar.svelte';
|
||||
import GoogleAnalytics from '$lib/common/framework/ui/GoogleAnalytics.svelte';
|
||||
import '../app.css';
|
||||
import '@fortawesome/fontawesome-free/css/all.min.css';
|
||||
</script>
|
||||
|
||||
<GoogleAnalytics />
|
||||
<svelte:head>
|
||||
<meta name="app-version" content={App.__VERSION__} />
|
||||
</svelte:head>
|
||||
|
Loading…
x
Reference in New Issue
Block a user