Expand description
§Privatus Analytics for Rust
Prints the cookieless Privatus Analytics script tag, the attributes for custom events and click maps, and page props, for Rust web apps.
The crate only prints HTML for the browser. It never sends events or any other request from your server, sets no cookies, and never reads IP addresses, browser details (user agents) or any visitor id. The core has no dependencies.
use privatus_analytics::{click_attr, event_attrs, props, Config};
let config = Config::new().site_id("pa_YOURSITEID").enabled(true);
assert_eq!(
config.tag().to_html().as_str(),
r#"<script defer src="https://privatusstats.com/js/pa.js" data-site="pa_YOURSITEID"></script>"#
);
assert_eq!(
event_attrs("Report exported", props! { "format" => "csv" }).as_str(),
r#"data-privatus-event="Report exported" data-privatus-prop-format="csv""#
);
assert_eq!(click_attr("pricing.cta").as_str(), r#"data-privatus-click="pricing.cta""#);When Config::enabled is not set, the tag prints only in release builds
(cfg!(not(debug_assertions))).
§Cargo features
axum:axum::PrivatusLayer, a tower layer that adds the tag to HTML pages.actix:actix::PrivatusMiddleware, the same for actix-web.askama: askama does not escapeSafeHtmlandTag.maud:SafeHtmlandTagimplementmaud::Render, plusmaud::element.
See the README and the Privatus Analytics docs.
Modules§
- actix
actix - An actix-web middleware that adds the tag to the
<head>of HTML pages. - askama
askama - askama support:
SafeHtmlandTagare not escaped by askama. - axum
axum - A tower layer for axum that adds the tag to the
<head>of HTML pages. - maud
maud - maud support:
SafeHtmlandTagimplementmaud::Render.
Macros§
Structs§
- Config
- Settings for the tracking script.
- CspNonce
- A Content Security Policy nonce for the current request.
- Props
- An ordered list of props: key and value pairs.
- Safe
Html - HTML that is already escaped and safe to print as it is.
- Tag
- The script tag for one page. Get one with
Config::tag. - Unknown
SpaMode - The error for an unknown single-page app mode.
Enums§
- Spa
- How the tracker follows page changes in a single-page app (the
data-spaattribute). - Value
- One prop value: text, a number, true or false, or a list.
Constants§
- DEFAULT_
SCRIPT_ HOST - Where
/js/pa.jsis served when nothing else is set. - MARKER
- Text that shows a page already has the tracker.
- MAX_
CLICK_ NAME - Click map names may be at most this many characters.
- MAX_
ITEMS - Lists in page props keep at most this many items.
- MAX_KEY
- Prop keys are cut to this many characters.
- MAX_
NAME - Event names are cut to this many characters.
- MAX_
PROPS - At most this many props are kept.
- MAX_
VALUE - Prop values are cut to this many characters.
- PROPS_
FUNCTION - The name of the page props function the tracker calls (
data-props). - QUEUE_
STUB - The tracker’s official queue stub. It keeps
privatus.trackand the other calls in a list untilpa.jshas loaded.
Functions§
- attrs_
to_ html - Turns
(name, value)pairs into HTML attributes, with every value escaped. - click_
attr - The click map name attribute, as safe HTML.
- click_
data - The click map name as a
(name, value)pair, or an empty list when the name is not valid. - escape_
html - Escapes text for an HTML attribute value or element body.
- event_
attrs - The attributes for a custom event, as safe HTML to put inside a tag you write.
- event_
data - The same attributes as
event_attrs, as(name, value)pairs that are not escaped yet. Handy for template engines and builders that set attributes one by one and escape them for you. - inject_
tag - Puts
tagjust before the first</head>(any case) inhtml. - normalize_
key - Cleans a prop key: lowercase, anything outside
a-z 0-9 _becomes_, cut to 64 characters. - page_
props_ json - Turns page props into a JSON object, or
Nonewhen there is nothing to send. - tag
- The script tag for these settings. The same as
Config::tag. - valid_
click - Whether a click map name is valid: letters, digits and
_ . : -, from 1 to 100 characters.