How to use (Browser SDK)
1. Installation
npm install @cruxstack/browser-sdk
or with yarn:
yarn add @cruxstack/browser-sdk
2. Initialization
Initialize the SDK with your clientId
(optionally add tenant/user context):
import { init, cruxCustom, getUserTraits } from '@cruxstack/browser-sdk';
init({
clientId: 'web_demo_client_001', // Required
customerId: 'cust-2201', // Optional
customerName: 'Acme, Inc.', // Optional
// userId: 'user-8731', // Optional; omit for anonymous
autoCapture: true, // Optional (default: true)
debugLog: false // Optional (default: false)
});
3. Track Your First Event
// Track a custom event
cruxCustom('purchase_completed', {
productId: 'SKU-123',
amount: 99.99,
currency: 'USD'
});
// Fetch user traits (optional)
const traits = await getUserTraits('user-8731');
That's it! Your event is validated and sent to your analytics backend.