Skip to content

Commit 6eab08c

Browse files
feat: dynamic Google Analytics integration
1 parent 7c74553 commit 6eab08c

File tree

8 files changed

+51
-5
lines changed

8 files changed

+51
-5
lines changed

.env.dev

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
API_BASE_URL=https://dev.core-system.sdc.nycu.club/api
22

3+
# ===== Vite build-time variables (baked into frontend bundle) =====
4+
VITE_GA_ID=G-TZV3X2G3BH
5+
36
# ===== Core System Frontend Gateway (Fastify) =====
47

58
# Port Fastify listens on inside the container/host

.env.stage

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
API_BASE_URL=https://stage.core-system.sdc.nycu.club/api
2+
3+
# ===== Vite build-time variables (baked into frontend bundle) =====
4+
VITE_GA_ID=G-TZV3X2G3BH
5+
6+
# ===== Core System Frontend Gateway (Fastify) =====
7+
8+
# Port Fastify listens on inside the container/host
9+
PORT=80
10+
11+
# Upstream Go backend origin for /api proxy and for fetching SEO metadata
12+
BACKEND_ORIGIN=http://172.18.0.1:8080
13+
14+
# Optional: override Host header when calling BACKEND_ORIGIN
15+
BACKEND_HOST_HEADER=stage.core-system.sdc.nycu.club
16+
17+
# Optional: public site origin used for canonical/og:url
18+
PUBLIC_ORIGIN=https://stage.core-system.sdc.nycu.club
19+
20+
# Optional: cache control for HTML responses
21+
HTML_CACHE_CONTROL=no-store

.github/workflows/dev.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ jobs:
110110
nycusdc/core-system-frontend:dev
111111
nycusdc/core-system-frontend:${{ github.sha }}
112112
context: .
113+
build-args: |
114+
BUILD_MODE=dev
113115
114116
- name: Build and Push Docker image
115117
uses: docker/build-push-action@v6
@@ -122,6 +124,8 @@ jobs:
122124
nycusdc/core-system-frontend:stage
123125
nycusdc/core-system-frontend:${{ github.sha }}
124126
context: .
127+
build-args: |
128+
BUILD_MODE=stage
125129
126130
Deploy-Dev:
127131
if: ${{ contains(github.ref, 'refs/heads/main') }}

.github/workflows/snapshot.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ jobs:
108108
nycusdc/core-system-frontend:pr-${{ github.event.number }}
109109
nycusdc/core-system-frontend:${{ github.sha }}
110110
context: .
111+
build-args: |
112+
BUILD_MODE=dev
111113
112114
Deploy:
113115
needs: Push-Image

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
FROM node:23-alpine AS builder
22
WORKDIR /app
33

4+
ARG BUILD_MODE=dev
5+
46
RUN npm i -g pnpm
57
COPY package.json pnpm-lock.yaml ./
68
RUN pnpm install --frozen-lockfile
79

810
COPY . .
9-
RUN pnpm run build --mode=dev
11+
RUN pnpm run build --mode=${BUILD_MODE}
1012

1113
FROM node:23-alpine
1214
WORKDIR /app

admin.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<!-- SEO 由 gateway 動態注入 -->
77
<!-- Google tag (gtag.js) -->
8-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-TZV3X2G3BH"></script>
8+
<script async src="https://www.googletagmanager.com/gtag/js?id=%VITE_GA_ID%"></script>
99
<script>
1010
window.dataLayer = window.dataLayer || [];
1111
function gtag() {
1212
dataLayer.push(arguments);
1313
}
1414
gtag("js", new Date());
1515

16-
gtag("config", "G-TZV3X2G3BH");
16+
gtag("config", "%VITE_GA_ID%");
1717
</script>
1818
</head>
1919
<body style="background-color: #0e0d11">

forms.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<!-- Google tag (gtag.js) -->
7-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-TZV3X2G3BH"></script>
7+
<script async src="https://www.googletagmanager.com/gtag/js?id=%VITE_GA_ID%"></script>
88
<script>
99
window.dataLayer = window.dataLayer || [];
1010
function gtag() {
1111
dataLayer.push(arguments);
1212
}
1313
gtag("js", new Date());
1414

15-
gtag("config", "G-TZV3X2G3BH");
15+
gtag("config", "%VITE_GA_ID%");
1616
</script>
1717

1818
<!-- SEO 由 gateway 動態注入 -->

vite.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ const mpaFallback: Plugin = {
2626

2727
export default defineConfig(({ mode }) => {
2828
const env = loadEnv(mode, process.cwd(), "VITE_");
29+
const gaId = env.VITE_GA_ID ?? "";
30+
31+
const injectGA: Plugin = {
32+
name: "inject-ga",
33+
transformIndexHtml(html) {
34+
if (!gaId) {
35+
// Remove the GA block entirely when no ID is provided
36+
return html.replace(/<!--\s*Google tag[\s\S]*?<\/script>\s*/g, "");
37+
}
38+
return html.replaceAll("%VITE_GA_ID%", gaId);
39+
}
40+
};
41+
2942
return {
3043
build: {
3144
chunkSizeWarningLimit: 1000,
@@ -53,6 +66,7 @@ export default defineConfig(({ mode }) => {
5366
plugins: [
5467
react(),
5568
mpaFallback,
69+
injectGA,
5670

5771
visualizer({ open: true, filename: "dist/stats.html", gzipSize: true, brotliSize: true }),
5872

0 commit comments

Comments
 (0)