Installation
Install and configure the @apex-inc/sdk for server-side and client-side event tracking.
Install the Package
npm install @apex-inc/sdk
Info
The SDK uses the global fetch API. Node.js 18+ includes fetch natively — no polyfill needed.
On Node 16 or earlier, install node-fetch as a peer dependency.
Initialize the SDK
Call init() once at application startup before any track() or identify() calls.
import { init } from "@apex-inc/sdk";
init({
projectKey: "your-project-key",
});
Your project key is available in the Apex dashboard under Settings > Snippet.
Configuration Options
| Parameter | Type | Description |
|---|---|---|
projectKeyrequired | string | Your Apex project key. Found in Settings > Snippet. |
apiUrl | string | Base URL for the Apex API. Defaults to window.location.origin in browsers, or http://localhost:3000 in Node. |
flushInterval | number | How often queued events are sent to the server, in milliseconds. Default: 5000 (5 seconds). |
flushAt | number | Maximum number of events to queue before an automatic flush. Default: 20. |
debug | boolean | When true, logs SDK activity to the console. Default: false. |
Browser Usage
In a browser environment, the SDK automatically flushes queued events when the user navigates away (beforeunload) or switches tabs (visibilitychange), so you won't lose events.
import { init, track, shutdown } from "@apex-inc/sdk";
init({ projectKey: "pk_live_abc123" });
track("page_view", {
path: window.location.pathname,
referrer: document.referrer,
});
Node.js Usage
In a Node.js server, call shutdown() during graceful shutdown to flush remaining events and clear timers.
import { init, track, shutdown } from "@apex-inc/sdk";
init({
projectKey: "pk_live_abc123",
apiUrl: "https://your-apex-instance.com",
});
track("api_request", { endpoint: "/checkout", method: "POST" });
process.on("SIGTERM", () => {
shutdown();
process.exit(0);
});
Tip
Set debug: true during development to see [apex-sdk] log messages in your console —
helpful for verifying events are queued and flushed correctly.
Next Steps
- Track events — pageviews, clicks, purchases, and custom events
- Identify users — associate events with known users and stitch anonymous sessions