commit 660ff34e87842a89b6cef22ef347da102ae5da82
parent 16d58b672e15189b02e849f78cb7058b062601f1
Author: Joris Hartog <jorishartog@hotmail.com>
Date: Thu, 3 Sep 2026 09:51:57 +0200
Add Cloudflare analytics to website build
Diffstat:
3 files changed, 58 insertions(+), 1 deletion(-)
diff --git a/.dockerignore b/.dockerignore
@@ -17,6 +17,7 @@
!.git/**
!www/**
!downloads/**
+!scripts/inject_cloudflare_analytics.py
!scripts/postprocess_stagit_site.py
!src-tauri/icons/32x32.png
!nginx.conf
diff --git a/Dockerfile b/Dockerfile
@@ -67,7 +67,8 @@ RUN set -eux; \
cp /site/downloads.html /site/downloads/index.html; \
sed -i "s|\${IUNA_VERSION}|${version}|g" /site/downloads/index.html; \
printf '{"tag":"v%s","version":"%s","url":"https://getiuna.org/downloads/"}\n' "$version" "$version" > /site/downloads/latest.json; \
- rm -f /site/downloads.html
+ rm -f /site/downloads.html; \
+ python3 /src/iuna-work/scripts/inject_cloudflare_analytics.py /site
########################################################################
# 3. Ship it - plain nginx, static files only.
diff --git a/scripts/inject_cloudflare_analytics.py b/scripts/inject_cloudflare_analytics.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+import re
+import sys
+from pathlib import Path
+
+
+ANALYTICS = """<!-- Cloudflare Web Analytics --><script type='module' src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "a0d6dbbe9b7846f59f24fd2052322cda"}'></script><!-- End Cloudflare Web Analytics -->"""
+BODY_CLOSE_RE = re.compile(r"</body\s*>", re.IGNORECASE)
+
+
+def inject_analytics(html: str) -> str:
+ if ANALYTICS in html:
+ return html
+
+ matches = list(BODY_CLOSE_RE.finditer(html))
+ if not matches:
+ raise ValueError("HTML document has no closing body tag")
+
+ closing_body = matches[-1]
+ return html[: closing_body.start()] + ANALYTICS + "\n" + html[closing_body.start() :]
+
+
+def main() -> int:
+ if len(sys.argv) != 2:
+ print(f"Usage: {sys.argv[0]} <site-root>", file=sys.stderr)
+ return 2
+
+ site_root = Path(sys.argv[1])
+ if not site_root.is_dir():
+ print(f"error: site root does not exist: {site_root}", file=sys.stderr)
+ return 1
+
+ html_paths = sorted(site_root.rglob("*.html"))
+ if not html_paths:
+ print(f"error: no HTML pages found below {site_root}", file=sys.stderr)
+ return 1
+
+ injected = 0
+ for path in html_paths:
+ html = path.read_text(encoding="utf-8")
+ updated_html = inject_analytics(html)
+ if updated_html == html:
+ continue
+ path.write_text(updated_html, encoding="utf-8")
+ injected += 1
+
+ print(
+ f"Cloudflare Web Analytics present on {len(html_paths)} HTML pages "
+ f"({injected} injected)"
+ )
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())