Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,47 @@ jobs:
- uses: actions/checkout@v4
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Inject Google Analytics
env:
GA_ID: ${{ vars.GA_MEASUREMENT_ID }}
run: |
python3 - << 'EOF'
import os, re, sys

ga_id = os.environ.get('GA_ID', '').strip()
placeholder = '<!-- GOOGLE_ANALYTICS_PLACEHOLDER -->'

with open('src/index.html', 'r', encoding='utf-8') as f:
html = f.read()

if re.match(r'^G-[A-Z0-9]+$', ga_id):
count = html.count(placeholder)
if count != 1:
print(f'ERROR: Expected exactly 1 placeholder, found {count}. Cannot inject Google Analytics.')
sys.exit(1)
snippet = (
'<!-- Google tag (gtag.js) -->\n'
' <script async src="https://www.googletagmanager.com/gtag/js?id=' + ga_id + '"></script>\n'
' <script>\n'
' window.dataLayer = window.dataLayer || [];\n'
' function gtag(){dataLayer.push(arguments);}\n'
' gtag("js", new Date());\n'
' gtag("config", "' + ga_id + '");\n'
' </script>'
)
html = html.replace(placeholder, snippet)
print('Google Analytics injected:', ga_id)
Comment on lines +104 to +107

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script does not verify that the placeholder replacement actually happened. If src/index.html no longer contains the placeholder (or contains it multiple times), html.replace(...) will be a no-op (or replace multiple locations) but the step may still print that analytics was injected. Consider asserting the placeholder exists exactly once and failing the step (or at least warning) when a valid GA ID is provided but the placeholder is missing/ambiguous.

Copilot uses AI. Check for mistakes.
else:
html = re.sub(r'^[ \t]*' + re.escape(placeholder) + r'[ \t]*\r?\n?', '', html, flags=re.MULTILINE)
if ga_id:
print('WARNING: GA_MEASUREMENT_ID "' + ga_id + '" is not a valid GA4 ID (expected format: G- followed by uppercase letters/digits, e.g. G-HHYVHP2GN1). Skipping.')
else:
print('GA_MEASUREMENT_ID not set — skipping analytics injection.')

with open('src/index.html', 'w', encoding='utf-8') as f:
f.write(html)
EOF

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
Expand Down
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<link rel="icon" type="image/svg+xml"
href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='12' fill='%230a1628'/%3E%3Cpath d='M32 44a12 12 0 1 1 0-24' fill='none' stroke='%2300b8a9' stroke-width='4' stroke-linecap='round'/%3E%3Cpath d='M32 38a6 6 0 1 1 0-12' fill='none' stroke='%2300b8a9' stroke-width='4' stroke-linecap='round'/%3E%3Ccircle cx='32' cy='32' r='2.5' fill='%2300b8a9'/%3E%3C/svg%3E">
<link rel="stylesheet" href="css/styles.css">
<!-- GOOGLE_ANALYTICS_PLACEHOLDER -->
</head>

<body>
Expand Down