Adds the cookieless Privatus Analytics tracking script to Phoenix and Plug apps.
The package only prints HTML: the browser script tag and HTML attributes. It never sends events from your server, sets no cookies and never reads IP addresses or user agents.
The tag
PrivatusAnalytics.Components.tag/1for HEEx layouts.tag/1(safe HTML) andtag_string/1(plain string) for EEx or anything else.PrivatusAnalytics.Plugadds the tag before</head>for you.
Product analytics helpers
event_attrs/2sends an event when an element is clicked (or a form is submitted).click_attrs/1names an element for click maps.put_props/2and the:propsoption set page properties.props_json/1gives page properties as JSON, for a LiveView hook.
See PrivatusAnalytics.Config for every setting and how the environment is chosen.
Summary
Types
Attributes as {name, value} pairs, ready for {...} in HEEx.
Options for the tag. Any setting from PrivatusAnalytics.Config plus :nonce, :props, :queue_stub and :conn.
Event or page properties, as a map or keyword list.
Functions
The attribute that names an element for click maps.
Like click_attrs/1, as safe HTML for EEx.
Like click_attrs/1, as a plain escaped string.
Whether the tag prints for these options (site id set and enabled).
Attributes that send an event when the element is clicked. On a <form>
the event is sent when the form is submitted.
Like event_attrs/2, as safe HTML for EEx: <button <%= event_attrs_html("Export CSV") %>>.
Like event_attrs/2, as a plain escaped string of attributes.
Page properties as a JSON object string, cleaned the same way as the tag does.
Handy for a LiveView hook: data-privatus-props={PrivatusAnalytics.props_json(%{plan: @plan})}.
Adds page properties for this request. They are sent with every pageview and event on the page. Use groups (plan, role), never anything that names a person.
The official queue stub script, so privatus.track calls made before the
tracker loads are kept. Also printed by the tag with queue_stub: true.
The script tag as safe HTML ({:safe, iodata}), ready for EEx and HEEx.
The script tag as a plain string. Empty when nothing should print.
Types
Attributes as {name, value} pairs, ready for {...} in HEEx.
Options for the tag. Any setting from PrivatusAnalytics.Config plus :nonce, :props, :queue_stub and :conn.
Event or page properties, as a map or keyword list.
Functions
The attribute that names an element for click maps.
<a href="/signup" {PrivatusAnalytics.click_attrs("pricing.cta")}>Start</a>The name must be 1 to 100 letters, digits or _ . : -. Any other name
returns [] (and logs a warning outside production).
Like click_attrs/1, as safe HTML for EEx.
Like click_attrs/1, as a plain escaped string.
Whether the tag prints for these options (site id set and enabled).
Attributes that send an event when the element is clicked. On a <form>
the event is sent when the form is submitted.
<button {PrivatusAnalytics.event_attrs("Export CSV", format: "csv")}>Export</button>Returns [] when the name is empty. Printed even when the tag is off, so
your HTML is the same in development and production.
Like event_attrs/2, as safe HTML for EEx: <button <%= event_attrs_html("Export CSV") %>>.
Like event_attrs/2, as a plain escaped string of attributes.
Page properties as a JSON object string, cleaned the same way as the tag does.
Handy for a LiveView hook: data-privatus-props={PrivatusAnalytics.props_json(%{plan: @plan})}.
Adds page properties for this request. They are sent with every pageview and event on the page. Use groups (plan, role), never anything that names a person.
conn |> PrivatusAnalytics.put_props(%{plan: "pro", role: "admin"})The tag and the Plug read them from the conn. Calls merge.
The official queue stub script, so privatus.track calls made before the
tracker loads are kept. Also printed by the tag with queue_stub: true.
The script tag as safe HTML ({:safe, iodata}), ready for EEx and HEEx.
<%= PrivatusAnalytics.tag(nonce: @csp_nonce) %>Without a site id or when the tag is not enabled, prints only the queue stub
(with queue_stub: true) or nothing.
The script tag as a plain string. Empty when nothing should print.