diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index c1951e5..f934e07 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -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 = '' + + 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 = ( + '\n' + ' \n' + ' ' + ) + html = html.replace(placeholder, snippet) + print('Google Analytics injected:', ga_id) + 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: diff --git a/src/index.html b/src/index.html index 9d66b28..e6ccfcd 100644 --- a/src/index.html +++ b/src/index.html @@ -20,6 +20,7 @@ +