Skip to content

Add Google Analytics support - #24

Open
navin-mohan wants to merge 1 commit into
ebkalderon:mainfrom
navin-mohan:add-ga-support
Open

navin-mohan wants to merge 1 commit into
ebkalderon:mainfrom
navin-mohan:add-ga-support

Conversation

@navin-mohan

Copy link
Copy Markdown

Adds a new extra config google_analytics_id which when set will include Google Analytics tracking script.

[extra]
google_analytics_id = "G-XXXXXXXXXX"

@ebkalderon ebkalderon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks a lot for your submission! My apologies for the delayed code review; life has been pretty busy lately. 😅

Comment on lines +1 to +2
<script async src="https://www.googletagmanager.com/gtag/js?id={{ config.extra.google_analytics_id }}"></script>
<script>

@ebkalderon ebkalderon Jul 10, 2026 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Unfortunately, these <script> tags will won't work for users with CSP enabled without some modifications. I think we could assign a SHA-256 hash/nonce to the inline <script> tag if config.extra.content_security_policy.enable == true, and update templates/partials/content_security_policy.html accordingly to allow scripts with that same hash/nonce and scripts from the googletagmanager.com domain to run if the Google Analytics ID is enabled and set. Any thoughts?

Comment thread templates/base.html
{%- if config.extra.fediverse_creator %}
<meta name="fediverse:creator" content="{{ config.extra.fediverse_creator }}">
{%- endif -%}
{%- if config.extra.google_analytics_id -%}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

If we're going to integrate first-party support for web analytics into this Zola theme, I think it would be wise to ensure the public API is decoupled from any specific service. IMO, the way Tabi approaches this is pretty ideal (documentation).

[extra.analytics]
service = "google-analytics"
id = "G-XXXXXXXXXX"

There's no need to refactor the Tera templates to be Google Analytics agnostic as part of this PR, if you don't want to (that work can always be done later, if we eventually add support for a 2nd or 3rd analytics provider someday). But at least making the theme.toml API match the above format would be wonderful. ❤️

And if you can, please consider updating the theme.toml with some documentation as well. That would be most appreciated!

This branch has not been deployed

No deployments
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.

2 participants