Skip to content

Commit

Permalink
feat(webui): allow for google analytics or CF web analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
natelandau committed Dec 13, 2024
1 parent e23ec72 commit ce4876d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@

# Sets the file to write access logs to. Note, this is the directory used within the Docker container
# VALENTINA_WEBUI_ACCESS_LOG=/valentina/access.log

# Enables Cloudflare Web Analytics by setting this to your Cloudflare Web Analytics token
# VALENTINA_CLOUDFLARE_WEB_ANALYTICS_TOKEN=

# Enables Google Analytics by setting this to your Google Analytics ID
# VALENTINA_GOOGLE_ANALYTICS_ID=
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ Settings for Valentina are controlled by environment variables. The following is
| VALENTINA_DISCORD_OAUTH_CLIENT_ID | | Sets the ID for the Discord OAuth. This is required to run the web UI. |
| VALENTINA_WEBUI_BEHIND_REVERSE_PROXY | `false` | Set to `true` if the web UI is behind a reverse proxy. |
| VALENTINA_WEBUI_ACCESS_LOG | `/valentina/access.log` | Sets the file to write access logs to.<br />Note, this is the directory used within the Docker container |
| VALENTINA_CLOUDFLARE_WEB_ANALYTICS_TOKEN | | Optional: Enable Cloudflare Web Analytics by setting this to your Cloudflare Web Analytics token |
| VALENTINA_GOOGLE_ANALYTICS_ID | | Optional: Enable Google Analytics by setting this to your Google Analytics ID |

---

Expand Down
2 changes: 2 additions & 0 deletions src/valentina/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class ValentinaConfig(BaseConfig): # type: ignore [misc]
redis_addr: str = "127.0.0.1:6379"
webui_secret_key: str = ""
webui_behind_reverse_proxy: ENV_BOOLEAN = False
cloudflare_web_analytics_token: str = ""
google_analytics_id: str = ""

CONFIG_SOURCES: ClassVar[ConfigSources | None] = [
EnvSource(prefix="VALENTINA_", file=PROJECT_ROOT_PATH / ".env", allow_all=True),
Expand Down
2 changes: 2 additions & 0 deletions src/valentina/webui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Config:
DISCORD_CLIENT_SECRET = ValentinaConfig().discord_oauth_secret
DISCORD_REDIRECT_URI = f"{ValentinaConfig().webui_base_url}/callback"
DISCORD_BOT_TOKEN = ValentinaConfig().discord_token
CLOUDFLARE_WEB_ANALYTICS_TOKEN = ValentinaConfig().cloudflare_web_analytics_token
GOOGLE_ANALYTICS_ID = ValentinaConfig().google_analytics_id


class Production(Config):
Expand Down
20 changes: 20 additions & 0 deletions src/valentina/webui/shared/PageLayout.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@
<link href="{{ url_for('static', filename='valentina.css') }}"
rel="stylesheet">
</head>
{% if config.GOOGLE_ANALYTICS_ID %}
<!-- Google tag (gtag.js) -->
<script async
src="https://www.googletagmanager.com/gtag/js?id={{ config.GOOGLE_ANALYTICS_ID }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', '{{ config.GOOGLE_ANALYTICS_ID }}');
</script>
<!-- End Google tag (gtag.js) -->
{% endif %}
<body class="d-flex flex-column min-vh-100">
{# include nav bar only when user is logged in #}
{% if session["USER_ID"] is defined and session["GUILD_ID"] is defined %}<NavBar />{% endif %}
Expand Down Expand Up @@ -139,5 +152,12 @@
var toastE = new bootstrap.Toast(toast);
toastE.show();
</script>
{% if config.CLOUDFLARE_WEB_ANALYTICS_TOKEN %}
<!-- Cloudflare Web Analytics -->
<script defer
src='https://static.cloudflareinsights.com/beacon.min.js'
data-cf-beacon='{"token": "{{ config.CLOUDFLARE_WEB_ANALYTICS_TOKEN }}"}'></script>
<!-- End Cloudflare Web Analytics -->
{% endif %}
</body>
</html>

0 comments on commit ce4876d

Please sign in to comment.