feat: add google analytics setup
All checks were successful
Deploy / build-and-deploy (push) Successful in 59s

This commit is contained in:
juancwu 2026-02-10 16:52:35 +00:00
commit e7fdea5375
3 changed files with 22 additions and 0 deletions

View file

@ -21,3 +21,5 @@ MAILER_PASSWORD=
MAILER_EMAIL_FROM=
SUPPORT_EMAIL=
GOOGLE_MEASURING_ID=

View file

@ -34,6 +34,8 @@ type Config struct {
SupportEmail string
GoogleMeasuringID string
Version string
}
@ -68,6 +70,8 @@ func Load(version string) *Config {
SupportEmail: envString("SUPPORT_EMAIL", ""),
GoogleMeasuringID: envString("GOOGLE_MEASURING_ID", ""),
Version: version,
}
@ -91,6 +95,7 @@ func (c *Config) Sanitized() *Config {
MailerEmailFrom: c.MailerEmailFrom,
SupportEmail: c.SupportEmail,
GoogleMeasuringID: c.GoogleMeasuringID,
Version: c.Version,
}

View file

@ -56,6 +56,10 @@ templ Base(props ...SEOProps) {
@smoothScrollScript()
// HTMX CSRF configuration
@htmxCSRFScript()
// Google Analytics
if cfg := ctxkeys.Config(ctx); cfg != nil && cfg.GoogleMeasuringID != "" {
@googleAnalyticsScript(cfg.GoogleMeasuringID)
}
</head>
<body class="min-h-screen">
{ children... }
@ -113,3 +117,14 @@ templ smoothScrollScript() {
templ htmxCSRFScript() {
<script src="/assets/js/htmx-csrf.js"></script>
}
templ googleAnalyticsScript(id string) {
<script async src={ "https://www.googletagmanager.com/gtag/js?id=" + id }></script>
<script type="text/javascript">
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
window.gtag = gtag;
gtag('js', new Date());
gtag('config', {{ id }});
</script>
}