Web apps
This guide shows how to send signals from a web app with the JavaScript client.
You need the app's id and key. Make an app on the dashboard's Apps page and copy both: see Getting started.
Add the client
Download the client from /omerta.js and put it in your site. It has no imports and no dependencies. Load it as a module:
<script type="module">
import { createClient } from "./omerta.js";
// ...
</script>
The client needs fetch and crypto.subtle, which browsers give to pages served over HTTPS (and to localhost).
Create it
const omerta = createClient({
endpoint: "https://api.omertasignals.com",
app: "<the app's id>",
key: "<the app's key>",
});
endpointis where the service runs. A trailing/is ignored.appandkeyare the id and key from the Apps page.
The key is visible to anyone who loads the page. That is acceptable: it lets them send signals for your app, not read them.
Identify, track, flush
identify(user) says who the following signals belong to. Call identify(undefined) on sign-out, so later signals carry no user. A signal keeps the user that was set when track was called.
omerta.identify("ada@example.com");
omerta.track("app.launched");
omerta.track("plan.chosen", { plan: "pro" });
omerta.identify(undefined);
track(type, payload) queues a signal, stamped with the current time in whole seconds. It throws an Error at once if the input is wrong, and queues nothing:
typemust be 1 to 64 characters, start with a letter, and hold only letters, digits,.,_and-.payloadis optional. It must be an object with string values and at most 32 keys.- Each payload key must be 1 to 64 characters.
- Each payload value must be at most 1000 characters. Characters are counted as UTF-16 code units, so most emoji count as 2, as the server does.
flush() sends what is queued and returns a promise of the number of signals the server accepted. It sends at most 1000 signals and at most 1,048,576 bytes per request, in queue order. Calls to flush() never overlap: a later call waits for the running one.
try {
const accepted = await omerta.flush();
} catch (err) {
console.error(err.message); // the server's error, or "HTTP <status>"
}
If the server refuses a batch with 400, those signals are dropped from the queue and the promise rejects with an Error whose message is the server's error. Signals behind that batch stay queued for the next flush(). Any other failure (401, 5xx, or a network error) keeps the signals queued, so the next flush() sends them again.
The queue holds at most 10000 signals. When track is called on a full queue, the oldest queued signal is dropped, and no error is thrown.
When to flush
The client has no timers, so the page decides when to flush. This flushes every 30 seconds and when the page is hidden:
setInterval(() => omerta.flush().catch(() => {}), 30_000);
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") omerta.flush().catch(() => {});
});
Signals still queued when the page closes are lost, because the client stores nothing in the browser.
Visitors without an identifier
If you never call identify, or call identify(undefined), the signals carry no user. The service then gives each visitor a daily identifier. See How identifiers are kept.