Skip to main content

Crate privatus_analytics

Crate privatus_analytics 

Source
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

See the README and the Privatus Analytics docs.

Modules§

actixactix
An actix-web middleware that adds the tag to the <head> of HTML pages.
askamaaskama
askama support: SafeHtml and Tag are not escaped by askama.
axumaxum
A tower layer for axum that adds the tag to the <head> of HTML pages.
maudmaud
maud support: SafeHtml and Tag implement maud::Render.

Macros§

props
Builds Props from key => value pairs.

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.
SafeHtml
HTML that is already escaped and safe to print as it is.
Tag
The script tag for one page. Get one with Config::tag.
UnknownSpaMode
The error for an unknown single-page app mode.

Enums§

Spa
How the tracker follows page changes in a single-page app (the data-spa attribute).
Value
One prop value: text, a number, true or false, or a list.

Constants§

DEFAULT_SCRIPT_HOST
Where /js/pa.js is 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.track and the other calls in a list until pa.js has 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 tag just before the first </head> (any case) in html.
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 None when 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.