Skip to content

Events

The SDK records what happens in your app as events, keeps them in the browser, and sends them in batches. Your team uses them in segments, message triggers, workflows and goals. There is nothing to draw.

Without any code of yours, the SDK records:

EventWhen
app_openedThe page starts, and each time it comes back to the front.
session_endedThe page is hidden or closed, with the whole seconds it was in front.
app_updatedThe first load after your appVersion changed in this browser, from and to.
push_openedA push was tapped (Push).
link_openedA link handed to handleLink was served: one under your link base, or a page of yours that carried a saved link or an email’s click (Links).
push_permission_changed, location_permission_changedA permission’s answer changed (below).

What users do with in-app messages and the messenger goes to PurrCRM through their own requests (receipts, messages), not through this queue. These are standard events, on Settings › your app › Data › Events.

Name each of your screens as it shows:

crm.screen("wallet"); // records screen_viewed {screen: wallet}

Use a name, the same every time; never a path or an id. The same screen named twice in a row is one view, so calling it on every render is fine. With a router:

router.afterEach((to) => {
const name = screenNameOf(to.path); // "/offers/42" → "offer"
if (name !== null) crm.screen(name);
});
  1. Declare them in CrmConfig.customEvents:

    new Crm({
    config: {
    apiBase: "https://app.purrcrm.com",
    appKey: "app_...",
    appVersion: "2.4.0",
    customEvents: ["order_placed", "table_booked"],
    },
    auth,
    });
  2. Record them:

    crm.track("order_placed", { total: "1250", items: "3" });

Names are lower case with underscores. Props are strings, at most 10, each trimmed and cut to 120 characters; an empty one is left out. PurrCRM reads a number, a date or money from its string by the event’s definition. Money is a whole number of the smallest unit, such as pence: 1250 for £12.50.

The SDK drops a name you did not declare in customEvents, and writes a warning to the console, so a typo is found at your desk. On PurrCRM, a new event is accepted the first time and marked for review, unless your app has Strict data on, when it must be defined on Settings › your app › Data first. See Data.

Tell the SDK the user’s answer to a permission whenever you learn it, null for not asked yet:

await crm.permissionChanged("push", Notification.permission === "default" ? null : Notification.permission === "granted");

"push" and "location" are the permissions it knows. The first answer a browser gives is kept, not recorded; an event goes only when an answer this browser already knew changes.

Events wait in localStorage and leave in batches of up to 50, oldest first:

  • every 30 seconds while any wait (flushEveryMs);
  • at once when 50 are waiting;
  • as the page is hidden: switching tabs, minimising, going to another site, closing the tab. These go with fetch’s keepalive, so the request outlives the page. The SDK listens to visibilitychange and pagehide, and a page restored from the back-forward cache (pageshow) carries on.

crm.flushEvents() sends what is waiting now. A batch that fails waits 30 seconds, then twice as long each time up to 30 minutes, or as long as PurrCRM asks after too many requests; hiding the page tries once whatever the wait. Each event has its own id, so a batch sent twice is kept once. At most 500 wait, and an event older than seven days is dropped rather than sent. A batch PurrCRM refuses as malformed is dropped.

Each tab of your app runs its own Crm, and each tab has its own queue (crm.queue.<tab>), so two tabs never write over each other’s events. The tab’s id lives in its sessionStorage, so a reload keeps its queue; a duplicated tab gets an id of its own. Every 15 seconds a tab writes a heartbeat (crm.beat.<tab>), and as a tab starts it adopts, and sends, the queues of tabs silent for more than 60 seconds: the events of a tab closed before it could send are sent by the next tab to open. A tab throttled in the background may have its queue adopted while it lives; its events are then sent twice, and kept once.

Where localStorage is refused (a private window, blocked site data, a sandboxed frame), everything is kept in memory instead: nothing throws, and events still waiting are lost when the page goes. The setup check’s Storage row says so.

The queue belongs to the user signed in. Events recorded before session is first called are held in memory only (the last 50), kept once the session says who it is, and dropped if that user turned product events off. Signing out, or another user signing in, drops what was waiting, so the next user in the browser never sends the last one’s.

If a user turns product analytics off in your settings, say so in their session: nothing is recorded or sent for them, and what was waiting is dropped.

crm.session({ guestId: user.id, productEventsOff: true });

Under your storagePrefix (crm. unless you name one) in localStorage: the event queues and heartbeats, the last version of your app that ran, the last answer to each permission, the app’s public face, and when a push last arrived (for the setup check). In sessionStorage: the tab’s id. None of it is a credential: the user’s token and the push token are only ever in memory. The SDK sets no cookies.

Events your servers see, such as a payment, are better sent from the backend, with the server API.