Skip to content

Add Google Analytics injection to deployment workflow - #1

Merged
PBNZ merged 2 commits into
mainfrom
claude/add-google-analytics-7soFq
Mar 31, 2026
Merged

Add Google Analytics injection to deployment workflow#1
PBNZ merged 2 commits into
mainfrom
claude/add-google-analytics-7soFq

Conversation

@PBNZ

@PBNZ PBNZ commented Mar 31, 2026

Copy link
Copy Markdown
Owner

Summary

This PR adds automated Google Analytics integration to the deployment pipeline. The deployment workflow now injects a GA4 tracking script into the HTML during the build process, with validation and conditional injection based on configuration.

Key Changes

  • Workflow enhancement: Added a new "Inject Google Analytics" step in the deploy workflow that runs before artifact upload
  • HTML placeholder: Added a <!-- GOOGLE_ANALYTICS_PLACEHOLDER --> comment in src/index.html where the GA script will be injected
  • Dynamic script injection: Implemented Python script that:
    • Reads the GA_MEASUREMENT_ID from GitHub variables
    • Validates the ID format (must match G-[A-Z0-9]+ pattern for GA4)
    • Injects the official Google tag manager script and gtag configuration if valid
    • Removes the placeholder without injection if ID is invalid or not set
    • Provides informative logging for each scenario

Implementation Details

  • The GA script injection happens at build time, keeping the source HTML clean with just a placeholder
  • Validation ensures only properly formatted GA4 IDs are used, preventing silent failures
  • The solution gracefully handles missing or invalid configuration with appropriate warnings
  • Uses inline Python for simplicity and to avoid additional dependencies

https://claude.ai/code/session_01YRP3nqtybUtdG2Jqs5tggo

Adds a placeholder comment to src/index.html that the deploy workflow
replaces with the GA4 gtag snippet at build time using the
GA_MEASUREMENT_ID repository variable. If the variable is unset or
fails format validation (^G-[A-Z0-9]+$), the placeholder is removed
cleanly so the site deploys without errors or leftover comments.

https://claude.ai/code/session_01YRP3nqtybUtdG2Jqs5tggo
Copilot AI review requested due to automatic review settings March 31, 2026 08:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds build-time Google Analytics (GA4) injection to the GitHub Pages deployment workflow by inserting the GA snippet into src/index.html at a dedicated placeholder during deployment.

Changes:

  • Added a <!-- GOOGLE_ANALYTICS_PLACEHOLDER --> marker in src/index.html to define the injection point.
  • Added a deployment workflow step that validates GA_MEASUREMENT_ID and injects (or removes) the placeholder accordingly before uploading the Pages artifact.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/index.html Adds a placeholder comment in <head> where GA will be injected at deploy time.
.github/workflows/deploy.yml Adds an inline Python step to validate and inject GA4 tracking code before artifact upload.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +100 to +103
' </script>'
)
html = html.replace(placeholder, snippet)
print('Google Analytics injected:', ga_id)

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.
Comment thread .github/workflows/deploy.yml Outdated
Comment on lines +88 to +89
with open('src/index.html', 'r') as f:
html = f.read()

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.

index.html contains non-ASCII characters (e.g. em dashes). Reading/writing the file without an explicit encoding relies on the runner locale; to make this deterministic, open the file with encoding="utf-8" for both the read and write.

Copilot uses AI. Check for mistakes.
Comment thread .github/workflows/deploy.yml Outdated
else:
html = html.replace(placeholder, '')
if ga_id:
print('WARNING: GA_MEASUREMENT_ID "' + ga_id + '" is not a valid GA4 ID (expected G-XXXXXXXXXX). Skipping.')

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 warning text says the expected format is G-XXXXXXXXXX, but the validation regex accepts any length (^G-[A-Z0-9]+$). Either tighten the regex to match the format described in the message or adjust the message to reflect the actual validation rule so users aren’t misled.

Copilot uses AI. Check for mistakes.
Comment thread .github/workflows/deploy.yml Outdated
html = html.replace(placeholder, snippet)
print('Google Analytics injected:', ga_id)
else:
html = html.replace(placeholder, '')

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.

When the GA ID is missing/invalid, the placeholder is replaced with an empty string, which leaves a whitespace-only line in the <head> (because the indentation remains). Consider replacing the whole placeholder line (including leading whitespace and trailing newline) to avoid leaving stray blank/indented lines in the output HTML.

Suggested change
html = html.replace(placeholder, '')
# Remove the entire line containing the placeholder (including indentation and newline)
pattern = r'^[ \t]*' + re.escape(placeholder) + r'[ \t]*\r?\n?'
new_html, count = re.subn(pattern, '', html, flags=re.MULTILINE)
if count > 0:
html = new_html
else:
# Fallback: if the placeholder was not on its own line, just remove the placeholder text
html = html.replace(placeholder, '')

Copilot uses AI. Check for mistakes.
- Open index.html with explicit encoding='utf-8' (read + write) to
  handle non-ASCII characters deterministically across runner locales
- Verify placeholder occurs exactly once before replacing; exit 1 if
  count != 1 so a missing/duplicate placeholder surfaces as a build
  failure rather than a silent no-op
- Use re.sub with MULTILINE to strip the entire placeholder line
  (including indentation and newline) when skipping injection, leaving
  no blank or whitespace-only lines in the deployed HTML
- Fix warning message to accurately describe the actual regex rule
  (any length G-[A-Z0-9]+) rather than implying a fixed 10-char format

https://claude.ai/code/session_01YRP3nqtybUtdG2Jqs5tggo
@PBNZ
PBNZ merged commit e383d71 into main Mar 31, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants