Tracking Events
Track pageviews, clicks, purchases, and custom events with the Apex SDK. Events are batched in memory and sent to your Apex instance automatically.
track()
The primary method for recording events. Call it anywhere after init().
import { track } from "@apex-inc/sdk";
track("button_click", { buttonId: "cta-hero", label: "Get Started" });
| Parameter | Type | Description |
|---|---|---|
eventrequired | string | Name of the event. Use snake_case by convention (e.g. page_view, form_submit). |
properties | Record<string, unknown> | Arbitrary key-value pairs attached to the event. Numbers, strings, and booleans are recommended. |
Naming Conventions
Use descriptive snake_case names. Avoid generic names like click — include context so events are useful in reports. Good examples: page_view, signup_started, purchase_completed.
Examples
Pageviews
track("page_view", {
path: window.location.pathname,
title: document.title,
referrer: document.referrer,
});
Purchases
track("purchase_completed", {
orderId: "ord_8x92k",
total: 149.99,
currency: "USD",
plan: "pro_annual",
});
trackForm()
A convenience method that tracks a form_submit event and calls identify() with the submitter's email in a single call.
import { trackForm } from "@apex-inc/sdk";
trackForm({
email: "jane@acme.co",
formId: "demo-request",
action: "/thank-you",
fields: { company: "Acme", role: "VP Marketing" },
});
| Parameter | Type | Description |
|---|---|---|
emailrequired | string | The submitter's email address. Also used for identity stitching. |
formId | string | An identifier for the form (e.g. 'demo-request', 'newsletter'). |
action | string | The form's action URL or destination page. |
fields | Record<string, unknown> | Additional form field values to include as event properties. |
Tip
trackForm() is the fastest way to connect a form submission to a known user.
It handles both the event and the identity call for you.
How Batching Works
Events are not sent individually. The SDK queues them in memory and flushes the batch to POST /api/events when any of these conditions are met:
- Timer — every
flushIntervalmilliseconds (default: 5 seconds) - Queue size — when the queue reaches
flushAtevents (default: 20) - Page lifecycle — on
beforeunloadorvisibilitychange(browser only)
You can also force an immediate flush:
import { flush } from "@apex-inc/sdk";
flush();
Warning
Calling track() before init() logs a warning and drops the event.
Always initialize the SDK first.
Next Steps
- Identify users — tie events to known users and stitch anonymous sessions
- Installation reference — configuration options and environment setup