matomo-logger
Global logger for WASM apps that forwards Rust log records to both the browser console and Matomo (_paq). Provides a one-line initialization and an SPA-friendly track_page_view helper. Optional worker bridge API lets you forward logs from Web Workers to the main thread, where they are pushed into Matomo.
Features
- Single global logger (implements
log::Log) - Console + Matomo multiplexing with independent level thresholds
- Safe Matomo snippet injection when
_paqis absent (configurable) - SPA navigation helper:
track_page_view(title, url) - User attribution:
set_user_id(user_id)(Matomo User ID) - Web Worker bridge (optional feature) to forward logs to main thread
- Small, dependency-light; optimized for Yew and other WASM front-ends
Quick start
use ;
// Call as early as possible in your `main()`
let _ = init;
// Later, on SPA route change:
track_page_view;
// From anywhere in your code:
warn!;
The logger writes to the browser console at console_level and forwards records to Matomo at matomo_level. If Matomo is blocked or not available, logging continues to console without errors.
Configuration
inject_snippet: If true and_paqis missing, the crate injects the Matomo snippet (setTrackerUrl,setSiteId,enableLinkTracking, andmatomo.js). If your HTML already includes the Matomo snippet, you can keep it; the crate detects_paqand won’t inject again.matomo_level: strongly recommendWarnor higher to avoid analytics noise. UseInfoonly for short experiments.
SPA page views
Use the built-in helper:
track_page_view;
It sends setCustomUrl, setDocumentTitle, and trackPageView via _paq if available. Calls are safe even when Matomo is blocked.
Web Worker logs (optional)
Workers do not have access to window._paq. Use the bridge API to forward worker logs to the main thread:
// In worker (requires feature = "worker")
use worker;
let send = /* a js_sys::Function that posts messages to main thread */;
init_with_bridge?;
// In the main thread, handle worker messages and push to Matomo
// (Implement a tiny router that converts {type:"log", level, target, message} to
// _paq.push(['trackEvent', 'RustLog', level, `${target} — ${message}`, value]))
Best practices
- Keep
matomo_levelatWarnor above in production. - Consider removing the Matomo snippet from HTML to avoid double page views if you rely on
MatomoLogger::init. - Be mindful of PII: avoid logging sensitive data; messages are truncated to
max_event_len.
License
Dual-licensed under either of:
- Apache License, Version 2.0 (
LICENSE-APACHEor http://www.apache.org/licenses/LICENSE-2.0) - MIT license (
LICENSE-MITor http://opensource.org/licenses/MIT)
at your option.
Authors
- Dario Lencina dario@securityunion.dev
Changelog
See the repository’s main CHANGELOG for release notes.