Skip to content

feat(demo): inject GoatCounter tracker at runtime for public demo builds - #152

Merged
gnacho merged 1 commit into
mainfrom
feat/103-gc-runtime
Oct 13, 2026
Merged

gnacho merged 1 commit into
mainfrom
feat/103-gc-runtime

Conversation

@gnacho

@gnacho gnacho commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Fixes #103 with a different approach than the original branch.

The build-time variant (VITE_GC_COUNT baking the snippet into the frontend bundle) has a flaw: the public demo runs official release assets built by CI without that env, so the tracker silently disappeared from the demo when it moved to official assets (v2.9.23). Confirmed today: the live demo serves no tracker at all.

This reworks it as runtime injection:

  • gcIndexOverride (main.go) computes the injected index.html once at startup, only when DEMO=1 and GC_ORIGIN are set; spaHandler serves that override for /, /index.html and the SPA fallback. sw.js and assets are untouched.
  • security.cspValue adds the origin to script-src and connect-src only when GC_ORIGIN is defined (ported from the original branch).
  • Regular installs define neither variable: no injection, no CSP change, no calls home. Hits keep the /demo path prefix on the shared GoatCounter site.

Verification: unit tests for the injection (env gates, trailing slash, HTML without </head>) and for the CSP gate; deployed the preview (2.9.99-103gc) to the public demo and verified end to end with a real browser: count.js loads and the beacon goes out with p=%2Fdemo%2F.

The build-time approach (VITE_GC_COUNT baking the tracker into the
frontend bundle) stopped working the moment the demo started running
official release assets: CI builds do not set that env, so the tracker
silently disappeared from the demo back in v2.9.23.

The server now injects the snippet into the served index.html when
DEMO=1 and GC_ORIGIN are set (computed once at startup), and the CSP
gains the origin in script-src and connect-src only when GC_ORIGIN is
defined. Regular installs define neither and never call home. Hits keep
the /demo path prefix on the shared GoatCounter site.

Closes #103
@gnacho
gnacho merged commit c4822d2 into main Oct 13, 2026
3 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.

Public demo: register visits in GoatCounter (demo build only)

1 participant