JavaScript SDK
@tbdhdev/beacon-sdk is the same tracker as the script tag, as a package. Use it
in apps with a build step; it has integrations for the common
frameworks.
npm install @tbdhdev/beacon-sdkQuick start
Section titled “Quick start”Call init() once in code that runs in the browser:
import { init } from "@tbdhdev/beacon-sdk";
init({ websiteId: "your-website-uuid", endpoint: "https://beacon.example.com/x/e",});That records the first pageview and every client-side navigation (pushState, back and
forward, and replaceState to another path). The website id and event URL are on the website’s
Settings tab under Tracking code.
Options
Section titled “Options”| Option | Default | What it does |
|---|---|---|
websiteId |
required | Website id from the dashboard. |
endpoint |
/x/e |
Event URL: your Beacon origin, the ingest prefix and the event path. Relative URLs resolve against the page. |
autoPageviews |
true |
Record pageviews from the browser history. Set false to call page() from your router. |
outbound |
false |
Track clicks on links to other hosts. |
downloads |
false |
Track clicks on file links (pdf, zip, csv, docx, mp4, …). |
webVitals |
false |
Send LCP, INP, CLS and TTFB when the page is hidden, and CLS and INP again if they grow. |
hashRouting |
false |
The app routes in the URL hash (/#/pricing): record /pricing as the page. |
import { identify, page, track } from "@tbdhdev/beacon-sdk";
track("signup_clicked", { plan: "pro" }); // prop values: string, number or booleanidentify("user_123", { plan: "pro" }); // opt-in, for your own signed-in users; kept in memory onlypage(); // record a pageview for the current URL, or page("/virtual/path")- SSR-safe. Every function does nothing on the server, so importing the SDK in shared code is fine.
- Call order doesn’t matter. Calls made before
init()are queued and sent right after it. init()runs once. Later calls are ignored, so React Strict Mode and remounts are safe.- Repeated pageviews are skipped.
page()for the page already recorded does nothing. Campaign parameters (utm_*,gclid,fbclid,msclkid) don’t count, so removing them after landing is not a new page. - Prerendered pages send nothing until the visitor actually opens them. A page restored from the back/forward cache is recorded again.
- Link clicks include middle-clicks, links inside open shadow roots, and same-origin
<a download>links whatever the file extension.
Elements with data-beacon-event send events on click, the same as with the
script tag.
Blocked by ad blockers?
Section titled “Blocked by ad blockers?”A full-stack app can send events through one of its own routes instead. See Sending events through your own site.