report / setReportUrl / createData
Send telemetry beacons to your own endpoint.
API
setReportUrl(config)
Configure the default endpoint once at startup. Accepts a URL string, or an object:
| Field | Description | Type |
|---|---|---|
url |
Default endpoint for every report() without its own url |
string |
userIdCookie |
Cookie holding the user id, included by createData() |
string |
getReportUrl()
The configured endpoint, or ''.
report({ url?, type?, payload })
Sends payload. Prefers navigator.sendBeacon and falls back to a 1x1 image request.
Returns true when some transport accepted it, false when nothing could send, including
when no endpoint has been configured.
createData(params?)
Builds the standard envelope: event id, page URL, timestamp, referrer, viewport, user agent,
plus userId when userIdCookie is configured. Your params are applied last. Returns {}
under SSR.
Example
import { createData, report, setReportUrl } from 'ranuts';
setReportUrl({ url: 'https://telemetry.example.com/collect', userIdCookie: 'uid' });
report({ payload: { ...createData(), type: 'page_view' } });Notes
- There is no default endpoint, by design. A library cannot know where your telemetry
belongs, so
report()returnsfalserather than guessing. - The transport is chosen by whether sendBeacon actually succeeded, not by whether
navigatorexists.sendBeaconalso returnsfalsewhen the browser's queue is over quota; that case falls through to the image beacon too. - Call
createData()per event, not once at setup. It snapshots the URL and timestamp at the moment it runs; hoisting it out of a handler makes every later event report the state of page load.