Initialization
Configure the SDK with your project credentials and preferences.
Overview
The init() method must be called before you can track any events. It initializes the local queue, generates an anonymous identity if one doesn't exist, and starts the session heartbeat.
Basic Initialization
In a standard single-page application (React, Vue, etc.), you should call init() once in your entry file (e.g., main.js, index.js, or App.tsx).
import EmpMix from '@empmix/sdk-js';
// Initialize with your Public API Key
EmpMix.init('YOUR_PUBLIC_API_KEY');Advanced Configuration
You can pass an optional configuration object as the second argument to customize the SDK's behavior.
EmpMix.init('YOUR_PUBLIC_API_KEY', {
autoPageView: true,
batchSize: 20,
flushInterval: 5000,
debug: process.env.NODE_ENV !== 'production'
});Configuration Options
| Parameter | Type | Required | Description |
|---|---|---|---|
| autoPageView | boolean | No | Automatically tracks a 'Page View' event when the browser's history changes. Default: true. |
| batchSize | number | No | The maximum number of events to hold in the local queue before forcing a flush to the server. Default: 20. |
| flushInterval | number | No | The time in milliseconds between automatic flushes of the event queue. Default: 5000. |
| debug | boolean | No | If true, the SDK will print verbose logs and payload details to the browser console. Useful for local development. Default: false. |
| apiHost | string | No | Override the default Emp-Mix ingest URL. Useful if you are proxying requests to bypass ad-blockers. Default: 'https://api.empmix.com'. |
Expected Result
If debug: true is enabled, you should see a log in your browser console stating [EmpMix] Initialized successfully. Distinct ID: ....