SDKs
Altertable provides JavaScript and React SDKs that make it easy to track events, identify users, and manage sessions in your application. The SDKs handle event queuing, session management, and privacy compliance automatically.
Available SDKs
- JavaScript SDK (
@altertable/altertable-js) - Works with any JavaScript framework or vanilla JavaScript - React SDK (
@altertable/altertable-react) - React Hooks and components with type-safe funnel tracking
JavaScript SDK
The JavaScript SDK works with any JavaScript framework or vanilla JavaScript. Browse source code on GitHub.
Installation
npm install @altertable/altertable-js# orpnpm add @altertable/altertable-js# oryarn add @altertable/altertable-js# orbun add @altertable/altertable-js
Quick Start
import { altertable } from '@altertable/altertable-js';// Initialize with your API keyaltertable.init('YOUR_API_KEY');// Track an eventaltertable.track('Step Completed', {step: 1,});// Identify a useraltertable.identify('u_01jza857w4f23s1hf2s61befmw', {name: 'John Doe',company: 'Acme Corp',role: 'Software Engineer',});
Initialization
altertable.init(apiKey, config?)
Initializes the Altertable SDK with your API key and optional configuration.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
apiKey | string | Yes | Your Altertable API key |
config | AltertableConfig | No | Configuration options |
Example:
altertable.init('YOUR_API_KEY', {environment: 'development',debug: true,});
Configuration
The SDK supports various configuration options:
| Property | Type | Default | Description |
|---|---|---|---|
baseUrl | string | "https://api.altertable.ai" | The base URL of the Altertable API |
environment | string | "production" | The environment of the application |
autoCapture | boolean | true | Whether to automatically capture page views and events |
release | string | The release ID of the application | |
debug | boolean | false | Whether to log events to the console |
persistence | StorageType | "localStorage+cookie" | The persistence strategy for storing IDs |
trackingConsent | TrackingConsentType | "granted" | The tracking consent state |
Storage Options
The SDK supports multiple storage strategies for persisting device and session IDs:
| Value | Description |
|---|---|
"localStorage" | Use localStorage only |
"sessionStorage" | Use sessionStorage only |
"cookie" | Use cookies only |
"memory" | Use in-memory storage (not persisted) |
"localStorage+cookie" | Use localStorage with cookie fallback |
Example:
altertable.init('YOUR_API_KEY', {persistence: 'localStorage+cookie', // Default: localStorage with cookie fallback});
Privacy & Consent
The SDK includes built-in tracking consent management to help you comply with privacy regulations.
Consent States
| Value | Description |
|---|---|
"granted" | User has granted consent for tracking |
"denied" | User has denied consent for tracking |
"pending" | User hasn't made a decision yet |
"dismissed" | User dismissed the consent prompt |
Managing Consent
// Get current consent stateconst consent = altertable.getTrackingConsent();// Update consent statealtertable.configure({trackingConsent: 'granted',});
When consent is "pending" or "denied", events are queued and sent once consent is granted.
Updating Configuration
You can update the configuration after initialization:
altertable.configure({trackingConsent: 'granted',debug: true,});
React SDK
The React SDK provides Hooks and components with type-safe funnel tracking. Browse source code on GitHub.
Installation
npm install @altertable/altertable-js @altertable/altertable-react# orpnpm add @altertable/altertable-js @altertable/altertable-react# oryarn add @altertable/altertable-js @altertable/altertable-react# orbun add @altertable/altertable-js @altertable/altertable-react
Quick Start
import {AltertableProvider,useAltertable,} from '@altertable/altertable-react';import { altertable } from '@altertable/altertable-js';// Initialize the core SDKaltertable.init('YOUR_API_KEY');function App() {return (<AltertableProvider client={altertable}><SignupPage /></AltertableProvider>);}function SignupPage() {const { track } = useAltertable();function handleStart() {track('Signup Started', { source: 'homepage' });}return <button onClick={handleStart}>Start Signup</button>;}
Server-Side Rendering (SSR)
For server-side rendering, initialize the SDK in a useEffect():
import { useEffect } from 'react';import { altertable } from '@altertable/altertable-js';import { AltertableProvider } from '@altertable/altertable-react';function App() {useEffect(() => {altertable.init('YOUR_API_KEY');}, []);return (<AltertableProvider client={altertable}><YourApp /></AltertableProvider>);}
Type-Safe Funnel Tracking
The React SDK provides type-safe funnel tracking to ensure compile-time safety for your funnel definitions:
import {type FunnelMapping,useAltertable,} from '@altertable/altertable-react';interface SignupFunnelMapping extends FunnelMapping {signup: [{ name: 'Signup Started'; properties: { source: string } },{ name: 'Signup Completed'; properties: { userId: string } },];}function SignupPage() {const { selectFunnel } = useAltertable<SignupFunnelMapping>();const { trackStep } = selectFunnel('signup');function handleStart() {trackStep('Signup Started', { source: 'homepage' });}function handleComplete(userId: string) {trackStep('Signup Completed', { userId });}return (<div><button onClick={handleStart}>Start</button><button onClick={() => handleComplete('u_01jza857w4f23s1hf2s61befmw')}>Complete</button></div>);}
Features
Both SDKs provide:
- Automatic page view tracking – Captures page views automatically
- Identity management – Handles device, distinct, and session IDs automatically
- Event queuing – Queues events when offline or consent is pending
- Privacy compliance – Built-in tracking consent management
- Multiple storage options – localStorage, cookies, or both
- TypeScript support – Full TypeScript definitions included
- Lightweight – Minimal bundle size with no external dependencies
Next Steps
- Track Events: Learn how to track product events
- Identify Users: Set up user identification
- API Reference: Direct API access when needed