package privatus-analytics

All checked builds pass
  1. Overview
  2. Docs
Cookieless Privatus Analytics tracking script for OCaml web apps

Install

dune-project
 Dependency

Authors

Maintainers

Sources

privatus-analytics-1.0.0.tbz
sha256=9b9e413575d5b801aa21b221f6a79a222f8aabb05fd4a89295822798ef4b2266
sha512=e4f4c70636aaa45f9731c3b9f5860888e80b5c19c6837950171329b409aa0808b8f86ea0ddce770df4b50fae8fa01e9d67a785bfe247086f2f37acc6984929e4

doc/privatus-analytics/Privatus_analytics/index.html

Module Privatus_analyticsSource

Prints the cookieless Privatus Analytics tracking script and the HTML attributes the tracker reads.

This library only builds HTML text. It never sends events from your server, never makes web requests, sets no cookies and reads no IP addresses or browser details.

let config = Privatus_analytics.Config.make ~site_id:"pa_YOURSITEID" ()
let head = Privatus_analytics.tag ~config ()

In production it gives:

<script defer src="https://privatusstats.com/js/pa.js" data-site="pa_YOURSITEID"></script>

See the Privatus Analytics docs and the script attributes reference.

Sourceval version : string

The library version, "1.0.0".

Values

Sourcetype value = [
  1. | `String of string
  2. | `Int of int
  3. | `Float of float
  4. | `Bool of bool
  5. | `List of value list
  6. | `Null
]

A prop value. `Null leaves the prop out (and, in per call page props, removes a configured one).

Sourcetype props = (string * value) list

Props as key and value pairs, for example [ ("plan", `String "pro"); ("seats", `Int 5) ].

Never put names, emails, user ids or anything else that points to one person in props. Put groups (like the plan or the role), not people.

Sourcetype spa = [
  1. | `Auto
  2. | `History
  3. | `Hash
  4. | `Off
]

Single page app mode. Only modes other than `Auto print a data-spa attribute.

Settings

Sourcemodule Config : sig ... end

The script tag

Sourceval tag : ?config:Config.t -> ?site_id:string -> ?script_host:string -> ?modules:string list -> ?excluded_paths:string list -> ?domains:string list -> ?spa:spa -> ?manual:bool -> ?enabled:bool -> ?extra_attributes:(string * string) list -> ?queue_stub:bool -> ?props:props -> ?props_fn:(unit -> props) -> ?nonce:string -> unit -> string

The HTML for the head of your page. When there is no site id or the tag is off, it is only the queue stub (when queue_stub is on) or "". Every optional argument overrides the matching setting in config for this call.

props are merged over the configured props, and a `Null value removes one. props_fn is for framework helpers: its props sit between the configured ones and props, and it is only called when a tag prints. nonce is your Content Security Policy nonce. It goes on every script tag.

With page props or queue_stub, small inline scripts print before the tracker tag, one per line.

Sourceval queue_stub : string

The official queue stub script body.

Sourceval queue_stub_script : ?nonce:string -> unit -> string

The queue stub in a <script> tag.

Sourceval page_props_json : props -> string option

Page props as a JSON object that is safe inside a <script> tag, or None when no props are left. Keys follow the event prop rules. Numbers and booleans stay JSON numbers and booleans, lists stay lists (at most 30 items). <, >, &, U+2028 and U+2029 are written as < and so on.

Custom events and click maps

Sourceval event_attrs : ?props:props -> string -> string

event_attrs ~props:[ ("format", `String "csv") ] "Export CSV" is data-privatus-event="Export CSV" data-privatus-prop-format="csv".

Put it inside a button, link or form tag. On a form the event is sent when the form is submitted. The name is trimmed and cut to 120 characters, and an empty name gives "". Prop keys are made lowercase, anything other than a-z, 0-9 and _ becomes _, and keys are cut to 64 characters. At most 30 props. Values become text (lists joined with commas) cut to 500 characters, and `Null values are left out. Every value is escaped for HTML. The attributes print even when the tag is off.

Sourceval event_data : ?props:props -> string -> (string * string) list

The same attributes as name and value pairs, not escaped, for HTML builders. [] for an empty name.

Sourceval click_attr : string -> string

click_attr "pricing.cta" is data-privatus-click="pricing.cta", or "" when the name is not valid.

Sourceval click_data : string -> (string * string) list

The click map attribute as a name and value pair, or [].

Sourceval valid_click : string -> bool

Whether a click map name is valid: 1 to 100 letters, digits or _ . : -.

Helpers

Sourceval attrs_to_html : (string * string) list -> string

Turns attribute pairs into name="value" text with every value escaped.

Sourceval escape_html : string -> string

Escapes ampersands, angle brackets and both kinds of quotes for HTML.

Sourceval normalize_key : string -> string

Cleans one prop key the way the tracker expects.

Sourceval marker : string

"/js/pa.js". A page that contains this already has the tag.

Sourceval inject_html : tag:string -> string -> string option

inject_html ~tag html puts tag just before the first </head> (any case). None when tag is empty, the page already has the tag, or there is no </head>. Used by framework middleware.