Skip to main content

privatus_analytics/
lib.rs

1//! # Privatus Analytics for Rust
2//!
3//! Prints the cookieless [Privatus Analytics](https://privatusanalytics.com)
4//! script tag, the attributes for custom events and click maps, and page
5//! props, for Rust web apps.
6//!
7//! The crate only prints HTML for the browser. It never sends events or any
8//! other request from your server, sets no cookies, and never reads IP
9//! addresses, browser details (user agents) or any visitor id. The core has
10//! no dependencies.
11//!
12//! ```
13//! use privatus_analytics::{click_attr, event_attrs, props, Config};
14//!
15//! let config = Config::new().site_id("pa_YOURSITEID").enabled(true);
16//!
17//! assert_eq!(
18//!     config.tag().to_html().as_str(),
19//!     r#"<script defer src="https://privatusstats.com/js/pa.js" data-site="pa_YOURSITEID"></script>"#
20//! );
21//! assert_eq!(
22//!     event_attrs("Report exported", props! { "format" => "csv" }).as_str(),
23//!     r#"data-privatus-event="Report exported" data-privatus-prop-format="csv""#
24//! );
25//! assert_eq!(click_attr("pricing.cta").as_str(), r#"data-privatus-click="pricing.cta""#);
26//! ```
27//!
28//! When [`Config::enabled`] is not set, the tag prints only in release builds
29//! (`cfg!(not(debug_assertions))`).
30//!
31//! ## Cargo features
32//!
33//! * `axum`: [`axum::PrivatusLayer`], a tower layer that adds the tag to
34//!   HTML pages.
35//! * `actix`: [`actix::PrivatusMiddleware`], the same for actix-web.
36//! * `askama`: askama does not escape [`SafeHtml`] and [`Tag`].
37//! * `maud`: [`SafeHtml`] and [`Tag`] implement `maud::Render`, plus
38//!   [`maud::element`].
39//!
40//! See the [README](https://github.com/Privatus-Analytics/rust) and the
41//! [Privatus Analytics docs](https://privatusanalytics.com/docs).
42
43#![cfg_attr(docsrs, feature(doc_cfg))]
44#![forbid(unsafe_code)]
45#![warn(missing_docs, missing_debug_implementations)]
46
47mod attrs;
48mod config;
49mod html;
50mod page_props;
51mod tag;
52mod value;
53
54pub use attrs::{
55    attrs_to_html, click_attr, click_data, event_attrs, event_data, normalize_key, valid_click,
56    MAX_CLICK_NAME, MAX_KEY, MAX_NAME, MAX_PROPS, MAX_VALUE,
57};
58pub use config::{Config, CspNonce, Spa, UnknownSpaMode, DEFAULT_SCRIPT_HOST};
59pub use html::{escape_html, SafeHtml};
60pub use page_props::{page_props_json, MAX_ITEMS};
61pub use tag::{inject_tag, Tag, MARKER, PROPS_FUNCTION, QUEUE_STUB};
62pub use value::{Props, Value};
63
64/// The script tag for these settings. The same as [`Config::tag`].
65pub fn tag(config: &Config) -> Tag<'_> {
66    config.tag()
67}
68
69#[cfg(feature = "actix")]
70#[cfg_attr(docsrs, doc(cfg(feature = "actix")))]
71pub mod actix;
72#[cfg(feature = "askama")]
73#[cfg_attr(docsrs, doc(cfg(feature = "askama")))]
74pub mod askama;
75#[cfg(feature = "axum")]
76#[cfg_attr(docsrs, doc(cfg(feature = "axum")))]
77pub mod axum;
78#[cfg(feature = "maud")]
79#[cfg_attr(docsrs, doc(cfg(feature = "maud")))]
80pub mod maud;