@hiveit/browser
v1.1.1
Published
hiveit analytics for the browser: anonymous ids, pageviews, optional click autocapture. Also ships a <script> build.
Maintainers
Readme
@hiveit/browser
hiveit analytics in the browser: an anonymous id kept in localStorage, pageviews across
client-side navigation, optional click autocapture, and delivery that survives the page
being closed.
import { hiveit } from "@hiveit/browser";
// Point at a proxy route on your own domain, so no key ships to the browser
// and no ad blocker sees a third-party host. See @hiveit/express or @hiveit/next.
const analytics = hiveit({ endpoint: "/api/hiveit/e", autocapture: true });
analytics.track("added_to_cart", { sku: "A-1" });
analytics.identify("user-42", { plan: "pro" }); // merges the anonymous history, once
analytics.reset(); // log out: a fresh anonymous idhiveit() attaches to the page as soon as it is called. When creation and wiring have to be
separate steps (a React render followed by an effect), pass start: false and call
analytics.start() yourself; analytics.stop() detaches again and leaves the client usable.
Or without a bundler:
<script src="https://unpkg.com/@hiveit/browser/dist/hiveit.iife.js"
data-endpoint="/api/hiveit/e" data-autocapture></script>
<script>
hiveit.track("signup", { plan: "pro" });
</script>What it sends
Every event carries the current page and campaign context: $current_url and $pathname
(query and hash stripped, because those routinely carry tokens), $referrer,
$referring_domain, $screen_*, $viewport_*, $locale, $lib, $lib_version, and any
utm_*, gclid or fbclid parameters by name. $pageview adds $title.
With autocapture, a click on a link, button, form control or role="button" sends
$autocapture_click with the element's tag, visible text, id, classes, href and
aria-label. What a user typed is never read: text is taken from textContent only,
and skipped entirely for password, email, tel, search, number, date, file and hidden inputs.
Add data-hiveit-no-capture to any element to exclude it and everything inside it.
Delivery
Events are batched and retried by @hiveit/core. Unsent events are kept in localStorage
(newest 200) so a reload does not lose them, and the queue is flushed on pagehide and when
the tab is hidden, using fetch with keepalive so the browser delivers it after the page
is gone. Where storage is blocked (private mode, cleared site data) everything still works,
it just does not persist.
Calling hiveit() during server rendering is safe: with no window it installs no
listeners and tracks nothing, so the same code can run in both places.
